How to keep changes in /etc/hosts inside container permanent using custom network interface?


So I usually use my container with network mode set to host, however I decided to use docker network create to create a new network device for the container (it is using default bridge mode). Now i want to add a line into /etc/hosts inside the contianer to block access to a single address, I can edit it and it works, however the line deletes itself the moment I have to kill the docker and restart the container. I even docker commit to a new image to make sure it saves, but it seems it doesn’t. Does the container generate /etc/hosts everytime i load the container? If so, how do I set it so the container will generate the additional line? The line is going to be hostname XXX.XXX.XXX?

Details for the setup
Host: Fedora 34/35 using moby-engine
Container: CentOS 7

If you run

docker container inspect "containername"

that will answer your question :slight_smile: Keep this command in mind. It gives you helpful information about the container and how its filesystem was built from different merged folders and mounted files.

Just the relevant parts:

        "ResolvConfPath": "/var/lib/docker/containers/09dc6c418db945985a3493e31680ab2037b2b4c88282955c797923ba5cefe972/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/09dc6c418db945985a3493e31680ab2037b2b4c88282955c797923ba5cefe972/hostname",
        "HostsPath": "/var/lib/docker/containers/09dc6c418db945985a3493e31680ab2037b2b4c88282955c797923ba5cefe972/hosts",
        "LogPath": "/var/lib/docker/containers/09dc6c418db945985a3493e31680ab2037b2b4c88282955c797923ba5cefe972/09dc6c418db945985a3493e31680ab2037b2b4c88282955c797923ba5cefe972-json.log",
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/2eae5c708d4d62ba30fcab347f90f8754a0e645cdf7cd4491bfb5a77a9756575-init/diff:/var/lib/docker/overlay2/01e14b2cb7794a2ca48572df966b6e6826feed5c238927d585baa2d5fa2e3a4f/diff:/var/lib/docker/overlay2/81450ab3cfc057ab0e60106189067e14244303f6e4f8a001884b7a51814ed2ee/diff:/var/lib/docker/overlay2/c58db506324b6522efa8d01400dfe0700371cd5208789f2f2910e19ada0d8082/diff:/var/lib/docker/overlay2/5416765de9833159c916f88a977e93c45ab2dd75de5ab841f9c2ad1ebebdd616/diff:/var/lib/docker/overlay2/6c4f2bfccde2ac42af718851337081373f7d543b7544afb580889421b765d077/diff",
                "MergedDir": "/var/lib/docker/overlay2/2eae5c708d4d62ba30fcab347f90f8754a0e645cdf7cd4491bfb5a77a9756575/merged",
                "UpperDir": "/var/lib/docker/overlay2/2eae5c708d4d62ba30fcab347f90f8754a0e645cdf7cd4491bfb5a77a9756575/diff",
                "WorkDir": "/var/lib/docker/overlay2/2eae5c708d4d62ba30fcab347f90f8754a0e645cdf7cd4491bfb5a77a9756575/work"
            "Name": "overlay2"

I tried to search for it on Google like I didn’t know anything about it. Actually, I didn’t remember exactly, because I almost never use it. So I searched for “docker add host”.
I could have searched for “docker add hostname” which would have also worked, but I usually try to use shorter words first, because Google smart enough to search for a part of a word.

The result is the docker run documentation, where you can find the --add-host parameter.

If you want to do it with Docker Compose, your next task is to search for “docker compose add host” :slight_smile: and you find “extra_hosts”