2

My Docker Version - 19.03.8, build afacb8b

I have pulled the ubuntu:18.04 from DockerHub. Then followed the below steps to add two new lines into /etc/hosts file of my docker image.

docker images
docker run --name ubuntu-18-1 -idt 8b353a2e5d1b /bin/bash
docker ps
# Executed the Container
docker exec -it 985ae774a352 /bin/bash
root@985ae774a352:/# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      985ae774a352

After adding the following two new lines (56.57.58.59 example1.com & 56.57.58.60 example2.com) into /etc/hosts file of my container, Then after i have saved, exited and then finally i have committed my container.

docker commit 985ae774a352 ubuntu-18-2
# Even after commit i can able to view the changes i made.
docker exec 985ae774a352 cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      985ae774a352
56.57.58.59 example1.com
56.57.58.60 example2.com

# Stopped & Removed the Container
docker container stop 985ae774a352
docker container rm 985ae774a352

# Launched the Container with udpated Image:-
docker run --name ubuntu-18-2 -idt 0ebc2d94a384 /bin/bash
docker ps
docker exec a8c1fa1dd65f cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      a8c1fa1dd65f

So in new image i can't see my changes that i made in /etc/hosts file. Please correct me if something wrong in above followed steps.

user4948798
  • 1,924
  • 4
  • 43
  • 89
  • You almost never want to use `docker commit`; building an image out of a Dockerfile using `docker build` is much better practice, and you'll have written down how to rebuild the image when there's a critical security issue in the base image in a year that you must fix. Similarly, setting up a DNS system (like BIND, DNSMasq, or a service-discovery system like Consul) will be more maintainable than trying to replicate `/etc/hosts` files in multiple places. – David Maze May 06 '20 at 11:14

2 Answers2

0

The problem is that the /etc/hosts is regenerated every time the docker run is executed.

In order to add new hosts, you can use the option --add-host. e.g.

docker run \
    --name ubuntu-18-1 \
    --add-host example1.com:56.57.58.59 \
    --add-host example2.com:56.57.58.60 \
    -idt ubuntu:18.04 \
    /bin/bash

More info can be found in the section Managing /etc/hosts of the docker documentation.

Stefano
  • 4,730
  • 1
  • 20
  • 28
  • Thanks Adii & Stefano for quick support. i have changed the logic and it works now. Thanks – user4948798 May 06 '20 at 09:45
  • just another note, you can also use the tag instead of the id in the docker run. e.g. `docker run -it --rm ubuntu:18.04 bash` is the equivalent of `docker run -it --rm 8b353a2e5d1b bash`. – Stefano May 06 '20 at 09:47
0

You can not commit changes to /etc/hosts, as docker maintain /etc/hosts when you start the container, you might notice one thing whenever you start the container, docker generate /etc/host

for example

::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2  c71f7cfa8140

you can look at bottom of /etc/hosts, you can see the last entry or somewhere in the file you will see running container id, you can cross-check with

docker ps

so you should change your logic something like here or better to use --add-host

Adiii
  • 54,482
  • 7
  • 145
  • 148