Stop containers in Synology Docker
Hi there, if I use nautical backup with a synology NAS, every time that the containers are stopped to make the backup I receive the following warning: Docker container stopped unexpectedly. After some research I have found that docker on Synology expect the containers to be stopped in a specific way: https://www.reddit.com/r/synology/comments/10wkxuc/comment/j7nyxos/ To stop a container:
synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="$container" To start a container:
synowebapi --exec api=SYNO.Docker.Container method="start" version=1 name="$container"
Can something like this be applied to nautical?
Thanks!
Hi,
I think this feature might be available. Check out this section of the docs https://minituff.github.io/nautical-backup/arguments/#execute-commands-before-or-after-backup.
There are environment variables that you can use to fill the $container portion of the command.
Hi there, I have tried to execute a cript before backing up but it can't find the file, can you help me with that?
EDIT: I have found that the problem is with the script itself, how can I add those commands to the script? My stop_containers.sh file is like this:
#!/usr/bin/env bash
# Retrieve the names of all running Docker containers with a specific format
docker_containers=$(sudo docker ps --format '{{.Names}}')
# Loop through each container
for container in $docker_containers
do
echo -n "Stopping $container... "
# Try to stop the Docker container with the Synoweb API.
if ! sudo synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="$container" > /dev/null 2>&1; then
# Error handling
echo "Error stopping container $container." >&2
# exit 1 # Uncomment to stop the script in case of error
else
# Confirmation of stopping
echo -e "\rStopping $container... OK"
fi
done
My docker compose:
services:
nautical-backup:
image: minituff/nautical-backup:latest
container_name: nautical-backup
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /volume1/docker/appdata/nautical/config:/config
- /volume1/docker/appdata:/app/source
- /volume1/backups/docker:/app/destination
environment: # Optional variables
- TZ=Europe/Lisbon
- PUID=1026
- PGID=100
- UMASK=002
- CRON_SCHEDULE=55 10 * * *
- LOG_LEVEL=DEBUG
# - SKIP_CONTAINERS=
- OVERRIDE_SOURCE_DIR=
- OVERRIDE_DEST_DIR=
- PRE_BACKUP_EXEC=/config/stop_containers.sh
The log:
DEBUG: REPORT_FILE: true
INFO: Nautical Backup Version: 2.10.4
DEBUG: Built for the platform: linux/amd64
DEBUG: Perparing enviornment variables...
DEBUG: Found defaults.env
DEBUG: CRON_SCHEDULE_ENABLED: true
DEBUG: BACKUP_ON_START: false
DEBUG: USE_DEST_DATE_FOLDER: false
DEBUG: DEST_DATE_PATH_FORMAT: date/container
DEBUG: DEST_DATE_FORMAT: %Y-%m-%d
DEBUG: USE_CONTAINER_BACKUP_DATE: true
DEBUG: USE_DEFAULT_RSYNC_ARGS: true
DEBUG: REQUIRE_LABEL: false
DEBUG: LABEL_PREFIX: nautical-backup
DEBUG: REPORT_FILE_LOG_LEVEL: INFO
DEBUG: REPORT_FILE_ON_BACKUP_ONLY: true
DEBUG: KEEP_SRC_DIR_NAME: true
DEBUG: EXIT_AFTER_INIT: false
DEBUG: LOG_RSYNC_COMMANDS: false
DEBUG: RUN_ONCE: false
DEBUG: SOURCE_LOCATION: /app/source
DEBUG: DEST_LOCATION: /app/destination
DEBUG: TEST_MODE: -1
DEBUG: HTTP_REST_API_ENABLED: true
DEBUG: HTTP_REST_API_USERNAME: admin
DEBUG: HTTP_REST_API_PASSWORD: password
DEBUG: ADDITIONAL_FOLDERS_WHEN: before
DEBUG: ADDITIONAL_FOLDERS_USE_DEST_DATE_FOLDER: false
DEBUG: NAUTICAL_DB_PATH: /config
DEBUG: NAUTICAL_DB_NAME: nautical-db.json
DEBUG: SELF_CONTAINER_ID: 021e447e5dfa5a6d3131b81f776d13baebc83623587a5096be4ea4a2c6d84eda
DEBUG: Installing CRON schedule: 55 10 * * * in TZ: Europe/Lisbon
DEBUG: Verifying source directory '/app/source'...
DEBUG: Verifying destination directory '/app/destination'...
INFO: Initializing database at '/config/nautical-db.json'...
INFO: Creating Database at path: '/config/nautical-db.json'...
INFO: Database initialized at '/config/nautical-db.json'...
DEBUG: Installing nautical backup script...
INFO: Initialization complete. Awaiting CRON schedule: 55 10 * * *
INFO: API listening on port 8069...
INFO: Connected to database at '/config/nautical-db.json'
DEBUG: Verifying source directory '/app/source'...
DEBUG: Verifying Nautical destination directory '/app/destination'...
INFO: Starting backup...
DEBUG: Running PRE_BACKUP_EXEC
INFO: Running EXEC command: /config/stop_containers.sh
WARN: Exec command error: /bin/bash: line 1: /config/stop_containers.sh: cannot execute: required file not found
DEBUG: Verifying destination directory '/app/destination'...
INFO: Processing 5 containers...
DEBUG: Containers: nautical-backup, portainer, mssql, watchtower, transmission
INFO: Stopping portainer...
DEBUG: Verifying destination directory '/app/destination'...
INFO: Backing up portainer...
DEBUG: RUNNING: 'rsync -raq /app/source/portainer/ /app/destination/portainer/'
INFO: Starting portainer...
INFO: Backup of portainer complete!
INFO: Stopping mssql...
DEBUG: Verifying destination directory '/app/destination'...
INFO: Backing up mssql...
DEBUG: RUNNING: 'rsync -raq /app/source/mssql/ /app/destination/mssql/'
INFO: Starting mssql...
INFO: Backup of mssql complete!
DEBUG: watchtower - Source directory '/app/source/watchtower' does not exist. Skipping
DEBUG: Container watchtower was not stopped. No need to start.
INFO: Stopping transmission...
DEBUG: Verifying destination directory '/app/destination'...
INFO: Backing up transmission...
DEBUG: RUNNING: 'rsync -raq /app/source/transmission/ /app/destination/transmission/'
INFO: Starting transmission...
INFO: Backup of transmission complete!
DEBUG: Containers completed: portainer, transmission, mssql
DEBUG: Containers skipped: nautical-backup, watchtower
INFO: Completed in 01m 20s
INFO: Success. 3 containers backed up! 2 skipped.
Thanks!
Oh okay, I see what's going on. The PRE_BACKUP_EXEC argument is going to run 1 time before ALL the containers are backed up. So the way you have it setup, it will stop all the containers via Synology and then Nautical will run--then Nautical wont see any running containers so not much will happen.
I think what you need to do is run a script on a per container basis using these labels.
I think this will require you to put a label on each container, kinda like this
I havent tested this script out because I don't have a synology NAS, but it should look similar to this:
services:
your-service:
labels:
- "nautical-backup.exec.before=echo name: synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="$$NB_EXEC_CONTAINER_NAME"
- "nautical-backup.exec.before=echo name: synowebapi --exec api=SYNO.Docker.Container method="start" version=1 name="$$NB_EXEC_CONTAINER_NAME"
You can also run a script like this
services:
your-service:
labels:
- "nautical-backup.exec.before=/config/stop_script.sh"
- "nautical-backup.exec.before=/config/start_script.sh"
volumes:
- path/to/your/stop_script.sh:/config/stop_script.sh
- path/to/your/start_script.sh:/config/start_script.sh
Example stop_script.sh
#!/usr/bin/env bash
# Get Nautical variables
echo "NB_EXEC_CONTAINER_NAME: $NB_EXEC_CONTAINER_NAME"
echo "NB_EXEC_CONTAINER_ID: $NB_EXEC_CONTAINER_ID"
echo -n "Stopping $NB_EXEC_CONTAINER_NAME... "
# Try to stop the Docker container with the Synoweb API.
if ! sudo synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="$NB_EXEC_CONTAINER_NAME" > /dev/null 2>&1; then
# Error handling
echo "Error stopping container $container." >&2
# exit 1 # Uncomment to stop the script in case of error
else
# Confirmation of stopping
echo -e "\rStopping $container... OK"
fi
Hi there, I have added the labels in the nautical compose:
services:
nautical-backup:
image: minituff/nautical-backup:latest
container_name: nautical-backup
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /volume1/docker/appdata/nautical/config:/config
- /volume1/docker/appdata:/app/source
- /volume1/backups/docker:/app/destination
environment: # Optional variables
- TZ=Europe/Lisbon
- PUID=1026
- PGID=100
- UMASK=002
- CRON_SCHEDULE=46 16 * * *
- LOG_LEVEL=DEBUG
# - SKIP_CONTAINERS=
- OVERRIDE_SOURCE_DIR=
- OVERRIDE_DEST_DIR=
labels:
nautical-backup.exec.before=echo name: synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="$$NB_EXEC_CONTAINER_NAME"
nautical-backup.exec.after=echo name: synowebapi --exec api=SYNO.Docker.Container method="start" version=1 name="$$NB_EXEC_CONTAINER_NAME"
It seems to be working for every container except transmission, I don't know why but it keeps saying that stopped unexpectedly...
It's good to hear it's mostly working. Im not sure what's up with transmission though. Does it need a stop timeout? Some containers need a little time to finish up any running processes.
If you run synowebapi --exec api=SYNO.Docker.Container method="stop" version=1 name="transmission" outside of Nautical (on the host machine) do you still get that error?
I have tried to run that command on the host but it doesn't work, I have tried via SSH, is there any other way?
Hmm, if the command doesn't work on the host machine then Nautical wont be able to do it. I wonder why transmission is being funky.
@alexkiddddd , for transmissions is the container name different to the running container name?
If you run sudo docker container ls --format "{{.Names}}" from your synology, it will list all the names, you may need to hard-code that one in the labels