Docker
Dockerfile
/etc/hosts
Containerization
DevOps

How to add /etc/hosts entries in Docker build (docker build --add-host)

Codemia Knowledge Hub ·

Don't edit /etc/hosts in the Dockerfile. Pass docker build --add-host name:ip when a build step needs a hostname, and docker run --add-host or Compose extra_hosts when the running container needs it. Docker manages name resolution around the container lifecycle, so a line appended in a RUN step is often temporary, misleading, or the wrong place to express the dependency.

Why appending to /etc/hosts in a RUN step misleads

Each RUN instruction runs in an intermediate build container with its own /etc/hosts, which Docker is free to manage. This can look like it works during the build:

dockerfile
1FROM alpine:3.20
2
3RUN echo "10.0.0.15 internal-api" >> /etc/hosts
4RUN grep internal-api /etc/hosts

But it says nothing clear about the runtime container, and it is easy to mistake this build-layer workaround for a durable fix. A successful echo during docker build does not mean the entry exists when the container starts: build and runtime are different phases.

Build-time resolution with --add-host

bash
docker build \
  --add-host internal-api:10.0.0.15 \
  -t my-image .

Build steps can now reach that hostname without touching any files:

dockerfile
1FROM alpine:3.20
2
3RUN apk add --no-cache curl
4RUN curl http://internal-api:8080/health

The Dockerfile stays focused on building the image, and the host mapping stays in the build invocation.

Runtime host mapping

If the running container needs the same mapping, configure it at runtime instead of baking it into the image. With docker run:

bash
docker run --add-host internal-api:10.0.0.15 my-image

With Docker Compose:

yaml
1services:
2  app:
3    image: my-image
4    extra_hosts:
5      - "internal-api:10.0.0.15"

The image stays portable: you deploy the same image to different environments with different mappings and never rebuild just because an IP changed.

Prefer DNS or service discovery

Static host entries solve narrow problems. DNS-based discovery is easier to maintain than fixed IPs:

  • In Docker Compose, containers on the same network can often resolve each other by service name.
  • In Kubernetes, a Service gives you a stable internal DNS name.
  • In managed infrastructure, private DNS records usually beat manual host file entries.

When a temporary /etc/hosts edit is acceptable

Some edge cases, such as a legacy installer that reads /etc/hosts directly, really need the line during a build step. Modify the file inside the same layer:

dockerfile
1FROM ubuntu:24.04
2
3RUN printf '10.0.0.15 internal-api\n' >> /etc/hosts \
4 && grep internal-api /etc/hosts

Treat this as a workaround. If runtime behavior depends on the mapping too, define it at runtime as well.

If a build step still cannot reach a dependency, confirm that the problem is really hostname resolution. Firewall rules, proxies, routing and private network boundaries produce the same symptom.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design