The 1+N problem | Menu | Correctly working with files and directories

Useful docker commands

There's really no need to create a 1000 words blog post for each one of these simple tasks, so unlike others, I won't. Here's the commands:

Updating compose containers

This only works if you use moving tags like "latest", otherwise you have to edit your compose file first and update the image versions.

Run the commands from within the directory that contains your compose file.

# Pull images
docker compose pull
# Run new images (if any)
docker compose up -d

Docker compose is smart enough to not kill containers if the image is still the same. Also you don't need to manually stop your containers for this. "up" is really just a badly chosen name for what it should be: "sync". Because it syncs the running state to the declared state.

Deleting all docker containers and images

If you've created a mess or just want a clean slate, you can delete all containers, images, and even volumes.

# Stop all containers
docker stop $(docker ps -aq)
# Remove all containers
docker container prune -f
# Remove all container images
docker image prune -a -f
# Remove all volumes (dangerous see below)
docker volume prune -a -f

Deleting volumes

Deleting volumes has no effect on paths mapped to the host, that data has to be removed manually.

Do not delete the volumes if you plan to recreate some containers later and want to reuse their existing data.

Volume backup and restore

Sometimes you may want to back up or restore files of a docker volume. Note that backing up volumes while a container is using it may lead to an inconsistent snapshot. Restoring while a container is using it is not recommended, because most applications don't deal well if you replace important files while they're using them.

Backup

docker volume ls -q | xargs -P 1 -r -I VOLNAME docker run --rm --volume VOLNAME:/VOLNAME:ro --volume ./backup:/backup busybox tar cf /backup/VOLNAME.tar VOLNAME

Output will be a series of tar files (one per volume) in a "backup" folder.

The command above uses xargs with -P 1 which limits parallelism to one command at a time. It's ill advised to change this, because you're just putting unnecessary stress on your disk by trying to back up volumes in parallel. The gain in speed is minimal unless you have dozens of tiny volumes.

If you insist on doing this anyways, run docker pull busybox before a backup/restore, or you will have multiple docker instances potentially trying to pull the busybox image at once.

Restore

Restoring is a bit more complex because we need to (re-)create all volumes first. You should not restore into an existing volume because it will not delete files that already exist.

Run the command from the directory that contains the "backup" directory. It will recreate a volume named after each tar file

for volname in $(basename -s .tar backup/*.tar)
do
    docker volume rm -f "$volname"
    docker volume create --name "$volname"
    docker run --rm --volume $volname:/$volname --volume $(pwd)/backup:/backup:ro busybox tar xf /backup/$volname.tar
done

I'm aware that Linux users have massive distrust in the functionality of their utilities if they don't spam the terminal full of messages. To curb your anxiety, you can replace "cf" and "xf" with "cvf" or "xvf" respectively to make tar show every entry it processes.