Skip to main content

Portainer

{{@9#bkmrk-callout-danger-NoResponsibilityDisclaimer-5wod5ufe}}


Introduction

Portainer's hybrid & multi-cloud container management platform supports Kubernetes, Docker, Swarm in any Data Center, Cloud, Network Edge or IIoT Device.


Requirements

The following requirement(s) assumes that the container/app will not be internet/public facing:


Deployment

Via CLI

Docker CLI

  1. Create the volume for the Portainer database:
    sudo docker volume create portainer_data
  2. Install Portainer:
    sudo docker run -e "America/Chicago" -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

Via Docker Compose (Preferred)

  1. Create the volume for the Portainer database:
    sudo docker volume create portainer_data

    This will create the external portainer_data volume that is later referenced in the docker-compose.yml file.


  2. Using the non-root user account, create a ~/portainer/docker-compose.yml file:
    sudo mkdir -p ~/portainer && cd ~/portainer && sudo vim ~/portainer/docker-compose.yml
  3. Enter the following content in the docker-compose.yml file:
    ---
    
    services:
      portainer:
        image: portainer/portainer-ce:lts
        container_name: portainer
        restart: always
        ports:
          - 8000:8000
          - 9443:9443
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - portainer_data:/data
    
    volumes:
      portainer_data:
        external: true
  4. Start the container:

    sudo docker compose up -d

Post Deployment

Login

  1. Visit the Portainer URL to create the initial administrator user:
    URL https://<ip>:9443 or https://<hostname>:9443

  2. Change user information as needed, such as your User/Email and Password.

Update via CLI

If Portainer was set up using the CLI, use this update method.

  1. Stop the Portainer container.
    sudo docker stop portainer
  2. Remove the Portainer container.
    sudo docker rm portainer
  3. Get the Portainer image id:
    sudo docker image ls | grep portainer
  4. Remove the Portainer image using the image id from the previous step:
    sudo docker image rm xxxxxxxxxxxx

    OR

    sudo docker rmi -f $(sudo docker images --format '{{.Repository}}:{{.Tag}}' | grep 'portainer')
  5. Run the following command to create a new container from pulled image:
    sudo docker run -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

Update via Docker Compose

Manual Update

If Portainer was set up using docker compose, use this update method.

  1. Navigate to ~/portainer:
    cd ~/portainer
  2. Stop the container:
    sudo docker compose stop
  3. Pull the new image:
    sudo docker compose pull
  4. Start the container:
    sudo docker compose up -d --force-recreate
  5. Prune unused images:
    sudo docker image prune -f

Scripted Update

  1. Create a new script file in a designated location, such as the user's home directory:
    nano ~/update_portainer.sh

    Paste the following contents:

    #!/bin/bash
    
    # Define variables
    TARGET_DIR="$HOME/portainer"
    SEARCH_STRING="portainer/portainer-ce:lts"
    
    # Validate directory existence
    if [ ! -d "$TARGET_DIR" ]; then
        echo "Error: Target directory '$TARGET_DIR' does not exist."
        exit 1
    fi
    
    # Dynamically locate the compose file (.yaml or .yml)
    if [ -f "$TARGET_DIR/docker-compose.yaml" ]; then
        COMPOSE_FILE="$TARGET_DIR/docker-compose.yaml"
    elif [ -f "$TARGET_DIR/docker-compose.yml" ]; then
        COMPOSE_FILE="$TARGET_DIR/docker-compose.yml"
    else
        echo "Error: No docker-compose file (.yml or .yaml) found in '$TARGET_DIR'."
        exit 1
    fi
    
    # Check for the required image string within the compose file
    if grep -q "$SEARCH_STRING" "$COMPOSE_FILE"; then
        echo "Verification passed: '$SEARCH_STRING' found in '$COMPOSE_FILE'."
        echo "Initiating update process..."
    
        # 1. Navigate to the target directory
        cd "$TARGET_DIR" || { echo "Failed to change directory to '$TARGET_DIR'."; exit 1; }
    
        # 2. Stop the container
        echo "Stopping existing containers..."
        sudo docker compose stop
    
        # 3. Pull the new image
        echo "Pulling the latest image..."
        sudo docker compose pull
    
        # 4. Start the container with forced recreation
        echo "Starting container and applying updates..."
        sudo docker compose up -d --force-recreate
    
        # 5. Prune unused images
        echo "Cleaning up dangling images..."
        sudo docker image prune -f
    
        echo "Update completed successfully."
    else
        # Abort execution if the target string is absent
        echo "Verification failed: Target image '$SEARCH_STRING' not found in '$COMPOSE_FILE'."
        echo "Aborting update."
        exit 0
    fi
  2. Make the script executable to allow the system to run it:
    chmod +x ~/update_portainer.sh
  3. Run the script to verify the conditional logic and perform the update if the conditions are met:
    ~/update_portainer.sh

Royal TS Key Sequence Task (Upgrade)

{{@9#bkmrk-callout-warning-UniqueToMarthursHomelab-uy8gtmxc}}

Royal TS provides easy and secure access to remote systems. It allows the use of "Key Sequence Tasks" to automate jobs. I've included the follow key sequence tasks to automate the upgrade process.

sudo docker stop portainer{ENTER}{WAIT:3000}
sudo docker rm portainer{ENTER}{WAIT:3000}
sudo docker rmi -f $(sudo docker images --format '{{.Repository}}:{{.Tag}}' | grep 'portainer')
sudo docker run -d -p 8000:8000 -p 9443:9443 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

Sources


KB Change/Issue Log

yyyy/mm/dd - Title

Issue

N/A

Solution

N/A


KB Meta

Page Includes @9#bkmrk-callout-danger-NoResponsibilityDisclaimer-5wod5ufe