31

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?

1
  • What is your question here? What are you trying to accomplish? Please rephrase the title of this post in terms of a question. Commented May 7, 2015 at 5:52

1 Answer 1

54

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
Sign up to request clarification or add additional context in comments.

3 Comments

A deeper explaining on why -t would break it would be nice to have :)
For those looking to import a database file, like the above, but using docker-compose, the following may help: docker-compose exec -T SERVICE mysql database < database.sql.xz Using -T will disable pseudo-tty allocation which allows you keep stdin open without tty.
I don't understand how this is creating a file inside of the container. Using the < to read I always get bash: /app/db/database.sql: No such file or directory. It instead seem to create the file on the system you're locally on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.