Search code examples
ubuntudockernginxproxypass

Nginx container acting as a proxy


I recently started learning Docker and I found out a tool called Portainer to manage Docker containers and images. I made it run as a container on a remote server but it uses port 9000 which I'd like to change to 80 so I thought about using a proxy. I decided to go with Nginx (which I have never used before) as a container. I start Nginx with following instruction:

docker run --name mynginx2 -v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -P -d nginx

and the /var/nginx/conf/nginx.conf file looks like this:

events {
    worker_connections 1024;
}
http {
    server {
        location / {
            proxy_pass http://localhost:9000;
        }
    }
}

worker_connections had to be inserted because of errors during container startup. When I go to the ip of my remote server (say: http://ip.of.my.server/), I expect that Portainer will show up but nothing happens. I don't even get a status code of the response.

What am I missing here?

Kind regards, Daniel


Solution

  • Run your portainer container like this:

    docker run -it --name myportainer -v "/var/run/docker.sock:/var/run/docker.sock" -d portainer/portainer
    

    Then run nginx like this:

    docker run --name mynginx2 -v /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -p 80:80 -P -d --link myportainer:myportainer nginx
    

    In your nginx config use this:

    events {
        worker_connections 1024;
    }
    http {
        server {
            listen 80;
    
            location / {
                proxy_pass http://myportainer:9000;
            }
        }
    }
    

    Also the commands above should do the trick, in the long term it will be easier to use docker-compose:

    This is how your docker-compose.yml should look like:

    version: "2"
    
    services: 
    
      proxy:
          image: nginx:latest
          container_name: proxy
          ports:
           - "80:80"
          volumes:
           - /var/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
    
      portainer:
          image: portainer/portainer
          container_name: myportainer
    

    Then you just run docker-compose up -d