Anthony Anthony - 2 years ago 87
Python Question

Is there a way to stop a command in a docker container

I have a docker container that is running a command. In the Dockerfile the last line is

CMD ["python", ""]
. This runs a flask server.

There are scenarios when I update
and need to kill the command, transfer the updated
file to the container, and execute
again. I imagine this to be a common scenario.

However, I haven't found a way to do this. Since this is the only command in the Dockerfile...I can't seem to kill it. from the containers terminal when I run
ps -aux
I can see that
is assigned a PID of 1. But when I try to kill it with
kill -9 1
it doesn't seem to work.

Is there a workaround to accomplish this? My goal is to be able to change
on my host machine, transfer the updated
into the container, and execute

Answer Source

You could use VOLUMES to mount your source code on your container, and just docker stop and docker restart the container.

To make a VOLUME :

add a VOLUME directive in your Dockerfile and rebuild your image :

VOLUME /path/to/mountpoint

and use the -v option when running your image.

docker run -d -v /path/to/dir/to/mount:/path/to/mountpoint myimage

/!\ These steps above are enough only for a Linux environment. /!\

To use it with something else (like Docker-machine on OSX), you must also make a mount point in the VM running Docker (probably virtualbox).

You'll have the following scheme :

<Dir to share from your host (OSX)> <= (1) mounted on => <Mountpoint on VM> <= (2) mounted on => <Container mountpoint>

The (2) is exactly like a Linux case (in fact, it IS a linux case).

The only step added is mounting the directory you want to share from your host on your VM.

Here are the steps to mount the directory you want to share on the mountpoint in your VM, and then using it with your container :

1- First stop the docker machine.

docker-machine stop <machine_name>

2- Add a sharedfolder to the VM.

VBoxManage sharedfolder add <machine_name> --name <mountpoint_name> --hostpath <dir_to_share>

3- Restart the docker machine :

docker-machine start <machine_name>

4- Creating the mountpoint with ssh and mounting the sharedfolder on it :

docker-machine ssh <machine_name> "sudo mkdir <mountpoint_in_vm>; sudo mount -t vboxsf <mountpoint_name> <mountpoint_in_vm>"

5- And then to mount the directory on your container, run :

docker run -d -v <mountpoint_in_vm>:</path/to/mountpoint (in the container)> myimage

And to clean all this when you don't need it anymore :

6- Unmount in VM :

docker-machine ssh <machine_name> "sudo umount <mountpoint_in_vm>; sudo rmdir <mountpoint_in_vm>"

7- Stop VM :

docker-machine stop <machine_name>

8- Remove shared folder :

VBoxManage sharedfolder remove <machine_name> --name <mountpoint_name>

Here is a script I made for studies purpose, feel free to use it if it can help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download