Install Portainer on Ubuntu 20.04 with Docker
Tags: ubuntu, docker, portainer, kubernetes
Published: 2021-04-09 12:42:40

In this article, we'll explain how to install Portainer on Ubuntu 20.04 with Docker.

Portainer is powerful, open-source toolset that allows you to easily build and manage containers in Docker, Swarm, Kubernetes and Azure ACI. It works by hiding the complexity that makes managing containers hard, behind an easy to use GUI.

####Prerequisites
<ul>
<li>Ubuntu 20.04 installed <a href="https://www.hostnextra.com/dedicated-server.html">dedicated server</a> or <a href="https://www.hostnextra.com/vps-hosting.html">KVM VPS</a>.</li>
<li>A root user access or normal user with administrative privileges.</li>
<li>Add A record of your preferred domain like port.example.com</li>
</ul>

<a href="https://www.hostnextra.com/kb/how-to-install-portainer-on-ubuntu/">Install Portainer on Ubuntu 20.04 with Docker</a>

####1. Keep the server up to date
```
# dnf update -y
```
####2. Install Docker

Install the required dependencies for Docker:
```
# apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
```
Add the Docker CPG Key
```
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
```
Add the Docker Repository
```
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
```
The following command will download Docker and install it:
```
# apt-get update -y
# apt-get install docker-ce -y
```
Start and enable Docker service
```
# systemctl start docker && systemctl enable docker
```

####3. Create a container

We'll show you two ways to deploy the container.

1. If you want to use domain name to access Portainer, use following command to deploy the container:
````
# 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
```
<ul>
<li>-v /var/run/docker.sock:/var/run/docker.sock means mounting /var/run/docker.sock to the container so portainer can control the Docker.</li>
<li>-v /vol/portainer/data:/data means storing data of portainer on directory /vol/portainer/data.</li>
<li>port.example.com is your domain to access the portainer.</li>
</ul>

2. If you want to access Portainer using server IP, use following command to deploy the container:
```
# docker volume create portainer_data

# docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
```

####4. Configure Reverse Proxy for Portainer (Optional if you will use domain name)

Caddyfile is a reverse proxy server. It is necessary to secure the connection to prevent network hijacking. Caddyfile can obtains and automatically maintains SSL certificate.

Create a Caddyfile. Caddyfile is a document containing configs for your sites:
```
# mkdir -p /vol/caddy/configs
# vi /vol/caddy/configs/Caddyfile
```
Add following content:
```
port.example.com {
tls youremail@example.com
reverse_proxy portainer:8000
}
```
Replace: port.example.com with your domain name and youremail@example.com with your actual email id.

Save and exit.

Finally, create a Caddy container using 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
```
<ul>
<li>-p 80:80 -p 443:443 means publish its 80 and 443 port to your host so you can access it with those ports.</li>
<li>-v "/vol/caddy/data:/data/caddy" means mount caddy working directory to your host to persist data such as certificates.</li>
<li>-v "/vol/caddy/configs:/etc/caddy" means mount caddy configuration directory to your host to persist configurations.</li>
<li>--link portainer means link container caddy with portainer so they can access with each other.</li>
</ul>

####5. Access Portainer

Navigate to your browser and access the Portainer by using either your domain or server IP and set admin password and finish the installment.

That's it. The installation has been completed successfully.

In this article, we've have seen how to install Portainer on Ubuntu 20.04 with Docker.