Moved to _dev
This commit is contained in:
65
bash/docker-volumes.sh
Normal file
65
bash/docker-volumes.sh
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# The docker-export and docker-commit/docker-save commands do not save the container volumes.
|
||||
# Use this script to save and load the container volumes.
|
||||
#
|
||||
# v1.8 by Ricardo Branco
|
||||
#
|
||||
# NOTES:
|
||||
# + This script could have been written in Python or Go, but the tarfile module and the tar package
|
||||
# lack support for writing sparse files.
|
||||
# + We use the Ubuntu 22.04 Docker image with tar v1.29 that uses SEEK_DATA/SEEK_HOLE to manage sparse files.
|
||||
#
|
||||
|
||||
verbose=""
|
||||
if [[ $1 == "-v" || $1 == "--verbose" ]] ; then
|
||||
verbose="-v"
|
||||
shift
|
||||
fi
|
||||
|
||||
if [[ $# -ne 3 || ! $2 =~ ^(save|load)$ ]] ; then
|
||||
echo "Usage: $0 [-v|--verbose] CONTAINER [save|load] TARBALL" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IMAGE="ubuntu:22.04"
|
||||
|
||||
# Set DOCKER=podman if you want to use podman.io instead of docker
|
||||
DOCKER=${DOCKER:-"docker"}
|
||||
|
||||
get_volumes () {
|
||||
$DOCKER inspect --type container -f '{{range .Mounts}}{{printf "%v\x00" .Destination}}{{end}}' "$CONTAINER" | head -c -1 | sort -uz
|
||||
}
|
||||
|
||||
save_volumes () {
|
||||
if [ -f "$TAR_FILE" ] ; then
|
||||
echo "ERROR: $TAR_FILE already exists" >&2
|
||||
exit 1
|
||||
fi
|
||||
umask 077
|
||||
# Create a void tar file to avoid mounting its directory as a volume
|
||||
touch -- "$TAR_FILE"
|
||||
tmp_dir=$(mktemp -du -p /)
|
||||
get_volumes | $DOCKER run --rm -i --volumes-from "$CONTAINER" -e LC_ALL=C.UTF-8 -v "$TAR_FILE:/${tmp_dir}/${TAR_FILE##*/}" $IMAGE tar -c -a $verbose --null -T- -f "/${tmp_dir}/${TAR_FILE##*/}"
|
||||
}
|
||||
|
||||
load_volumes () {
|
||||
if [ ! -f "$TAR_FILE" ] ; then
|
||||
echo "ERROR: $TAR_FILE doesn't exist in the current directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
tmp_dir=$(mktemp -du -p /)
|
||||
$DOCKER run --rm --volumes-from "$CONTAINER" -e LC_ALL=C.UTF-8 -v "$TAR_FILE:/${tmp_dir}/${TAR_FILE##*/}":ro $IMAGE tar -xp $verbose -S -f "/${tmp_dir}/${TAR_FILE##*/}" -C / --overwrite
|
||||
}
|
||||
|
||||
CONTAINER="$1"
|
||||
TAR_FILE=$(readlink -f "$3")
|
||||
|
||||
set -e
|
||||
|
||||
case "$2" in
|
||||
save)
|
||||
save_volumes ;;
|
||||
load)
|
||||
load_volumes ;;
|
||||
esac
|
||||
40
bash/update_all_docker_containers.sh
Normal file
40
bash/update_all_docker_containers.sh
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
# Function to preserve and update container
|
||||
preserve_and_update_container() {
|
||||
local container=$1
|
||||
local image=$(docker inspect --format '{{.Config.Image}}' "$container")
|
||||
|
||||
# Pull the latest image version
|
||||
docker pull $image
|
||||
|
||||
# Compare image IDs to determine if an update is needed
|
||||
local latest_image_id=$(docker inspect --format '{{.Id}}' $image)
|
||||
local container_image_id=$(docker inspect --format '{{.Image}}' "$container")
|
||||
|
||||
if [[ "$latest_image_id" != "$container_image_id" ]]; then
|
||||
echo "Updating $container..."
|
||||
|
||||
# Capture current configurations
|
||||
local env_vars=$(docker inspect $container --format '{{range .Config.Env}}{{println .}}{{end}}')
|
||||
local volumes=$(docker inspect $container --format '{{range .Mounts}}{{println .Source ":" .Destination}}{{end}}')
|
||||
local network=$(docker network ls --filter id=$(docker inspect $container --format '{{.HostConfig.NetworkMode}}') --format '{{.Name}}')
|
||||
|
||||
# Remove the outdated container
|
||||
docker rm -f $container
|
||||
|
||||
# Recreate the container with the same configurations
|
||||
docker run -d --name $container $(echo "$env_vars" | xargs -I {} echo --env '{}') $(echo "$volumes" | xargs -I {} echo -v '{}') --network="$network" $image
|
||||
echo "$container updated successfully."
|
||||
else
|
||||
echo "$container is already up to date."
|
||||
fi
|
||||
}
|
||||
|
||||
# Iterate over all running containers
|
||||
for container in $(docker ps --format "{{.Names}}"); do
|
||||
preserve_and_update_container $container
|
||||
done
|
||||
|
||||
echo "Container update check complete while preserving existing container configurations."
|
||||
Reference in New Issue
Block a user