4

I want to create mysql dumps for a database which is running in docker container. However I do not want to get into the container and execute the command but do it from the host machine. Is there a way to do it. I tried few things but probably I am wrong with the commands.

docker exec -d mysql sh mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql

docker exec -d mysql sh $(mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql)

docker exec -d mysql mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql

the dumps directory is already bind to the host machine.

These commands are seems not the right way to do it or probably not the right way to do it at all. These always ends up with an error:

bash: /dumps/MyNewDump.sql: No such file or directory

But if I just run mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql inside the container it works fine.

Rahul Chakrabarty
  • 2,149
  • 7
  • 39
  • 70

2 Answers2

7

This worked on my end(just replace the container ID):

docker exec 1d3595c0ce87 sh -c 'mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql'

mysqldump: [Warning] Using a password on the command line interface can be insecure.
Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • Any ideas or approach how to execute mysqldump from another container? For example, when there's a package that automates backups but it assumes mysqldump is in the same host. [Question here](https://stackoverflow.com/q/64070267/1883256). – Pathros Sep 27 '20 at 13:47
1

mysqldump takes the usual MySQL CLI options to connect to a server running somewhere else. That means you can run it directly from the host, without needing docker exec (administrator) permissions.

mysqldump -h127.0.0.1 -uroot -pSomePassword DBName > MyNewDump.sql

In contrast to the docker exec forms, this creates the dump file on the host, but that's probably what you want.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Any ideas or approach how to execute mysqldump from another container? For example, when there's a package that automates backups but it assumes mysqldump is in the same host. [Question here](https://stackoverflow.com/q/64070267/1883256). – Pathros Sep 27 '20 at 13:47
  • You'd use normal inter-container communication (for example, as described in [Networking in Compose](https://docs.docker.com/compose/networking/)) but use the other container's name in the `mysqldump -h` hostname option. In general if you've hard-coded `localhost` or `127.0.0.1` or any other host name in your application code you'll have trouble running it in Docker or another environment that's not your developer setup. – David Maze Sep 27 '20 at 14:06