#!/bin/bashWEBUI_PORT="9000"## Defaults to 'portainer' if emptyCONTAINER_NAME=## Defaults to a named volume, portainer_data.# Note: create this volume with $ docker volume create portainer_dataDATA_DIR=echo""echo"Checking for new image"echo""dockerpullportainer/portainer-ce
echo""echo"Restarting Portainer"echo""dockerstopportainer&&dockerrmportainer
dockerrun-d\-p8000:8000\-p${WEBUI_PORT:-9000}:9000\--name=${CONTAINER_NAME:-portainer}\--restart=unless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\-v${DATA_DIR:-portainer_data}:/data\portainer/portainer-ce
Running Portainer Agent
Start a Portainer in agent mode to allow connection from a Portainer server. This setup is done in the Portainer server's webUI.
Warning
It is probably easier to just download the agent script from the Portainer server when you are adding a connection. It offers a command you can run to simplify setup.
#!/bin/bashecho""echo"Checking for new container image"echo""dockerpullportainer/agent
echo""echo"Restarting Portainer"echo""dockerstopportainer-agent&&dockerrmportainer-agent
dockerrun-d\-p9001:9001\--name=portainer-agent\--restart=unless-stopped\-v/var/run/docker.sock:/var/run/docker.sock\-v/var/lib/docker/volumes:/var/lib/docker/volumes\portainer/agent:latest
My Portainer backup script
Run this script to backup the Portainer portainer_data volume. Backup will be placed at ${CWD}/portainer_data_backup
#!/bin/bash# Name of container containing volume(s) to back upCONTAINER_NAME=${1:-portainer}THIS_DIR=${PWD}BACKUP_DIR=$THIS_DIR"/portainer_data_backup"# Directory to back up in containerCONTAINER_BACKUP_DIR=${2:-/data}# Container image to use as temporary backup mount containerBACKUP_IMAGE=${3:-busybox}BACKUP_METHOD=${4:-tar}DATA_VOLUME_NAME=${5:-portainer-data}if[[!-d$BACKUP_DIR]];thenecho""echo$BACKUP_DIR" does not exist. Creating."echo""mkdir-pv$BACKUP_DIRfifunctionRUN_BACKUP(){sudodockerrun--rm--volumes-from$1-v$BACKUP_DIR:/backup$BACKUP_IMAGE$2/backup/backup.tar$CONTAINER_BACKUP_DIR}functionRESTORE_BACKUP(){echo""echo"The restore function is experimental until this comment is removed."echo""read-p"Do you want to continue? Y/N: "choice
case$choicein[yY]|[YyEeSs])echo""echo"Test print: "echo"sudo docker create -v $CONTAINER_BACKUP_DIR --name $DATA_VOLUME_NAME"2" $BACKUP_IMAGE true"echo""echo"Test print: "echo"sudo docker run --rm --volumes-from $DATA_VOLUME_NAME"2" -v $BACKUP_DIR:/backup $BACKUP_IMAGE tar xvf /backup/backup.tar"echo""echo""echo"Compare to original container: "echo""echo"Test print: "echo"sudo docker run --rm --volumes-from $CONTAINER_NAME -v $BACKUP_DIR:/backup $BACKUP_IMAGE ls /data";;[nN]|[NnOo])echo""echo"Ok, nevermind."echo"";;esac}# Run a temporary container, mount volume to back up, create backup filecase$1in"-b"|"--backup")case$BACKUP_METHODin"tar")echo""echo"Running "$BACKUP_METHOD" backup using image "$BACKUP_IMAGEecho""RUN_BACKUP$CONTAINER_NAME"tar cvf";;esac;;"-r"|"--restore");;esac
Run Portainer with Docker Compose
Note
I do not use this method. I find it easier to run Portainer with a shell script.