Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipeline in docker exec from command line and from python api

Tags:

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?

like image 745
pingz Avatar asked May 06 '15 09:05

pingz


People also ask

How do I run a command in Docker exec?

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.

How do I Dockerize API in Python?

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.

How do I run a docker container from the command line?

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.

Which command can exec commands inside docker container?

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.


1 Answers

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 
like image 135
Philippe Blanc Avatar answered Oct 05 '22 17:10

Philippe Blanc