doktoreas
doktoreas

Reputation: 243

Running multiple commands on the same container using docker-py

I'd like to create a procedure using docker-py to run multiple commands inside a Docker container. Reading the documentation, I have found that I can use the command option when creating the container..something like this:

c.create_container(image="myimage", command="mycommand")

Is there a way to run other commands on the just created container?

Thanks!

Upvotes: 5

Views: 3885

Answers (4)

Amos Folarin
Amos Folarin

Reputation: 2179

As of docker v1.3. it is possible to inject new processes into a running container using the docker-exec command (see https://docs.docker.com/reference/commandline/cli/#exec).

However, you should consider the downside to running multiprocess containers, "separation of concerns" (discussed in many articles online).

Upvotes: 2

itsafire
itsafire

Reputation: 6093

You can do that by using a list for the command parameter instead of a string. Additionally you need to use sh -c to execute your commands:

c.create_container(image="myimage", command=['sh','-c','ls | wc && date'])

You're still running only one command (the shell) but you are providing a shell script as a string argument via the -c shell parameter. And within this shell script you can do whatever you like to.

Upvotes: 3

mgaido
mgaido

Reputation: 3055

I don't think it is possible using docker-py, but there are several ways to do that, depending on which command you wish to run.

A possibility is to set command1 && command2 as command argument, but the second will start only when the first finishes (so if it never lasts, the second command will never start...). Another option is to run the bash in the container, and then run commands on it through the syntax:

echo your_command | docker attach container_id

from the command line of the host.

Upvotes: 0

Thomasleveil
Thomasleveil

Reputation: 103965

Using docker-py or not a container can only start one process. However this process can be a script or a program that spawns multiple other processes.

Take a look at the Docker article on how to supervisor with Docker to run multiple process in a container.

Upvotes: 0

Related Questions