I want to be able to have access to multiple containers, on the same host, containing web applications.
When I want to access the host (by IP address) or the containers (e.g. by host_ip_adress/container1), I get for both a 503 error from nginx
. What I want is to access my container1 by ip_addrress_host/container1.
The solution I found on internet was to set an nginx-proxy
front-end server (source: https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/)
My docker-compose file : docker-compose.yml
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock
site_a:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=container1
volumes:
- ./php:/var/www/html
site_b:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=container2
volumes:
- ./php:/var/www/html
I run it with the command :
docker-compose up
My entries in the /etc/hosts file :
127.0.1.1 container1
127.0.0.1 container2
The logs I see when I make a request from the outside :
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /container1 HTTP/1.1" 503 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://192.168.12.28/container1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
Thank you for your help, and sorry for my bad English! :-)
Edit :
Here are my logs for the nginx-proxy container at the start :
forego | starting dockergen.1 on port 5000
forego | starting nginx.1 on port 5100
dockergen.1 | 2017/10/25 14:01:53 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2017/10/25 14:01:53 Running 'nginx -s reload'
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container1" has suspicious symbols in /etc/nginx/conf.d/default.conf:60
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container2" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
dockergen.1 | 2017/10/25 14:01:54 Watching docker events
dockergen.1 | 2017/10/25 14:01:54 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
Edit2 : I tried to "custumize" nginx-proxy with the configuration file give by Paweł Tatarczuk (https://serverfault.com/a/880384/441157)
Now, when I do a request llike http://192.168.12.28/container1 I have got this log :
nginx-proxy_1 | nginx.1 | 2017/10/26 08:46:19 [error] 41#41: *1 open() "/etc/nginx/html/container1" failed (2: No such file or directory), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"
Edit 3 : Add the ? to the rewrite
nginx-proxy_1 | nginx.1 | 2017/10/26 09:11:00 [error] 31#31: *1 container1 could not be resolved (2: Server failure), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"