1

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"
Benjamin Seche
  • 113
  • 1
  • 7

1 Answers1

0

What I want is to access my container1 by ip_addrress_host/container1.

Are you sure you want to access container with e.g. http://127.0.0.1/container1? Then jwilder/nginx-proxy is not the best approach.

Your proxy is listening locally on port 80, it will proxy request to container1 and container2 but it won't proxy paths /container1 and /container2.

Curl

curl -H "Host: container1" localhost

Browser

Open up http://container1


You can attach a custom config that will take care of proxying paths to make ip_address/container-name work:

  1. Add this to volumes: ./custom.conf:/etc/nginx/conf.d/custom.conf
  2. Create custom.conf next to docker-compose.yml:

    server {
        server_name 192.168.X.Y;
        listen 80;
    
        location ~ ^/([^/]+)(/.*)? {
            proxy_pass http://$1$2;
        }
    }
    
  3. Link containers

    nginx-proxy:
      ...
      links:
        - site_a:container1
        - site_b:container2
    

This is just for start, you should improve this to match your needs. It should work with http://192.168.X.Y/container1 or http://192.168.X.Y/container2.

Please note that there is a rewrite, so http://192.168.X.Y/container1/some/path is proxied to http://container1/some/path. I've assumed that you don't want requests with /container1 prefix to your destination conatiner.

  • No, I want to access container with 192.168.XXX.XXX/container1 with 192.168.XXX.XXX the ip address of my host – Benjamin Seche Oct 26 '17 at 08:09
  • Then do not use `jwilder/nginx-proxy`, it does not have such feature, at least I can't see it in docs. – Paweł Tatarczuk Oct 26 '17 at 08:12
  • OK, I certainly misunderstood the purpose of this one! – Benjamin Seche Oct 26 '17 at 08:30
  • I've edited my answer to show you how you can force `jwilder/nginx-proxy` to work with your scenario, this is not the cleanest solution though. I'd suggest to build custom `nginx-proxy` image instead. – Paweł Tatarczuk Oct 26 '17 at 08:31
  • I edited my question with the log I have when I try to reach 192.168.12.28/container1. (The response is a 404 not found) – Benjamin Seche Oct 26 '17 at 08:53
  • Have you changed `192.168.X.Y` to `192.168.12.28`? I've also edited the answer and added `?` to rewrite, it did not match url without trailing backslash. – Paweł Tatarczuk Oct 26 '17 at 08:56
  • Yes, I changed the ip address. I also add the ? to rewrite. Now, I have a 502 bad Gateway. I added the log to my question – Benjamin Seche Oct 26 '17 at 09:13
  • Weird, it worked for me. I've added instruction to link containers, it should help in case `nginx-proxy` can't resolve `container1`. Does it help? – Paweł Tatarczuk Oct 26 '17 at 09:20
  • Yes! it works! I just have to put a "/" after the url (e.g. 192.168.12.28/container1/") – Benjamin Seche Oct 26 '17 at 09:39
  • Oh, I guess I know why. You have different container names and host names: `site_a != container1`. Probably changing container names to `container1` and `container2` should suffice. The trailing "/" should not be needed after adding missing "?". Did you restart container after change? – Paweł Tatarczuk Oct 26 '17 at 09:39
  • Problem fixed. All work perfectly. The allias in the links option make it works whith url like "http://192.168.12.28/container1". Thank you! – Benjamin Seche Oct 26 '17 at 09:53