Search code examples
dockerdocker-composedocker-network

It's possible to tie a domain to the docker container when building it?


Currently in the company where I am working on they have a central development server which contains a LAMP environment. Each developer has access to the application as: developer_username.domain.com. The application we're working on uses licenses and the licenses are generated under each domain and are tied to the domain only meaning I can't use license from other developer.

The following example will give you an idea:

developer_1.domain.com ==> license1
developer_2.domain.com ==> license2
developer_n.domain.com ==> licenseN

I am trying to dockerize this enviroment at least having PHP and Apache in a container and I was able to create everything I need and it works. Take a look to this docker-compose.yml:

version: '2'
services:
    php-apache:
        env_file:
          - dev_variables.env
        image: reypm/php55-dev
        build:
            context: .
            args:
                - PUID=1000
                - PGID=1000
        ports:
            - "80:80"
            - "9001:9001"
        extra_hosts:
            - "dockerhost:xxx.xxx.xxx.xxx"
        volumes:
            - ~/var/www:/var/www

That will build what I need but the problem comes when I try to access the server because I am using http://localhost and then the license won't work and I won't be able to use the application.

The idea is to access as developer_username.domain.com, so my question is: is this a work that should be done on the Dockerfile or the Docker Compose I mean at image/container level let's say by setting up a ENV var perhaps or is this a job for /etc/hosts on the host running the Docker?


Solution

  • tl;dr

    No! Docker doesn't do that for you.


    Long answer:

    What you want to do is to have a custom hostname on the machine hosting docker mapped to a container in Docker compose network. right?

    Let's take a step back and see how networking in docker works:

    By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

    This network is not equal to your host network and without explicit ports exporting (for a specific container) you wouldn't have access to this network. All exposing does, is that:

    The exposed port is accessible on the host and the ports are available to any client that can reach the host.

    From now on you can put a reverse proxy (like nginx) or you can edit /etc/hosts to define how clients can access the host (i.e. Docker host, the machine running Docker compose).