Deploying Portainer behind nginx reverse proxy

Deploying in a Docker Standalone scenario

To deploy Portainer behind an nginx proxy in a Docker standalone scenario you must use a Docker Compose file. In the following docker-compose.yml you will find the configuration of the nginx proxy and the Portainer Server.
This example uses the excellent jwilder/nginx-proxy image as the proxy container, which requires no additional configuration beyond the two environment variables added to the portainer container's definition.
1
version: "2"
2
3
services:
4
nginx-proxy:
5
image: jwilder/nginx-proxy
6
restart: always
7
ports:
8
- "80:80"
9
volumes:
10
- "/var/run/docker.sock:/tmp/docker.sock:ro"
11
12
portainer:
13
image: portainer/portainer-ce:latest
14
command: -H unix:///var/run/docker.sock
15
restart: always
16
environment:
17
- VIRTUAL_HOST=portainer.yourdomain.com
18
- VIRTUAL_PORT=9000
19
ports:
20
- 8000:8000
21
volumes:
22
- /var/run/docker.sock:/var/run/docker.sock
23
- portainer_data:/data
24
25
volumes:
26
portainer_data:
Copied!
To start working with this recipe, change the VIRTUAL_HOST value then deploy Portainer by running the following:
1
docker-compose up -d
Copied!
When this has finished, run docker ps . You should see an output similar to this:
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2
8c8f2eac7c9a portainer/portainer:latest "/portainer -H unix:…" 4 minutes ago Up 4 minutes 9000/tcp, 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp, 9443/tcp portainer_portainer_1
3
3e7c8b5d71d7 jwilder/nginx-proxy "/app/docker-entrypo…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp portainer_nginx-proxy_1
Copied!
Once the deployment has finished you can browse portainer.yourdomain.com.

Deploying in a Docker Swarm scenario

Deploying Portainer in Docker Swarm behind nginx has similar steps to the Docker Standalone scenario. Before deploying, you need to create two elements: networks and volumes.
This deployment assumes you are running one manager node. If you are using multiple managers we advise reading this FAQ entry before proceeding.
First, create two networks:
    One for the agent and the communication with the Portainer Server.
    One to 'expose' the Portainer container to the same network as the reverse proxy.
1
docker network create -d overlay proxy
Copied!
1
docker network create -d agent_network
Copied!
Next, create the volume:
1
docker volume create portainer_data
Copied!
And finally, save the following recipe as portainer.yml:
1
version: '3.2'
2
3
services:
4
nginx-proxy:
5
image: jwilder/nginx-proxy
6
networks:
7
- proxy
8
ports:
9
- "80:80"
10
volumes:
11
- "/var/run/docker.sock:/tmp/docker.sock:ro"
12
- "./vhost.d:/etc/nginx/vhost.d:ro"
13
14
agent:
15
image: portainer/agent:latest
16
environment:
17
# REQUIRED: Should be equal to the service name prefixed by "tasks." when
18
# deployed inside an overlay network
19
AGENT_CLUSTER_ADDR: tasks.agent
20
# AGENT_PORT: 9001
21
# LOG_LEVEL: debug
22
volumes:
23
- /var/run/docker.sock:/var/run/docker.sock
24
- /var/lib/docker/volumes:/var/lib/docker/volumes
25
networks:
26
- agent_network
27
deploy:
28
mode: global
29
placement:
30
constraints: [node.platform.os == linux]
31
32
portainer:
33
image: portainer/portainer-ce:latest
34
command: -H tcp://tasks.agent:9001 --tlsskipverify
35
volumes:
36
- data:/data
37
environment:
38
- VIRTUAL_HOST=portainer.yourdomain.com
39
- VIRTUAL_PORT=9000
40
ports:
41
- 8000:8000
42
networks:
43
- proxy
44
- agent_network
45
deploy:
46
mode: replicated
47
replicas: 1
48
placement:
49
constraints: [node.role == manager]
50
51
52
networks:
53
proxy:
54
external: true
55
agent_network:
56
external: true
57
58
volumes:
59
data:
Copied!
To start working with this recipe, change the VIRTUAL_HOST value then deploy Portainer by running the following:
1
docker stack deploy portainer -c portainer.yml
Copied!
To check the deployment, run docker service ls. You should see an output similar to the following:
1
ID NAME MODE REPLICAS IMAGE PORTS
2
gy2bjxid0g4p portainer_agent global 1/1 portainer/agent:latest
3
jwvjp5bux4sz portainer_nginx-proxy replicated 1/1 jwilder/nginx-proxy:latest *:80->80/tcp
4
5nflcvoxl3c7 portainer_portainer replicated 1/1 portainer/portainer-ce:latest *:8000->8000/tcp
Copied!
Once the services are running, you will be able to access Portainer from the URL you defined earlier, for example: portainer.yourdomain.com.
Last modified 26d ago