Easily deploy containers with Portainer
Posted on: 2023-12-01
For the past few years, containers have become mainstream for a lot of applications. It's far easier to try new software by simply deploying a container than it is to deploy a VM along with all their dependencies. While there are many container daemons and orchestrators, Portainer is a popular option because it offers an easy way to deploy and manage containers. In this post I'll show you how to deploy Portainer on a Debian host, and then some tips on deploying containers.
Prerequisites
The first step to deploy Portainer, which itself comes as a container, is to install Docker:
apt-get update -y
apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update -y
apt-get install docker-ce docker-ce-cli containerd.io -y
docker version
Here we're updating the repositories used by the apt
command, and then installing Docker from that repository. If all went well, you should see the latest Docker version show up on the screen.
Next, let's install Docker Compose:
wget https://github.com/docker/compose/releases/download/v2.1.2/docker-compose-linux-x86_64
mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod a+x /usr/local/bin/docker-compose
docker-compose --version
Here again, the Docker version should be shown on the screen. Note that this downloads the release for X86. If you run an ARM platform, you would need to download a different release file.
Finally, we need to create a volume where Portainer will be storing its data:
docker volume create portainer_data
Deploying Portainer
Once you have the prerequisites, deploying Portainer can be done with a single command:
docker run -d -p 80:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
This automatically pulls the latest version of Portainer, and runs it as a container. It maps two ports, 80 and 9000, and maps two volumes to the host volume we created earlier. You can use the docker ps
command to see the container being run.
One thing to note is that this command exposes Portainer on port 80, which means your connections won't be encrypted. I highly suggest you install an SSL certificate for it and run it on HTTPS. For example, if you use LetsEncrypt, this is how you would run the container instead:
docker run -d -p 443:9443 -p 8000:8000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data -v /etc/letsencrypt/live/your.hostname.com:/certs/live/your.hostname.com:ro -v /etc/letsencrypt/archive/your.hostname.com:/certs/archive/your.hostname.com:ro portainer/portainer-ce:2.21.5 --sslcert /certs/live/your.hostname.com/fullchain.pem --sslkey /certs/live/your.hostname.com/privkey.pem
Here, replace your.hostname.com with the hostname you assigned.
Managing containers
Once Portainer is deployed, you can connect over the web. The default username is admin and the web UI will ask you to create a password. From there, you will see the local environment. One of the good things about Portainer is the ability to manage multiple environments, including deployments like Docker Swarm. Once you click on the environment you want to manage, you will see a number of options on the left.
Before creating a new container, one good tip is to create a new volume for it. Docker has the ability to map volumes either to a specific folder or to a named volume. By default, in Portainer you should use named volumes. For example, if you want to deploy a container called grafana, you can create a new volume of the same name, and then any volume mapping the container requires can be configured to use it. That way, you can easily keep track of volumes tracked by specific containers.
Another tip is that you should use the Stacks option whenever you can. Using the Containers page allows you to specify individual settings like ports mapping, environment variables and volumes in the same way you would typically run the docker
command, while Stacks allows you to specify all the same information as a YAML script, the same way you would use the docker-compose
command. So if the application you want to deploy provides Docker Compose instructions, you want to use Stacks.
Finally, one of the options you can set when deploying containers is the Restart policy. Remember to set this up for any container you want to run at all times, that way they will start automatically whenever your Portainer instance reboots. If you ever run into issues with a specific container, you can easily look at its logs or open a console inside the container through the web UI. Overall, this all makes management of containers much easier and faster.