2

I'm building a simple docker image based on a Dockerfile, and I'd like to add an alias to the hosts file to allow me to access an application on my local machine rather than out on the internet.

When I run the following...

> docker build --add-host=example.com:172.17.0.1 -f ./Dockerfile -t my-image .
> docker run --name=my-container --network=my-bridge --publish 8080:8080 my-image
> docker exec -it my-container cat /etc/hosts

I don't see example.com 172.17.0.1 like I'd expect. Where does the host get added? Or is it not working? The documentation is very sparse, but it looks like I'm using the param correctly.

My Dockerfile is doing very little - just specifying a base image, installing a few things, and setting some environment variables. It looks somewhat like this:

FROM tomcat:9.0.40-jdk8-adoptopenjdk-openj9
RUN apt update
RUN apt --assume-yes iputils-ping
# ... a few more installs ...
COPY ./conf /usr/local/tomcat/conf
COPY ./lib /usr/local/tomcat/lib
COPY ./webapps /usr/local/tomcat/webapps
ENV SOME_VAR=some value
# ... more env variables ...
EXPOSE 8080

When the image is created and the container is run my web app works fine, but I'd like to have certain communications (to example.com) redirected to an app running on my local machine.

Luke
  • 18,811
  • 16
  • 99
  • 115

1 Answers1

2

when you run the container you can put the --add-host

docker run --add-host=example.com:172.17.0.1 --name=my-container --network=my-bridge --publish 8080:8080 my-image

the --add-host feature during build is designed to allow overriding a host during build, but not to persist that configuration in the image.

see also question for docker build --add-host command

ozs
  • 3,051
  • 1
  • 10
  • 19