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.

  • Running a longer command from docker
  • Root user can't connect to any database with MongoDB
  • Docker permissios shared Volume on mac
  • Sandbox command execution with docker via Ajax
  • docker-compose conflicts with Dokerfile entry-point script
  • Running 32bit LXC on 64 bit host machine?
  • 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.

  • Null port bindings from inspect, the first time. a second time good. why?
  • Adding a backup crontab into a docker container
  • Reflecting code changes in docker containers
  • Docker volume mounts with template strings in Docker Compose
  • want to use the same host and port for ssh session
  • Get docker-compose.yml file location from running 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.