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

后端 未结 15 1166
滥情空心
滥情空心 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条回答
  •  萌比男神i
    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

提交回复
热议问题