Backing up wordpress docker containers created with the official image
In this short guide we'll learn how to back up a wordpress docker container that doesn't use Volumes, but keeps its data inside itself (something highly discouraged).
> Archived article from 2017, from the old nutellino.it: commands and versions may be out of date.
In this short guide we'll learn how to back up a wordpress docker container that doesn't use Volumes, but keeps its data inside itself (something highly discouraged).
Let's take as an example a generic container created with the name nome-wordpress
This image will create an internal data volume /var/www/html/
To see the data of the attached volumes we need to use the inspect command:
bash
docker inspect nome-wordpress
as you can see from the output, the data is in /var/www/html .
There are two phases: in the first one you save the docker container, in case any particular changes were made to it, php packages installed or ad hoc configurations.
To find the id you need to use docker ps -a
So we'll go ahead and commit the container locally, so we can then save it to an archive.
nome-wordpress will be the name of the saved image, :backup will be the tag we're going to use
after that you save it to a file by doing
bash
docker save nome-wordpress:backup |gzip> nome-wordpress_backup.tar.gz
In the file nome-wordpress_backup.tar.gz we'll have the backup of the running container, but without data.
To save the data that's inside the container in /var/www/html/ we need to use a small helper container: busybox
Here are the parameters:
$(pwd) means we map the current directory to the container directory written after the :
/backups is the directory inside the busyboxy container
—-rm is the parameter saying that after running the command tar -c /var/www/html -f /backups/html_nome-wordpress.tar the created container will be destroyed
bash
docker run --rm-v$(pwd):/backups --volumes-from=nome-wordpress busybox tar-c /var/www/html -f /backups/html_nome-wordpress.tar
RESTORE ON ANOTHER MACHINE
The procedure here should be a bit simpler. Once you've copied over the two tars, the nome-wordpress container image and the data, you need to import them.
First we need to load the container image locally
bash
docker load < nome-wordpress_backup.tar.gz
This way, running the command
bash
docker images
We'll get in the output an entry with the name of the loaded image
text
# output
REPOSITORY TAG IMAGE ID CREATED SIZE
nome-wordpress backup 3915f4713629 About an hour ago 419 MB
We can then recreate and start the container from the image
bash
docker run --name nome-wordpress-nuova-istanza -p8080:80 -d nome-wordpress:backup
Listing the running containers we'll then get:
bash
docker images
# output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
874bca63408e nome-wordpress:backup "docker-entrypoint..."25 seconds ago Up 23 seconds 0.0.0.0:8080->80/tcp nome-wordpress-nuova-istanza
This container will have all the service customizations and other configurations made earlier, but not the old site's data.
To import the data back we'll get help from a busybox helper container like for the backup, where instead of running the save command, we'll run the extraction command tar -xvf /backups/html_nome-wordpress.tar -C / .
Let's assume we're in the same folder where we copied the two tars
bash
docker run --rm-v$(pwd):/backups --volumes-from=nome-wordpress-nuova-istanza busybox tar-xvf /backups/html_nome-wordpress.tar -C /
And that's it, we've restored the container with its data included!