简体   繁体   English

命令 docker 启动 -a<container id> 什么都不做,“Ubuntu 20.04.02 LTS”</container>

[英]Command docker start -a <Container ID> do nothing, “Ubuntu 20.04.02 LTS”

i'm trying to run the start command in docker after creating a container.创建容器后,我正在尝试在 docker 中运行start命令。 for example:例如:

$ docker create busybox echo hi there

it gives me the id of that container like 4e59d0fe8584bb4dcaf44dbce100253b6767bf51546edc27f29f39f52ed57957它给了我该容器的 ID,例如4e59d0fe8584bb4dcaf44dbce100253b6767bf51546edc27f29f39f52ed57957

and when i try to start that container without any flags like: -a flag it works, but it only gives me that id back again.当我尝试在没有任何标志的情况下启动该容器时: -a标志它可以工作,但它只会再次给我那个 id。 but when i try to show the output using the attach -a flag, actually nothing happened, even it didn't give me an error, simply the command still running without anything happened.但是当我尝试使用附加-a标志显示 output 时,实际上什么也没发生,即使它没有给我一个错误,只是命令仍在运行而没有发生任何事情。

i also couldn't kill the command and stop the execution by Ctrl+c, so the only option i had to close the terminal我也无法通过 Ctrl+c 终止命令并停止执行,所以我必须关闭终端的唯一选项

在此处输入图像描述 i tried to make the problem clear as possible as i can我尽量把问题弄清楚

You can run the image via this command:您可以通过以下命令运行映像:

docker run -it busybox

It goes you to shell environment and you have -i (interactive) -t (tty) terminal, which means you see the terminal.它会带您进入 shell 环境,并且您有 -i(交互式)-t(tty)终端,这意味着您可以看到终端。

The default CMD (PID1) for busybox image is sh , see this . busybox image的默认CMD (PID1) 是sh ,请参阅this

For docker create busybox echo hi there , the COMMAND becomes echo hi there .对于docker create busybox echo hi thereCOMMANDecho hi there This means after container starts, it will first execute echo hi there , then as PID1 exit, the container exit too.这意味着容器启动后,首先会在此处执行echo hi there ,然后作为 PID1 退出,容器也退出。 If you use docker ps , you won't find your container, you could just find your exited container with docker ps -a .如果您使用docker ps ,您将找不到您的容器,您可以使用docker ps -a找到您退出的容器。

So,所以,

  • If you intend to run a one time task, then as the container finish its task, it's normal you could not enter into container anymore.如果您打算运行一次性任务,那么随着容器完成其任务,您无法再进入容器是正常的。

  • If you intend to run a daemon task, leave the container service there, you should choose a command which won't finish after run, then your container will still there.如果你打算运行一个守护任务,把容器服务留在那里,你应该选择一个运行后不会完成的命令,那么你的容器仍然在那里。

For your case, to quick let you understand it, you could use next to have a quick understanding, use tail -f /dev/null to let the container not exit:对于您的情况,为了让您快速了解它,您可以使用 next 快速了解,使用tail -f /dev/null让容器不退出:

# docker create busybox sh -c "echo hi there; tail -f /dev/null"
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root@shlava:~# docker start 840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
840d7c972a96712e48c9aa391aa63638fb10e12307797e338157105bdfb6934e
root@shlava:~# docker logs 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26
hi there
root@shlava:~# docker exec -it 0ae2d689e63a8688213d1eaf285e555ba3d672b8953f0d2730a1897c9d648a26 /bin/sh
/ #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM