简体   繁体   English

在后台运行 ubuntu docker 图像

[英]Running ubuntu docker image in background

I was trying to run ubuntu docker image in background.我试图在后台运行 ubuntu docker 图像。 So I tried below command所以我尝试了下面的命令

sudo docker container run -d --name my-ubuntu-container ubuntu:latest

But this command do not run the container in background.但是这个命令不会在后台运行容器。 In fact the status becomes "EXITED" on checking using实际上,在使用检查时状态变为“已退出”

docker container ls -a

But if I add "-it" flag in above command.但是如果我在上面的命令中添加“-it”标志。 Then the container runs in background.然后容器在后台运行。

sudo docker container run -itd --name my-ubuntu-container ubuntu:latest

Now on checking in docker container ls -a .现在检查docker container ls -a We see the status now in "UP" and it runs in background.我们现在在“UP”中看到状态,它在后台运行。

Anyone can please advise why adding "-it" flag in above command along with "-d" runs the ubuntu docker image in background?任何人都可以请告知为什么在上面的命令中添加“-it”标志以及“-d”会在后台运行 ubuntu docker 图像?

First of all, you should avoid runing docker command with "sudo".首先,您应该避免使用“sudo”运行 docker 命令。 Instead, add your current user to the docker group in your linux OS.相反,将您的当前用户添加到 linux 操作系统中的 docker 组。

Also, you could do docker container logs <container_id> to see if there were eventual errors.此外,您可以执行docker container logs <container_id>以查看是否存在最终错误。

As stated in that other post answer: https://stackoverflow.com/a/48368594/2409641如其他帖子回答中所述: https://stackoverflow.com/a/48368594/2409641

-it is short for --interactive + --tty when you docker run with this command.. it would take you straight inside of the container,, where -d is short for --detach which means you just run the container and then detach from it so basically you run container in the background.. edit: so if you run docker container with-itd it would run the-it options and detach you from the container, so your container still running in the background even without any default app to run.. - 当您使用此命令运行 docker 时,它是 --interactive + --tty 的缩写。它会带您直接进入容器,其中 -d 是 --detach 的缩写,这意味着您只需运行容器,然后与它分离,所以基本上你在后台运行容器.. 编辑:所以如果你运行带有-itd 的 docker 容器,它将运行 -it 选项并将你从容器中分离出来,所以即使没有任何默认值,你的容器仍然在后台运行应用程序运行..

You didn't specify a command so the container runs with the default command bash .您没有指定命令,因此容器使用默认命令bash运行。 If you run bash interactively ( -ti ) it stays open, but otherwise it quits immediately.如果您以交互方式运行 bash ( -ti ),它会保持打开状态,否则会立即退出。 If you want to run a container detached in the backgroud you can change the command from bash to a command that blocks - for example tail -f /dev/null .如果您想在后台运行分离的容器,您可以将命令从 bash 更改为阻塞命令 - 例如tail -f /dev/null

You can run the following command and the container won't exit immediately.您可以运行以下命令,容器不会立即退出。

sudo docker container run -d --name my-ubuntu-container ubuntu:latest tail -f /dev/null

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

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