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:
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
Build steps can now reach that hostname without touching any files:
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:
With Docker Compose:
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:
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.
Related reading
- How to update existing images with docker-compose?
- How to upgrade docker container after its image changed
- How to use --volume option with Docker Toolbox on Windows?
- How to use an init container to check if MySQL is ready for connections?
- How to update nginx-ingress controller so that latest ingress paths are used?
- How to upload and deploy zip file to AWS elastic beanstalk via CLI?
- How to use bash with an Alpine based docker image?
- How to use Docker Image in ECR with AWS EKS
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.