What is Portainer?

Portainer is a web-based GUI that allows you to manage, configure, and monitor a Docker environment. This simplifies the Docker workflow so that the administrator does not need to complete every task within the CLI.

Installation

Create a Portainer directory:

Terminal
mkdir -p ~/docker/portainer

Move to the directory:

Terminal
cd ~/docker/portainer

Create a volume to permanently store Portainer data:

Terminal
docker volume create portainer_data

Create the Docker compose file:

Terminal
nano docker-compose.yml

Add the below:

services:
    portainer:
        image: portainer/portainer-ce:latest
        container_name: portainer
        restart: unless-stopped

    security_opt:
        - no-new-privileges:true

    ports:
        - 9443:9443

    volumes:
        - /var/run/docker.sock:/var/run/docker.sock
        - portainer_data:/data

volumes:
    portainer_data:

Press CTRL+O to save the file, then press CTRL+X to close.

Start Portainer:

Terminal
docker compose up -d

Confirm the container is running:

Terminal
docker ps

Access the Web Interface in a browser:

https://server-ip:9443

You will be prompted to create the administrator user. Do not use a username such as Admin or Administrator.

Select Get Started.

You can now select your environment.

Confirm that the Docker environment starts on boot:

Terminal
sudo reboot

After the reboot completes:

Terminal
docker ps

Hardening

There are some small changes you want to make in order to set a strong baseline security configuration. Move to your Portainer directory:

Terminal
cd ~/docker/portainer

Restrict access to your local subnet(s). Substitute your local subnet in the command below:

Terminal
sudo ufw allow from 10.0.0.0/24 to any port 9000

Create an update automation script:

Terminal
sudo nano /usr/local/bin/portainer-updates.sh

Add the below text, replacing user-name with the appropriate value:

#!/bin/bash
 
cd /home/user-name/docker/portainer || exit 1
 
docker compose pull
docker compose up -d
docker image prune -f

Press CTRL+O to save the file, then press CTRL+X to close.

Make the script executable:

Terminal
sudo chmod +x /usr/local/bin/portainer-updates.sh

Test the script:

Terminal
sudo /usr/local/bin/portainer-updates.sh

Schedule the script to run daily by adding a cron job:

Terminal
sudo crontab -e

Add the following lines:

# Docker Portainer Updates
30 3 * * * /usr/local/bin/portainer-updates.sh >> /var/log/portainer-updates.log 2>&1

The update log can be checked by running the command:

Terminal
sudo cat /var/log/update-portainer.log

Generate an SSL certificate for HTTPS access.

Create a certificate directory:

Terminal
mkdir certs

Move into the certificate directory:

Terminal
cd certs

Generate a self-signed certificate:

Terminal
openssl req -x509 -nodes -days 3650 \
-newkey rsa:4096 \
-keyout portainer.key \
-out portainer.crt

Follow and complete the prompts.

Move back to the Portainer directory:

Terminal
cd ..

Open and edit the compose file:

Terminal
nano docker-compose.yml

Between ports and volumes, add:

 command:
        - --sslcert
        - /certs/portainer.crt
        - --sslkey
        - /certs/portainer.key

Under the first volumes line, add:

     - ./certs:/certs

Press CTRL+O to save the file, then press CTRL+X to close.

Stop Portainer:

Terminal
docker compose down

Restart Portainer:

Terminal
docker compose up -d

Verify the certificate:

Terminal
openssl x509 -in certs/portainer.crt -text -noout

Log in to the Portainer Web UI.

On the left, select settings.

Then select General.


Locate SSL certificate.
Enable Force HTTPS only.
Click on Save Settings.

Select Authentication.


Increase the minimum character count in Password Rules to enforce strong passwords.


Click on Save settings.

Ensure you are creating non-admin users for anyone who needs to manage containers.



Loading...