3

Assume I have the following file structure

.                                  
├── docker-compose.yml             
└── webserver                      
    ├── Dockerfile                 
    ├── html                       
    │   ├── test1                  
    │   │   └── index.html         
    │   └── test2                  
    │       └── index.html         
    └── vhosts.conf                

4 directories, 5 files

with docker-compose.yml:

version: "3.1"
services:

    webserver:
        build: webserver
        container_name: web
        working_dir: /application
        volumes:
            - ./webserver/html/:/application
        ports:
            - "80:80"

    other:
        image: php:7.2.7
        container_name: other
        tty: true

with Dockerfile:

FROM php:7.2.7-apache

COPY vhosts.conf /etc/apache2/sites-enabled/000-default.conf

with vhosts.conf:

<VirtualHost *:80>
    ServerName test1.localhost

    DocumentRoot /application/test1
    <Directory /application/test1>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName test2.localhost

    DocumentRoot /application/test2
    <Directory /application/test2>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

and the index.html files just with some content to distinguish them.

Additionally, I added the following lines to my docker host's /etc/hosts file:

127.0.0.1   test1.localhost
127.0.0.1   test2.localhost

Now I can run docker-compose build and docker-compose up to setup and run the containers. When running curl test1.localhost on either the host machine or the web container I get the expected content of the index.html. However, if I run the same command from the other container I get:

curl: (7) Failed to connect to test1.localhost port 80: Connection refused

I found at https://docs.docker.com/v17.09/engine/userguide/networking/configure-dns/ the following quote:

In the absence of the --dns=IP_ADDRESS..., --dns-search=DOMAIN..., or --dns-opt=OPTION... options, Docker uses the /etc/resolv.conf of the host machine (where the docker daemon runs).

So to my understanding the name resolution inside the containers simply uses the entries from the hosts file of the docker host. However, this fails on the other container because the /etc/hosts file resolves the names to 127.0.0.1 but the other container is not running a webserver. I want the names on other to be revolved to the web container.

This is obviously a minimal example. I need this kind of behaviour for a dev setup of a web app where I want to run cronjobs from a container separate from the one where the apache host is running. The scripts run by the cronjobs perform http requests to different hosts in the container.

I suspect the solution to somehow use the docker networks configuration...

2 Answers 2

7

Ok, with some help of the Docker Slack channel I found a solution, actually even two to be precise...

Using Docker Network aliases

First solution uses docker network aliases. Essentially I explicitly spelled out the default network both containers belong to and created some aliases for the webserver container.

docker-compose-yml:

version: "3.1"

services:

    webserver:
        build: webserver
        container_name: web
        working_dir: /application
        volumes:
            - ./webserver/html/:/application
        ports:
            - "80:80"
        networks:
            default:
                aliases:
                    - test1.localhost
                    - test2.localhost

    other:
        image: php:7.2.7
        container_name: other
        tty: true
        networks:
            - default

networks:
    default:

Using Docker Network links

Second solution uses docker network links. This works the other way around. For each dependend container (other) you can specify additional links / names that should be forwarded to the webserver.

docker-compose-yml: version: "3.1"

services:

    webserver:
        build: webserver
        container_name: web
        working_dir: /application
        volumes:
            - ./webserver/html/:/application
        ports:
            - "80:80"

    other:
        image: php:7.2.7
        container_name: other
        tty: true
        links:
            - "webserver:test1.localhost"
            - "webserver:test2.localhost"
1
0

If i correctly understand. You can resolve by containername between containers.

3
  • Thank you for your reply. Problem is that I want to resolve multiple names to the same container. In the example: test1.localhost and test2.localhost to container web.
    – Ace7k3
    Commented Jul 18, 2018 at 12:26
  • try to change your hosts file to: 127.0.0.1 test1.localhost test2.localhost localhost Commented Jul 18, 2018 at 12:28
  • AFAIK you cannot "forward" names in that way. Also even if I could what is test2.localhost localhost to mean?
    – Ace7k3
    Commented Jul 18, 2018 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.