Accessing an Apache server in a Docker container using a hostname

12,371

Solution 1

docker run -h from docker networking

-h HOSTNAME or --hostname=HOSTNAME — sets the hostname by which the container knows itself. This is written into /etc/hostname, into /etc/hosts as the name of the container's host-facing IP address, and is the name that /bin/bash inside the container will display inside its prompt. But the hostname is not easy to see from outside the container. It will not appear in docker ps nor in the /etc/hosts file of any other container.

So you should not be able to ping your container with the hostname but this command is useful for the apache setup.

The Apache ServerName does the following, from Apache Docs:

The ServerName directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.

Additionally, ServerName is used (possibly in conjunction with ServerAlias) to uniquely identify a virtual host, when using name-based virtual hosts.

For accessing the subdomain you will need a Serverpath directive. Also you have to enter the IP address of vm (192.168.59.103) into the hosts file under the name mydomain.com. If you have done this you should be able to ping mydomain.com.

Here is an example (Question from askubuntu).

Solution 2

Apparently, all I needed to do was add a line to my /etc/hosts file:

192.168.59.103 sub.mydomain.com

And I could go to that address in the browser and the subdomain would be parsed correctly. I still don't really understand how this is affected by the docker run -h option, but at least it works. This isn't very extensible because I might want to use multiple subdomains in the future and have them all route to the web app container (which will serve different pages depending on the subdomain).

Solution 3

You can define the hostname and domainname of a container and use a shell script to update the /etc/hosts for you.

Here you can find some insides on how to: http://www.intrapesite.ro/access-docker-application-by-hostname.

It is possible to create a docker application that will listen to docker api (start and stop) and update a shared /etc/hosts file between host and guest (as an elegant, automated solution). Take a look, for example at ruby api: https://github.com/swipely/docker-api#events, maybe in conjunction with bjeanes/ghost hosts manager (github).

UPDATED:

Another way is to add a bridge network on the host, giving a static ip address such as 192.168.33.100.

http://www.intrapesite.ro/wp-content/uploads/2015/11/Captur%C4%83-de-ecran-de-la-2015-12-11-21-51-36.png

After that use the docker port forwarding which accept IP address as well. In docker compose yaml the syntax is:

ports:
    - "192.168.33.100:80:80"

The file /etc/hosts needs to be updated with the IP pointing to domain_url. This way can be added as many bridges having different static IP with only only modification of the /etc/hosts file per docker project.

The last solution looks the most elegant from all.

Share:
12,371
Taylor H
Author by

Taylor H

Updated on June 05, 2022

Comments

  • Taylor H
    Taylor H almost 2 years

    I have a web application running in a Docker container using Apache 2.4.6 and CentOS; this application in turn talks to several other Docker containers such as backend services and mysql through Docker links. I can connect to the app container locally using the ip of the boot2docker vm, i.e. http://192.168.59.103:80/. However, I would like to be able access it locally through a domain name with a subdomain, i.e. http://test.mydomain.com. The reason is that the app uses the $_SERVER[ 'HTTP_HOST' ] variable to authenticate requests, and in turn, the backend services use the HTTP_ORIGIN header to authenticate that requests from the web app are coming from a valid subdomain. As such, when testing the app in a browser, I need it to send those HTTP headers correctly. I only need to configure this for a local environment, not deployment (specifically for the purposes of running continuous integration tests against the app). I don't really know what to do to configure my local system (OS X Mavericks) and CI environment (Ubuntu 12.04) to make the domain name accessible.

    This is my current configuration:

    httpd.conf

    <VirtualHost *:80>
        DocumentRoot /var/www/site
        ServerAlias 172.*
        ServerName mydomain.com
        UseCanonicalName On
    
        <Directory "/var/www/site">
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    

    This is the command I run to start the web app container:

    docker run -d -p 80:80 -h mydomain.com --name web --link mysql:mysql --link memcached:memcached --link service:service --link api:base-api myorg/mywebapp:develop

    Under this setup, I can navigate to the app successfully at http://192.168.59.103 but because the HTTP_HOST is the IP, the app does not parse a subdomain and will not authenticate logins. Is there a way I can make it so local requests to something.mydomain.com will redirect to the docker container? What purpose does the ServerName mydomain.com line have in the apache config, and the -h mydomain.com parameter in the docker run command?