Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple docker containers accessible by nginx reverse proxy

I'd like to run multiple docker containers on one host VM which would be accessible through only one domain. I wanted to use request url to differentiate between containers. To achieve this I'm trying to set nginx server as reverse proxy and run it in the container also listening on port 80.

Let's say I have two containers running on port 3000 and 4000. The routing would be following:

docker-host.example.com/3000 -> this will access container exposing port 3000
docker-host.example.com/4000 -> this will access container exposing port 4000

The thing is I'm currently stack even with trying to define static rule for such reverse proxy. It works fine without any location:

upstream application {
    server <docker container>:3000;
}

server {
        listen 80;

        location / {
            proxy_pass_header  Server;
            proxy_set_header   Host $http_host;
            proxy_redirect     off;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Scheme $scheme;
            proxy_pass         http://application/;
        }
}

But when I add port location and try to access it using localhost:{nginx port}/3000/

upstream application {
    server <docker container>:3000;
}

server {
        listen 80;

        location /3000/ {
            proxy_pass_header  Server;
            proxy_set_header   Host $http_host;
            proxy_redirect     off;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Scheme $scheme;
            proxy_pass         http://application/3000/;
        }
}

It seems that first resource (main html) is requested correctly, but any other depending resource (for example js or css needed for this site) is missing. If I examine request for those resources I have in logs:

09:19:20 [error] 5#5: *1 open() "/etc/nginx/html/public/css/fonts.min.css" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /public/css/fonts.min.css HTTP/1.1", host: "localhost:8455", referrer:"http://localhost:8455/3000/"

So request url is http://localhost:8455/public/css/fonts.min.css

Instead of http://localhost:8455/3000/public/css/fonts.min.css

Could I ask you for any suggestions ? Is this scenario possible ?

like image 266
Tomasz Gawlik Avatar asked Apr 27 '17 09:04

Tomasz Gawlik


People also ask

Can 2 containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Can I run multiple Docker containers on same port?

So there is no conflict if multiple containers are using the same port ( :80 in this case). You can access one container from another using its container-name or service-name or ip-address, whereas ip-address is not a good idea because this might change every time you (re)start the container.

Can a server run multiple Docker containers?

You can connect multiple containers using user-defined networks and shared volumes. The container's main process is responsible for managing all processes that it starts. In some cases, the main process isn't well-designed, and doesn't handle “reaping” (stopping) child processes gracefully when the container exits.


1 Answers

You can select a docker container per port, your example:

  • http://example.com:4000/css/fonts.min.css
  • http://example.com:3000/css/fonts.min.css

But there is another approach that I like more, because I think it is clearer, access to a docker container by domain name, e.g:

  • http://a.example.com/css/fonts.min.css
  • http://b.example.com/css/fonts.min.css

Whichever you choose, there is a project in github that helps you to implement docker multi-container reverse proxy: https://github.com/jwilder/nginx-proxy

I've written an example using docker-compose for a similar scenario at: http://carlosvin.github.io/posts/reverse-proxy-multidomain-docker/

like image 177
carlosvin Avatar answered Nov 15 '22 05:11

carlosvin