Portainer on a Path

On my last post, I got Docker and Portainer installed. I also installed NgInx proxy to be a single point of access and organize the different containers instead of using a bunch of different ports. I wanted to put portainer on a custom location, but I learned it is better to use a subdomain than a custom location path. It works now, so here’s what I did.

To reconfigure, I had to stop and remove Portainer.

sudo docker stop portainer
sudo docker rm portainer

Then, I re-ran the docker run command. The important part was the –base-url option. That option had to go at the end after the image id.

sudo docker run -d -p 8000:8000 \
              -p 9443:9443 \
              --name portainer \
              --restart=always \
              -v /var/run/docker.sock:/var/run/docker.sock \
              -v portainer_data:/data \
              portainer/portainer-ce:sts \
              --base-url /portainer 

At first, it didn’t work. I had to restart the portainer container. Even after the restart, I didn’t see the base url change when I visited port 9443. But, it must have worked because it was fine after I added the custom location.

Then, in the Nginx Proxy Manager, I added the custom location. I had to make sure that I added the trailing slash in “/portainer/”. I also had to click the gear button to add the custom configuration:

This is the config that I found that worked:

    # Main location block for Portainer UI
    location /portainer/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://<PORTAINER_IP>:9000/; # Replace with Portainer container IP/hostname and port
        proxy_http_version 1.1;
        # The following headers are critical for WebSockets
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

I needed to restart nginx (webfront is my nginx container’s name):

sudo docker restart webfront

Leave a Comment

Your email address will not be published. Required fields are marked *