How to Install Portainer on Ubuntu 20.04 with Docker

Updated on November 21, 2023
How to Install Portainer on Ubuntu 20.04 with Docker header image

Introduction

Portainer is a powerful container management tool for Kubernetes and Docker. This tutorial explains how to install the Portainer with Docker on Ubuntu 20.04.

Prerequisites

Example Names

This guide uses example names.

  • port.example.com : Example domain with the correct DNS record to your instance.
  • youremail@example.com: Example email to receive Let's Encrypt reminders. Replace it with a real email address, or Let's Encrypt will not issue a certificate to you

1. Create a Container

Create a directory for the Portainer to store data, then create a Portainer container.

$ sudo mkdir -p /vol/portainer/data
$ docker run --restart always -d --name=portainer -v /var/run/docker.sock:/var/run/docker.sock -v /vol/portainer/data:/data -e VIRTUAL_HOST=port.example.com -e VIRTUAL_PORT=9000 portainer/portainer-ce -H unix:///var/run/docker.sock
  • -v /var/run/docker.sock:/var/run/docker.sock means mounting /var/run/docker.sock to the container so portainer can control the Docker.
  • -v /vol/portainer/data:/data means storing data of portainer on directory /vol/portainer/data.
  • port.example.com is your domain to access the portainer.

2. Configure Reverse Proxy for Portainer

HTTPS is a necessary security measure to protect your data from network hijacking. Caddy is a light ZeroSSL reverse proxy server. It means that you don't need to consider complex HTTPS configuration, it obtains and automatically maintains SSL certificates. This tutorial uses Caddy for reverse proxy Portainer.

Create a Caddyfile

Caddyfile is a document containing configs for your sites. Create a Caddyfile:

$ sudo mkdir -p /vol/caddy/configs
$ sudo nano /vol/caddy/configs/Caddyfile

Then populate it with the following text:

port.example.com {
    tls youremail@example.com
    reverse_proxy portainer:8000
}
  • port.example.com is your domain with the correct DNS record to your instance.
  • tls youremail@example.com means apply free certificates and use this email to receive Let's Encrypt reminders.
  • reverse_proxy portainer:8000 means forward request to 8000 port on container portainer.

Press :KEY_CTRL: + :KEY_X: to save the file.

Create a Caddy Container

After compose the Caddyfile, create a container with the following command:

$ docker run --restart always -d -p 80:80 -p 443:443 -v "/vol/caddy/data:/data/caddy" -v "/vol/caddy/configs:/etc/caddy" --link portainer --name caddy caddy
  • -p 80:80 -p 443:443 means publish its 80 and 443 port to your host so you can access it with those ports.
  • -v "/vol/caddy/data:/data/caddy" means mount caddy working directory to your host to persist data such as certificates.
  • -v "/vol/caddy/configs:/etc/caddy" means mount caddy configuration directory to your host to persist configurations.
  • --link portainer means link container caddy with portainer so they can access with each other.

3. Install Portainer

Visit your domain in a web browser and set a password to finish the installment.

More Resources