PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 21, 2022

[FIXED] How to provide custom name resolution in Docker containers

 September 21, 2022     dns, docker, docker-compose, virtualhost     No comments   

Issue

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


Solution

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"


Answered By - Ace7k3
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing