Manage containers with Portainer

Maciej
5 min readFeb 8, 2021

What exactly is Portainer ??

It is a tool that can manage Docker containers and images with GUI. With a stylish UI and simple operability, it is a tool that can be recommended for a wide range of people from beginners to advanced users.

Basic usage

Ok so let’s use Portainer first. Since Portainer it self is provided as a Docker image, all you have to do is start the container as shown below. For this we will use example environment running on vagrant.

root@vagrant:/home/vagrant# docker run \
> --detach \
> --publish 9000:9000 \
> --publish 8000:8000 \
> --name portainer \
> --restart always \
> --volume /var/run/docker.sock:/var/run/docker.sock \
> --volume portainer_data:/data \
> portainer/portainer-ce
Unable to find image 'portainer/portainer-ce:latest' locally
latest: Pulling from portainer/portainer-ce
b890dbc4eb27: Pull complete
81378af8dad0: Pull complete
Digest: sha256:21713e42233ee953b4cd4e6e8b1e4b6c43ebe2ca1c2dc762824a1866fdb91d3e
Status: Downloaded newer image for portainer/portainer-ce:latest
ba35bed0ae7baed1ee1f92b2ada2735f111455bf137d31ba7e2d612e641efb36
root@vagrant:/home/vagrant#

We create docker volume for like persisting registered information (logged-in user information and etc.

When you open http://IP-ADDRESS:9000 you will be prompted to set a password as follows. Set an appropriate value.

Then select the environment you want to manage. Select Docker and press Connect. If there is no problem, the endpoint that will be the Home screen will be displayed.

Open local docker and then click to the Containers arrow Images, etc. link is displayed. Once Containers you check, you can see how the Portainer container is working properly as shown below.

When you add a container, it will automatically be managed by Portainer.

For test let’s launch nginx:

root@vagrant:/home/vagrant# docker run -dit -p 8085:80 --name nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
512572da2072c05fbb4fc1b5d1b2408696b82b74deed3444c766fdce6efc6b37

If there are no particular errors, you can see that nginx is in portainer.

For each container, you can check the log with logs, check the container information with inspect, and check the resource usage etc. with stats. Also, when the container is started, option is set, you can operate inside the container with console.

Let’s access nginx that you started earlier with console by click button Exec Console

Now we know how to manage a single host container with Portainer, however, in reality, the Docker host may span multiple nodes. Once you have confirmed the basic usage, delete Portainer once.

root@vagrant:/home/vagrant# docker stop portainer && docker rm portainer
portainer
portainer

Manage the containers from Docker Swarm cluster

First we need to prepare docker swarm cluster, for this we can use this vagrant file:

  • Creating Swarm manager
root@master:/home/vagrant# docker swarm init --advertise-addr 192.168.123.123
Swarm initialized: current node (we5z19hghwf44wc3zwks0ebrw) is now a manager.
To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-12zm5fjspt6g2tov9pkigwyvr16nwrj8ko9ttm2nb5vfchfzas-960mns9unlgwgyz1b49vhu7fp 192.168.123.123:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.root@master:/home/vagrant#
  • Issue tokens for workers to participate
vagrant@node1:~$ sudo su
root@node1:/home/vagrant# docker swarm join --token SWMTKN-1-12zm5fjspt6g2tov9pkigwyvr16nwrj8ko9ttm2nb5vfchfzas-960mns9unlgwgyz1b49vhu7fp 192.168.123.123:2377
This node joined a swarm as a worker.
root@node1:/home/vagrant#
vagrant@node2:~$ sudo su
root@node2:/home/vagrant# docker swarm join --token SWMTKN-1-12zm5fjspt6g2tov9pkigwyvr16nwrj8ko9ttm2nb5vfchfzas-960mns9unlgwgyz1b49vhu7fp 192.168.123.123:2377
This node joined a swarm as a worker.
root@node2:/home/vagrant#
  • Confirmation of settings If Manager Swarm is set normally, three hosts will be displayed in the node list.
root@master:/home/vagrant# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
we5z19hghwf44wc3zwks0ebrw * master Ready Active Leader 20.10.2
sb3794hrm0jv08xc9r1huku3x node1 Ready Active 20.10.2
nli6rg86pkd77cwtnlbb93tvz node2 Ready Active 20.10.2
root@master:/home/vagrant#

Cluster is now complete.

Now we can deploy portainer stack. Stack is a set of Services, roughly speaking, Docker Swarm is a technology for bundling servers, and Docker Stack is a technology for bundling applications.

Login on master node and run command as follow:

root@master:/home/vagrant# curl -L https://downloads.portainer.io/portainer-agent-stack.yml -o portainer-agent-stack.yml
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 757 100 757 0 0 351 0 0:00:02 0:00:02 --:--:-- 351
root@master:/home/vagrant# docker stack deploy --compose-file=portainer-agent-stack.yml portainer_example
Creating network portainer_example_agent_network
Creating service portainer_example_agent
Creating service portainer_example_portainer
root@master:/home/vagrant#

Now verify if everything works properly:

root@master:/home/vagrant# docker stack ls
NAME SERVICES ORCHESTRATOR
portainer_example 2 Swarm

root@master:/home/vagrant# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
tk7qnpcw6ub1 portainer_example_agent global 3/3 portainer/agent:latest
r5apksf1iw3o portainer_example_portainer replicated 1/1 portainer/portainer-ce:latest *:8000->8000/tcp, *:9000->9000/tcp

root@master:/home/vagrant#

When you access Portainer after the deployment is complete, you should see four containers as shown below. From the Host column, you can see that containers on three different hosts can be managed by one Portainer.

Conclusion

Portainer is simple and sophisticated, and can do many things. I think this is a good tool that will certainly facilitate the work with docker swarm. In version 2.0 we also have support with Kubernetes

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.