3 Update Docker Container
Philip edited this page 2024-09-18 17:23:02 +12:00

This is a simple script to update any docker container defined by a docker-compose.yml file. It can be placed anywhere and takes the path to the directory containing the compose file as an argument. Docker commands are run from there. This is written for (default) root-mode Docker, hence the sudo command. If you are running rootless-mode, simply remove sudo from the three docker command lines.

Don't forget to make the file executable! chmod +x update-docker-container.sh

Usage: $ ./update-docker-container.sh path/to/docker/folder

if [[ $# -ne 1 ]]
then
  echo "Please provide the path to the directory containing the compose file."
  exit 1
fi

cd "$1"

if [[ $? -ne 0 ]]
then
  echo "Directory does not exist."
  exit 1
fi

find docker-compose.yml &> /dev/null

if [[ $? -ne 0 ]]
then
  echo "Docker compose file not found."
  exit 1
fi

sudo docker compose pull 2>&1 | tee tmp-docker-update.log

if [[ $( cat tmp-docker-update.log | grep complete | wc -l) -eq 0 ]]
then
  echo "Already up to date."
  rm tmp-docker-update.log
  exit 0
fi

rm tmp-docker-update.log

sudo docker compose down
sudo docker compose up -d

Note that &> may not work in all shells, but does work from Bash 4, so I would assume any modern shells can handle it. Otherwise, replace the line with find docker-compose.yml > /dev/null 2>&1