I have a MySQL Docker container (db_mysql
) with database wp_almond
When I try to dump it with docker exec -i db_mysql mysqldump
it doesn't work
$ docker exec -i db_mysql mysqldump -u root -p wp_almond > wp_almond.sql
$ docker exec -i db_mysql ls # Notice that "wp_almond.sql" was not created
bin
boot
dev
docker-entrypoint-initdb.d
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
But entering the container and then doing the mysqldump
inside works
$ docker exec -it db_mysql sh
$ mysqldump -u root -p wp_almond > wp_almond.sql
$ ls # Here "wp_almond.sql" is present
bin
boot
dev
docker-entrypoint-initdb.d
entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
wp_almond.sql
I don't know why, I need to do it without entering the container with sh
for a backup script
CodePudding user response:
When you run
docker exec -i db_mysql mysqldump -u root -p wp_almond
then the output is produced to stdout (of your host terminal)
Therefore, when you run
docker exec -i db_mysql mysqldump -u root -p wp_almond > wp_almond.sql
It simply redirects the output of your terminal to file on your host itself.
If you want to save it in the container, try this instead
docker exec -i db_mysql sh -c "mysqldump -u root -p wp_almond > wp_almond.sql"