docker-compose cannot resolve DNS

Viewed 54

The problem is that docker compose cannot build image, failing on RUN npm ci. But after hours of debugging, I isolated the problem and pinned it in this minimal setup:

My docker-compose.yml

version: '3.8'
services:
  myapp:
    build:
      dockerfile: Dockerfile
      context: .
      target: development
    command: sleep Infinity

My Dockerfile

FROM node:18-alpine AS development
RUN ping google.com

When I run docker compose -f docker-compose.yml up -d --build I'm getting error:enter image description here

What I tried so far

  1. In Dockerfile replace ping google.com to ping <real-ip>. ✅ And it works, so I assume it's DNS problem.
  2. Add dns into docker-compose.yml: dns: 8.8.8.8. ❌ No luck
  3. Run under super user sudo docker compose …. ❌ No luck
  4. I tried to build image from Dockerfile without compose, using just docker build command. ✅ And it works, so the problem with docker compose.
  5. Commented RUN ping … command, so it does not fail and runs sleep Infinity form the compose config. Then I connected into the container via docker exec -it <container> sh and was able to ping google and run npm ci. So when container is running it has access to DNS. The problem happens only in docker compose on the build stage from Dockerfile.

Environment

It's a VPS on hetzner. I ssh under a user with sudo and docker group.

0 Answers
Related