Portainer - Easy Container Management

Containers Sep 1, 2021
💡
EDIT: Since the original article, Portainer has launched a dark-mode theme, and also gone through a general redesign. Everything below is still accurate, however some buttons and layouts have changed slightly. If anything really doesn't make sense anymore, let me know in the comments at the bottom of the page.

Portainer is a self-hosted service I was lucky enough to find early on in my docker journey. It is a container which helps you manage your other containers, and frankly, life would have been a lot more difficult without it.

Having said that, maybe you're fine with the Synology Docker GUI, or are a whiz on SSH and it suits your needs. But I love Portainer, and I love spreading the love.

In this article I'll take you through the steps to set it up, and give you a broad overview of the various tools you'll have available to you.

for any command starting with docker, to avoid permissions issues you either need to write sudo before it, or you can follow this guide (scroll to point 6) to add your user to the docker group which means you will no longer need to add sudo for permissions

Installing Portainer

First up, we need to create the container, and we'll use our trusty docker-compose for this:

  • Login to your NAS via SSH and create your portainer network (skip this step if you just plan to use the default bridge network)
docker network create portainer
  • In your file manager (either File Station or SMB via Windows 10 etc.) create your docker-compose.yml file, and copy paste the following:
services:
  portainer: #container management
    image: portainer/portainer-ce
    container_name: portainer
    volumes:
      - $DOCKERDIR/portainer:/data
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - TZ=$TZ
    ports:
      - 9000:9000 #change before the ':' if necessary to avoid port conflicts
    networks: #delete as necessary
       - portainer
    restart: unless-stopped

networks: # delete if you didn't create a portainer network
  portainer:
  	external: true
note that I have some variables as $ information passed from a .env
  • Create a file called .env in the same folder as your docker-compose.yml file and copy paste the following:
TZ=[your timezone e.g. 'Europe/Amsterdam']
DOCKERDIR=[/path/to/your/folder]
change the information inside the [ ] brackets (and remove the brackets!) to match your system config
you do not need to use a .env and can pass that information directly via the docker-compose file, however I have gotten used to using one to keep sensitive data secure
  • Regardless of the method you use to specify a folder for Portainer's /data mapping, make sure that you create that volume before moving on to the next step
  • Back in SSH, navigate to the directory you created your docker-compose.yml file, and run the following command:
docker-compose -p "portainer" up -d
the -p "portainer" part of the command will name your stack portainer
note that if you've named your stack, any further commands relating to this stack must also include the -p "portainer" argument or it will create a new stack

If everything's gone well, you can now enter http://yourMachine'sIP:9000 in your browser address bar and you will be directed to this screen:

it is highly recommended that you do not use admin as your username
  • Create a username (I've called mine 'myuser'), password, confirm the password, then hit 'Create user'
  • At the next screen, make sure you select the 'Docker' button before hitting connect
notice that we've already included the /var/run/docker.sock in our docker-compose file

Portainer Configuration

On the next screen, you should see a dashboard-type view, which gives a broad overview of your current docker container numbers, plus some other stats like storage space used etc.

ctrl+click to enlarge

Select 'local':

you now have more options to choose from

All these options are now available for selection. Going into your 'Containers' section shows you a tabulated list of your containers, including their names, stack names, the images they use, and if set, their (clickable) port numbers for direct access to those services/gui.

You can also use portainer to create containers with docker-compose files. Click into the 'Stacks' tab:

ctrl+click to enlarge

You'll notice that my stacks here show as Limited under 'Control'. This is because they weren't created using Portainer, rather via SSH. To create a stack via Portainer, click the '+ Add stack' button:

note the container_name: diun2 because I already have a diun container

I'm going to recreate my Diun container

ctrl+click to enlarge

Notice that I have variables listed as '$DOCKERDIR' and '$TZ' and '$APPTOKEN' - the compose file will look for this data in a .env file, which Portainer allows you to load from your local machine:

ctrl+click to enlarge

When you're ready, hit 'Deploy the stack'. You should get a green 'stack deployed successfully' notice on the top right corner of the screen, and you'll be redirected back to the 'Stacks' screen, where you should be able to select your new stack:

notice how 'Control' is now total and it shows a user under 'Created'

If you click into it,  you'll have the option to modify, stop, start and delete the stack.

Let's go to the 'Containers' page and find the container:

here's my diun2 container in the 'mynewdiunstack'

Let's click the container name, and see what it shows us:

ctrl+click to enlarge

There's a lot of info here. At the top we have the actions (start, stop, remove etc.). Under the 'Status' parts, we can see the name (and change it if we want) and runtime. And under that, we have some other tools - 'Logs' and 'Console' are particularly useful.

'Console' allows you to ssh or 'bash' into your container should you have a need to make configurations or settings changes in that way

At the bottom of the page, we have the environment variables, the volumes being used, and the network options:

ctrl+click to enlarge

These network options are very handy for network joining/leaving.

There's a lot more you can do here, whether you use Portainer to create your stacks or not. You can prune (remove) unused images and volumes, create and manage networks, and of course have complete control over containers and stacks created via Portainer's GUI.

Enjoy!


Related Article

Preshared Keys and more - SSH on Linux and Synology
An easy guide which explains how to access and use your SSH sessions to their fullest, including clients and logging in with preshared keys
Getting the most out of docker-compose: tips and tricks
A list of handy tips you can implement immediately when creating your docker compose files

PTS

PTS fell down the selfhosted rabbit hole after buying his first NAS in October 2020, only intending to use it as a Plex server. Find him on the Synology discord channel https://discord.gg/vgSq5pcT

Have some feedback or something to add? Comments are welcome!

Please note comments should be respectful, and may be moderated