Add a Host Entry to a Docker Container in 1 Simple Step

Introduction

Occasionally when working with a pre-built Docker Container image, it will become necessary for you to add an entry to the /etc/hosts file within the Docker container. Docker provides an easy way to add a host entry to a docker container. An example of when would become necessary is if you are installing WordPress as a Docker container and then receiving the WordPress loopback error when you login to the admin screen.

Can’t I just use a text editor to manually edit the host file?

docker hosts file

You can use the text editor to edit the /etc/hosts file within the container, however, once you rebuild the container, the /etc/hosts file will return to it’s previous state, eliminating your entry. This is because the hosts file is not stored in a persistent volume. Thus, manually editing the file can only be used as a temporary solution.

How to insert an entry into the hosts file with a docker run command

Docker has a built in solution that will allow you to have your docker host automatically insert lines into the hosts file of a docker container when the image is initially starting up. All you need to do is include the following flag within your docker run command. This will allow you to add a host entry to a docker container.

--add-host yourdomain.com:127.0.0.1

All you have to do is replace yourdomain.com with the host name or domain that you’d like to use and then replace 127.0.0.1 with the IP address that the host name needs to resolve to.

An example of using the --add-host flag within a docker run command is below:

docker run -d \
--name wordpress \
-p 8080:80 \
--add-host yourdomain.com:127.0.0.1 \
wordpress

The above command will run the wordpress container in detached mode, expose it on port 8080 and add a line in the /etc/hosts file that will resolve yourdomain.com to 127.0.0.1.

Before and After Examples of the Hosts File

Prior to inserting the --add-host command into your docker run command, the hosts file on the container will be populated with the entries that were entered in the original image, if any. The below output was generated by using the cat /etc/hosts command within a freshly ran Alpine Linux container.

::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      84e1cd04f888

The output below shows a freshly ran Alpine Linux container’s /etc/hosts file after inserting --add-host yourdomain.com:127.0.0.1 within the docker run command.

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
127.0.0.1       yourdomain.com
172.17.0.2      0ed7f031b7e5

As you can see, the Docker host inserted a new line in the hosts file on line 7 of the containers /etc/hosts file.

How to insert an entry into the hosts file via Docker Compose

If you use Docker Compose to run your Docker containers, then you will need to add extra_hosts: to your Docker compose file.

extra_hosts:
  - "yourdomain.com:127.0.0.1"

Just like using the --add-host flag within your docker run command, changing yourdomain.com to your host name and 127.0.0.1 to the IP address that you would like it to resolve to will automatically add a line within your containers /etc/hosts file with that information.

An example of the above section within a docker-compose.yml file is below:

version: '2.1'

services:
  wordpress:
    image: "wordpress"
    container_name: wordpress
    ports:
      - 8080:80
    extra_hosts:
      - "yourdomain.com:127.0.0.1"

The above docker-compose.yml service will run a basic wordpress container, expose it on port 8080, and add a line in the etc/hosts file that will resolve yourdomain.com to the IP address of 127.0.0.1.

That’s it!

Using the --add-host command to at a host entry to a docker container is the simplest way to ensure that your docker containers continue to work smoothly with your docker host setup and the rest of your docker services.

Adding the extra_hosts: portion to a docker_compose.yml file will ensure that your hosts are always added to your containers after you run the docker-compose up command. This will ensure your settings persist seamlessly across docker container image updates and other environment changes.

If you have any questions or comments, please leave them in the comments section below.

The article, Add a Host Entry to a Docker Container in 1 Simple Step, first appeared on Codeopolis.