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

后端 未结 15 1198
滥情空心
滥情空心 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:03

    First thing you cannot run

    docker run "existing container" command
    

    Because this command is expecting an image and not a container and it would anyway result in a new container being spawned (so not the one you wanted to look at)

    I agree with the fact that with docker we should push ourselves to think in a different way (so you should find ways so that you don't need to log onto the container), but I still find it useful and this is how I work around it.

    I run my commands through supervisor in DEAMON mode.

    Then I execute what I call docker_loop.sh The content is pretty much this:

    #!/bin/bash
    /usr/bin/supervisord
    /usr/bin/supervisorctl
    while ( true )
        do
        echo "Detach with Ctrl-p Ctrl-q. Dropping to shell"
        sleep 1
        /bin/bash
    done
    

    What it does is that it allows you to "attach" to the container and be presented with the supervisorctl interface to stop/start/restart and check logs. If that should not suffice, you can Ctrl+D and you will drop into a shell that will allow you to have a peek around as if it was a normal system.

    PLEASE DO ALSO TAKE INTO ACCOUNT that this system is not as secure as having the container without a shell, so take all the necessary steps to secure your container.

提交回复
热议问题