What I try to implement is invoking mysqldump
in container and dump the database into the container's own directory.
At first I try command below:
$ docker exec container-name mysqldump [options] database | xz > database.sql.xz
That's not working, so I try another one which is :
$ docker exec container-name bash -c 'mysqldump [options] database | xz > database.sql.xz'
This time it worked.
But that's really lame.
Then I try using docker-py this time cmd
option that worked looks like this:
cmd=['bash', '-c', 'mysqldump [options] database | xz > database.sql.xz']
the logger event as below:
level="info" msg="-job log(exec_start: bash -c mysqldump [options] database | xz > database.sql.xz, fe58e681fec194cde23b9b31e698446b2f9d946fe0c0f2e39c66d6fe68185442, mysql:latest) = OK (0)"
My question:
is there a more elegant way to archive my goal?
Use the command docker exec -it <container name> /bin/bash to get a bash shell in the container. Or directly use docker exec -it <container name> <command> to execute whatever command you specify in the container.
You need to set up a working directory and then copy the contents from the local directory to the docker directory. Then, you need to run the install command so that it installs all the dependencies. Finally, you need to give the command when the container is instantiated.
The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images. If you use an image that is not on your system, the software pulls it from the online registry.
Method 1: Using Bash You can directly access the bash of the Docker Container and execute commands there. It's very easy to launch the bash of the Container and you can do so using this command.
You are almost there, you just need to add the -i flag to make the pipe work:
-i, --interactive Keep STDIN open even if not attached docker exec -i container-name mysqldump [options] database > database.sql.xz
I replaced the pipe by a file redirection but it will work the same with a Pipe. Just make sure to don't use the -t option as this will break it.
Extra:
To import back the sql dump into mysql:
docker exec -i container-name mysql [options] database < database.sql.xz
This little script will detect if I am running mysql in a pipe or not:
#!/bin/bash if [ -t 0 ]; then docker exec -it container-name mysql "$@" else docker exec -i container-name mysql "$@" fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With