Is it possible to start a shell session in a running container (without ssh)

后端 未结 15 1020
滥情空心
滥情空心 2020-11-29 14:33

I was naively expecting this command to run a bash shell in a running container :

docker run \"id of running container\" /bin/bash

it look

相关标签:
15条回答
  • 2020-11-29 15:21

    Maybe you were mislead like myself into thinking in terms of VMs when developing containers. My advice: Try not to.

    Containers are just like any other process. Indeed you might want to "attach" to them for debugging purposes (think of /proc//env or strace -p ) but that's a very special case.

    Normally you just "run" the process, so if you want to modify the configuration or read the logs, just create a new container and make sure you write the logs outside of it by sharing directories, writing to stdout (so docker logs works) or something like that.

    For debugging purposes you might want to start a shell, then your code, then press CTRL-p + CTRL-q to leave the shell intact. This way you can reattach using:

    docker attach <container_id>
    

    If you want to debug the container because it's doing something you haven't expect it to do, try to debug it: https://serverfault.com/questions/596994/how-can-i-debug-a-docker-container-initialization

    0 讨论(0)
  • 2020-11-29 15:24

    With docker 1.3, there is a new command docker exec. This allows you to enter a running docker:

    docker exec -it "id of running container" bash
    
    0 讨论(0)
  • 2020-11-29 15:24

    Since things are achanging, at the moment the recommended way of accessing a running container is using nsenter.

    You can find more information on this github repository. But in general you can use nsenter like this:

    PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
    nsenter --target $PID --mount --uts --ipc --net --pid
    

    or you can use the wrapper docker-enter:

    docker-enter <container_name_or_ID>
    

    A nice explanation on the topic can be found on Jérôme Petazzoni's blog entry: Why you don't need to run sshd in your docker containers

    0 讨论(0)
提交回复
热议问题