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...