bash 如何在不进入容器的情况下运行docker容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43493478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:02:27  来源:igfitidea点击:

How can I run docker container without entering into container

bashdocker

提问by nick_gabpe

I have Dockefile

我有 Dockefile

FROM centos:7

So I have no entrypoint in dockerfile. Then I build it to image

所以我在 dockerfile 中没有入口点。然后我将它构建为图像

sudo docker build -t my_container .

Then I start it.

然后我开始。

sudo docker run -t my_container

And I get open tty to container

我打开 tty 到容器

root@my_container_id/

If I start it without -tit stopped immidiately after start. How can I run docker container without start tty and without entrypoint?

如果我在没有它的情况下启动它,-t它会在启动后立即停止。如何在没有启动 tty 和入口点的情况下运行 docker 容器?

回答by naimdjon

You can start your container in a detachedmode:

您可以在分离模式下启动容器:

docker run -it -d my_container

The -doption here means your container will run in "detached" mode, in the background.

-d此处的选项意味着您的容器将在后台以“分离”模式运行。

If you want to attachthe container and drop to a shell, you can use:

如果要附加容器并放到外壳上,可以使用:

docker exec -it my_container /bin/bash

Note, if your container is based on an alpine image, you need to use sh, i.e.:

请注意,如果您的容器基于 alpine 映像,则需要使用sh,即:

docker exec -it my_container /bin/sh 

回答by kstromeiraos

You can't do that. Your container lives if its main process is running, so you have to have a main process which is the process with PID 1 inside your container, and your container will be up if that process is running.

你不能那样做。如果主进程正在运行,您的容器就会存活,因此您必须有一个主进程,它是容器内 PID 为 1 的进程,如果该进程正在运行,您的容器就会启动。