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", "myprogram.py"] . This runs a flask server.

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

  • not able to install mysql-server on docker container
  • Looking for a convenient way to start and stop applications with docker-compose
  • Robot Framework and CI, run in container or not
  • Get the size of a Docker image before a pull?
  • Docker 0.9 - /etc/sysctl.conf not working
  • Rails 5.0.0.1 Atomicity problems?
  • 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 python myprogram.py 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 myprogram.py on my host machine, transfer the updated myprogram.py into the container, and execute python myprogram.py again.

  • Cant connect to docker mongodb
  • How to deploy node app to docker using maven
  • How to install nvm in a Dockerfile?
  • How to expose host machine aliases to Docker container?
  • How to install app from git repository via docker?
  • Run commands on create a new Docker container
  • 2 Solutions collect form web for “Is there a way to stop a command in a docker container”

    You could use VOLUMES to mount your myprogram.py 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.

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

    Not really. The common scenario is either:

    • Kill existing container
    • Build new image via your Dockerfile
    • Boot container from new image

    Or:

    • Start container with a volume mount pointing at your source
    • Restart the container when you update your code

    Either one works. The second is useful for development, since it has a slightly quicker turnaround.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.