LLI Wiki — My Digital Knowledge Base

Back up and restore a MySQL database in a Docker container

Dump and reload a MySQL database running in a Docker container with mysqldump and mysql, and inspect the container network and published ports.

When MySQL runs in a Docker container, mysqldump and mysql can be run inside the container with docker exec. The commands below dump a database to a file on the host and load it back.

Back up a database

docker exec CONTAINER /usr/bin/mysqldump -u root --password=<PASSWORD> DATABASE > backup.sql

Restore a database

cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=<PASSWORD> DATABASE

An equivalent restore runs the client through a shell inside the container:

docker exec -i CONTAINER_NAME_OR_ID sh -c 'exec mysql -uYOUR_USER -p<PASSWORD> YOUR_DATABASE' < backup.sql

Inspect the container

Purpose Command
Full container details docker inspect CONTAINER_NAME_OR_ID
Networks of the container docker inspect CONTAINER_NAME_OR_ID | jq '.[].NetworkSettings.Networks'
Network mode, ID and name docker inspect CONTAINER_NAME_OR_ID | grep -i '"NetworkMode"|"NetworkID"|"NetworkName"'
Published ports of all containers docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" -a

Notes

Mastodon LLIMastodon SocialFosstodon