Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

Use domains instead of ports for multiple Docker containers

I'm working on creating Docker environment for multiple websites. I've decided to have for each domain separate docker-compose.yml file with multiple containers. At the moment I have something like this:

web:
  image: nginx:latest
  volumes:
    - /c/Users/marcin/dock-test/html/test-laravel-project-2/:/usr/share/nginx/html/test-laravel-project-2/
    - /c/Users/marcin/dock-test/nginx-conf/conf.d2/:/etc/nginx/conf.d
    - /c/Users/marcin/dock-test/nginx-log/:/var/log/nginx
  ports:
    - "8088:80"
  working_dir: /usr/share/nginx/html/test-laravel-project-2/
  links:
    - php
    - db
  container_name: lara.web
  environment:  
    - VIRTUAL_HOST='l2.app'
    - VIRTUAL_NETWORK='nginx-proxy'
  expose:
    - 8088
php:
  build: ../dockerfiles/
  dockerfile: Dockerfile-php7-fpm
  volumes:
    - /c/Users/marcin/dock-test/html/test-laravel-project-2/:/usr/share/nginx/html/test-laravel-project-2/
    - /c/Users/marcin/dock-test/php-config/:/usr/local/etc/php/
  working_dir: /usr/share/nginx/html/
  links:
    - db
  container_name: lara.php
db:
  image: mysql:5.7
  environment:
    MYSQL_ROOT_PASSWORD:
    MYSQL_DATABASE:
    MYSQL_USER: 
    MYSQL_PASSWORD:   
  expose:
     - 3306
  volumes:
    - /c/Users/marcin/dock-test/mysql2/:/var/lib/mysql/
    - /c/Users/marcin/dock-test/mysql-log2/:/var/log/mysql/
  ports:
    - "33062:3306"
  container_name: lara.db

This is working fine, but I've wanted to use domain for this because at the moment I need to use 8088 port like so: http://192.168.99.100:8088. Obviously I can in above config use 80:80 mapping but let's assume I want to use 8088 port (because for multiple sites I will need to use different port for each website).

And now the problem - how can I use domain instead of IP with port? I've added to my Windows (I'm using Windows as OS) l2.app with mapping to 192.168.99.100, but I still need to use it like this: http://l2.app:8088.

I've also tried nginx-proxy like this:

docker pull jwilder/nginx-proxy
docker-compose up -d
docker run -d -p 80:8088 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

but when I try to access website using http://l2.app I'm getting 503 and obviously when I'm using http://l2.app:8088 everything is fine.

I assume I probably misconfigured something with port mapping for nginx-proxy but have no clue in which place the mapping is incorrect. Could you please give me advice for this?

Upvotes: 0

Views: 3832

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12107

This is easier than I thought. Here are the steps through command line, but you should be able to create a corresponding docker-compose.yml file based on this. You don't need port mapping for the app containers if it runs on 80 by default (the proxy will know how to forward traffic to each container's 80 port, so you don't need to map them!)

  1. Edit your Windows hosts file to point the domains to 192.168.99.100

On Windows 2000/XP/Vista/7/8/10, the file location is C:\\Windows\\System32\\drivers\\etc\\hosts. Add the following lines

192.168.99.100 myapp1.com
192.168.99.100 myapp2.com
  1. Start nginx-proxy container first

Note that the double slash before var. You may only need a single slash depending on the terminal you are using. Try for yourself.

docker run -d -p 80:80 -v //var/run/docker/sock:/tmp/docker.sock:ro jwilder/nginx-proxy
  1. Start your app1 or app2 container

You can start more containers with the same command, just change the domain and image name

docker run -d -e VIRTUAL_HOST=myapp1.com myapp1
docker run -d -e VIRTUAL_HOST=myapp2.com myapp2
  1. In your web browser, open http://myapp1.com or http://myapp2.com

Upvotes: 3

Related Questions