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

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

    There is actually a way to have a shell in the container.

    Assume your /root/run.sh launches the process, process manager (supervisor), or whatever.

    Create /root/runme.sh with some gnu-screen tricks:

    # Spawn a screen with two tabs
    screen -AdmS 'main' /root/run.sh
    screen -S 'main' -X screen bash -l
    screen -r 'main'
    

    Now, you have your daemons in tab 0, and an interactive shell in tab 1. docker attach at any time to see what's happening inside the container.

    Another advice is to create a "development bundle" image on top of the production image with all the necessary tools, including this screen trick.

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

    It's useful assign name when running container. You don't need refer container_id.

    docker run --name container_name yourimage docker exec -it container_name bash

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

    EDIT: Now you can use docker exec -it "id of running container" bash (doc)

    Previously, the answer to this question was:

    If you really must and you are in a debug environment, you can do this: sudo lxc-attach -n <ID> Note that the id needs to be the full one (docker ps -notrunc).

    However, I strongly recommend against this.

    notice: -notrunc is deprecated, it will be replaced by --no-trunc soon.

    0 讨论(0)
  • 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.

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

    Here's my solution

    In the Dockerfile:

    # ...
    RUN mkdir -p /opt
    ADD initd.sh /opt/
    RUN chmod +x /opt/initd.sh
    ENTRYPOINT ["/opt/initd.sh"]
    

    In the initd.sh file

    #!/bin/bash
    ...
    /etc/init.d/gearman-job-server start
    /etc/init.d/supervisor start
    #very important!!!
    /bin/bash
    

    After image is built you have two options using exec or attach:

    1. Use exec (preferred) and run:

      docker run --name $CONTAINER_NAME -dt $IMAGE_NAME
      

      then

      docker exec -it $CONTAINER_NAME /bin/bash
      

      and use CTRL + D to detach

    2. Use attach and run:

      docker run --name $CONTAINER_NAME -dit $IMAGE_NAME
      

      then

      docker attach $CONTAINER_NAME
      

      and use CTRL + P and CTRL + Q to detach

      Note: The difference between options is in parameter -i

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

    Keep an eye on this pull request: https://github.com/docker/docker/pull/7409

    Which implements the forthcoming docker exec <container_id> <command> utility. When this is available it should be possible to e.g. start and stop the ssh service inside a running container.

    There is also nsinit to do this: "nsinit provides a handy way to access a shell inside a running container's namespace", but it looks difficult to get running. https://gist.github.com/ubergarm/ed42ebbea293350c30a6

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