How do you start a Docker-ubuntu container into bash?

后端 未结 2 1692
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 16:41

The answers from this question do not work.

The docker container always exits before I can attach or won\'t accept the -t flag. I could list al

2条回答
  •  孤独总比滥情好
    2021-02-01 17:08

    First of all, a container is not a virtual machine. A container is an isolation environment for running a process. The life-cycle of the container is bound to the process running inside it. When the process exits, the container also exits, and the isolation environment is gone. The meaning of "attach to container" or "enter an container" actually means you go inside the isolation environment of the running process, so if your process has been exited, your container has also been exited, thus there's no container for you to attach or enter. So the command of docker attach, docker exec are target at running container.

    Which process will be started when you docker run is configured in a Dockerfile and built into a docker image. Take image ubuntu as an example, if you run docker inspect ubuntu, you'll find the following configs in the output:

    "Cmd": ["/bin/bash"]
    

    which means the process got started when you run docker run ubuntu is /bin/bash, but you're not in an interactive mode and does not allocate a tty to it, so the process exited immediately and the container exited. That's why you have no way to enter the container again.

    To start a container and enter bash, just try:

    docker run -it ubuntu
    

    Then you'll be brought into the container shell. If you open another terminal and docker ps, you'll find the container is running and you can docker attach to it or docker exec -it bash to enter it again.

    You can also refer to this link for more info.

提交回复
热议问题