The other day was maintenance day for my home lab, which mainly means updating docker containers. This time I decided to take a different approach and, rather than using Watchtower, I opened my trusted IDE and created a Bash Script.
The reason why I decided to go for a Bash Script is because I had an issue with an update using Watchtower, which basically wiped out the container’s data (luckily that container was in staging). I am sure the issue was not related to Watchtower itself, but it was a bit annoying going through the setup process again.
NOTE: Watchtower gives you the option to exclude some containers. Take a look at this for more details.
I wanted something very simple (always KISS): while executing the script, I pass the containers’ name as arguments, than the script asks me if I want to Delete or Update them (that’s why DU-it).
IMPORTANT: Keep in mind that this script is based on my lab, where I mainly use docker-compose and every folder is named with the same name of the container, therefore it may not work in your environment.
This option stops and removes the container, it also deletes the image used by that container.
The script will check if the argument provided is a valid match for a container’s name and, if it is, it creates a container_name.tmp file which contains the image used by that container; than, stops and remove that container and
cat out the .tmp file to know what image needs to be deleted. Once done, all the .tmp files will be deleted.
This option goes through all the steps of the previous option, but also goes into the container’s directory and executes a
docker-compose up -d so it will pull down the new and updated image.
As final touch, I gave each option a colour, so I could easily recognise what’s going on if I get distracted doing something else.
I am sure it can be written in a better way, but at the end of the day it works just fine to me. Also, there’s always room for future improvements.
You can see the script on my GitHub here.