Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

Docker hostname alternative names

I have my docker-compose.yml looking like this:

version: "3.2"

services:
  web:
    build: /Users/marcin/Docker/definitions/php-nginx/php-7.2-ubuntu
    volumes:
      - /some_dir/:/some_dir
    working_dir: /usr/share/nginx/html/
    links:
      - db
    container_name: name.web
    hostname: name.local
    expose:
      - "10015"
    ports:
      - "313:22"
      - "8313:80"
      - "18313:443"
    environment:
      - VIRTUAL_HOST=name.local,name-be.local,name-com.local,subdomain.name-com.local
      - CERT_NAME=default
      - HTTPS_METHOD=noredirect
    networks:
      - default
      - proxy_default
  db:
    build: /Users/marcin/Docker/definitions/mysql/8.0
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=
      - MYSQL_USER=
      - MYSQL_PASSWORD=
    expose:
      - 3306
    volumes:
      - /Users/marcin/Docker/projects/name.local/mysql/data/:/var/lib/mysql/
      - /Users/marcin/Docker/projects/name.local/mysql/conf.d/:/etc/mysql/conf.d/source
      - /Users/marcin/Docker/projects/name.local/mysql/log/:/var/log/mysql/
    ports:
      - "33313:3306"
    container_name: name.db
    hostname: name.local
    networks:
      proxy_default:
      external:
        name: proxy_default 

I access this site (in web browser) using name.local and subdomain.name-com.local domain names - without any problems.

However today I had to run some tests and I found out that when running:

wget http://name.local

I can get site content without a problem but when running

wget http://subdomain.name-com.local

this host cannot be resolved.

How can I make that both domains will direct traffic to same hosts?

What I did at the moment was looking at /etc/hosts and I added the same IPs I found there for domain.local below

hostname: name.local

line like this:

extra_hosts:
   - "subdomain.name-com.local:192.168.0.3"
   - "subdomain.name-com.local:172.19.0.3"

and now it seems to be working fine. However, I don't like this solution that much (although it seems to be working without any problems) because I have to hardcode here some IP addresses and I'm not sure if they are going to change or not.

Is it possible somehow to add aliases of hostname here instead of specifying some magic IP addresses?

Upvotes: 1

Views: 818

Answers (1)

mpromonet
mpromonet

Reputation: 11963

In order to define hostname aliases you should use the network aliases feature :

ALIASES Aliases (alternative hostnames) for this service on the network. Other containers on the same network can use either the service name or this alias to connect to one of the service’s containers.

With your example it could looks like :

services:
  web:
    container_name: name.web
    hostname: name.local
    networks:
       default:
         aliases:
           - subdomain.name-com.local
       proxy_default
         aliases:
           - subdomain.name-com.local

Upvotes: 1

Related Questions