Docker 运行:如何在没有命令的情况下运行 Ubuntu:16.04?

oto*_*ong 4 docker

我想尝试在 docker 中在 Ubuntu 中安装一个程序,

所以我直接从命令提示符运行

docker run --name ubuntu_test ubuntu:16.04
docker exec -it ubuntu_test bash
Run Code Online (Sandbox Code Playgroud)

但它不起作用,它说容器没有运行?如何在不设置 dockerfile 的情况下运行 bash?(我尝试使用 dockerfile,但由于交互式安装程序问题,它不起作用)

所以我想也许直接从 bash 安装它可以工作。

her*_*erm 7

问题是您的命令不会使进程保持活动状态,也不会将其保留在后台,因此容器完成其工作并停止运行。它就像docker run hello-world打印一些东西并退出一样。

docker run -it --name ubuntu_test ubuntu:16.04会为你工作。该文件解释说:

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process. -i -t is often written -it

另一种方法是以分离模式 (-d) 运行容器并为其提供长时间运行的命令,以便它不会立即退出:

docker run --name ubuntu_test -d ubuntu:16.04 sleep 300 docker exec -it ubuntu_test bash


Krz*_*ski 6

You are failing to start the container. Try this:

docker run -itd ubuntu:16.04 bash
Run Code Online (Sandbox Code Playgroud)

-i, --interactive - Keep STDIN open even if not attached

-t, --tty - Allocate a pseudo-TTY

-d, --detach - Run container in background and print container ID

After this command list all your containers(docker ps):

在此处输入图片说明

Now you can attach to your running container and do some stuff:

docker exec -it 82d0bb7754e7 /bin/bash
Run Code Online (Sandbox Code Playgroud)

(in this case, to indicate the container I used the ID, you can also use container name)