I came across a need to start afresh with my docker setup. I didn’t want to re-create all the port and volume mappings for my various containers. Fortunately I found a way around this by using docker-autocompose to create .yml files with all my settings and docker-compose to restore them to my new docker host.
Backup
Docker-autocompose source: https://github.com/Red5d/docker-autocompose
git clone https://github.com/Red5d/docker-autocompose.git cd docker-autocompose docker build -t red5d/docker-autocompose .
With docker-autocompose created you can then use it to create .yml files for each of your running containers by utilizing a simple BASH for loop:
for image in $(docker ps --format '{{.Names}}'); do docker run -v /var/run/docker.sock:/var/run/docker.sock red5d/docker-autocompose $image > $image.yml; done
Simple.
Restore
To restore, install and use docker-compose:
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Next we use another simple for loop to go through each .yml file and import them into Docker. The sed piece escapes any $ characters in the .yml files so they will import properly.
for file in *.yml; do sed 's/\$/\$\$/g' -i $file; docker-compose -f $file up --force-recreate -d; done
You can safely ignore the warnings about orphans.
That’s it!
Troubleshooting
ERROR: Invalid interpolation format for “environment” option in service “Transmission”: “PS1=$(whoami)@$(hostname):$(pwd)$ “
This is due to .yml files which contain unescaped $ characters.
Escape any $ with another $ using sed
sed 's/\$/\$\$/g' -i <filename>.yml
ERROR: The Compose file ‘./MariaDB.yml’ is invalid because:
MariaDB.user contains an invalid type, it should be a string
My MariaDB docker .yml file had a user: environment variable that was a number, which docker compose interpreted as a number instead of a string. I had to modify that particular .yml file and add quotes around the value that I had for the User environment variable.