Docker Community Forums

Share and learn in the Docker community.

How to build the docker image with customized IP in /etc/hosts file

Hi Team,

Is there any way to build the docker image with customized IP in /etc/hosts file ?

I can see --add-host option in docker container run command to add custom IP in existing docker image. But if I use this option I need to start the container with customized IP all time.

Thanks,
Ramesh

2 Likes

We now have a single-line workaround here

In my case, I need to have a specific /etc/hosts file during the build, so I think it could be great if it was added :slight_smile:

1 Like

Is this command adding the host entry to the container’s /etc/hosts file? I can not see that happening. If I bash inside container, and ping the host i just added, it is still not resolvable inside it. Please let me know if I am missing anything.

If want to add value in running container. Reference
docker exec -u 0 /bin/sh -c “echo '192.168.50.100 ’ >> /etc/hosts”

If want to add entry while staring the container.
docker run -it --add-host=:192.168.50.100