Docker刚启动就退出 [英] Docker getting exited just after start
问题描述
所以在开始问题之前,这里是我对docker的理解。
So before starting the question here are my understanding for the docker.
- Docker有3个组件
- 图片
- 容器
- Dockerfile
- Docker has 3 components
- Images
- Containers
- Dockerfile
现在映像是在其上创建容器的映像,而Dockerfile就像一个流程。用简单的话来说,图像是类和容器是图像的对象。
Now Images are the ones on which the Containers are created and Dockerfile is like a flow what to do. In simple words Images are Classes and Containers are Objects of Images.
现在我不想接受 Dockerfile
的方法,在其中指定创建容器时要执行的步骤。
Now I don't want to take the approach of the Dockerfile
where you specify the steps to perform while creating the container.
我想在Linux上安装一些基本实体,例如MongoDb,Redis等,并在它们之上运行我的服务器。
I want to install some of the basic entities over Linux like MongoDb,Redis etc and run my server over them.
所以我是这样开始的:
-
我通过
docker从Docker Hub下载了Ubuntu镜像pull ubuntu
返回了我18261df960118..7a16(大十六进制键)
现在,我必须为此图像创建一个容器,以实现该操作:
Now I have to create a container for this image, to achieve that I did:
docker create -h abc。 com --name abc.com 18261df960118..7a16
这返回了我的ID
- 要进入容器,我必须先启动它,然后将其连接,因此,这里是命令
docker start containerId
,后跟docker attach containerId
。
- In order to go into the container I have to first start it and then get attached to it, so for that here are the commands
docker start containerId
followed bydocker attach containerId
.
但是每次都在说:
您无法连接到已停止的容器,请先启动它。
You cannot attach to a stopped container, start it first.
请帮助我做错了的地方,这可能是新手对于很多人来说,这是一个问题,但是我在这里陷入困境,请不要为同样的问题而投票。
Please help where I am doing wrong, this might be a novice question for many but I am getting stuck here, please don't downvote for the same.
预先感谢。
推荐答案
编辑:
在我的原始帖子中,我提到:尝试像虚拟机一样思考 。我最近遇到了此,它表示不这样做:
In my original post I mention: "try to think like with VMs". I recently fell into this, which says not to do so:
停止 将容器视为小型VM,而是开始将其视为。
也,值得一读的文章:容器不是虚拟机
also, worth-reading article: Containers are not VMs
原始帖子:
Docker容器的逻辑是它们应该已启动并运行。如果该服务停止,则它们退出并进入已停止状态。 (随着对Docker的更多了解,您将了解它的工作原理,并且能够使用 ENTRYPOINT
和 CMD
)。但是,让我们跳过一会儿,尝试像使用虚拟机一样思考,运行一个新容器并进入其中键入一些命令...
The logic with Docker containers is that they are supposed to have a service up and running. If this service stops, they exit and go to "stopped" state. (As you learn more about Docker, you will understand how this works and you will be able to use ENTRYPOINT
and CMD
). But let's skip this for a while and try to think like with VMs, run a new container and get inside to type some commands...
docker container create -it --name test ubuntu
445cad0a3afea97494635361316e5869ad3b9ededdd6db46d2c86b4c1461fb75
$ docker container start test
test
$ docker container exec -it test bash
root@445cad0a3afe:/# your are inside, you can type your commands here!
为什么您的失败...
创建容器时,您没有使用 -i
标志,该标志有助于即使未连接STIND也保持打开状态
。这实际上意味着在容器启动时,它将使用官方ubuntu bash
,然后立即退出。
why yours failed...
when you created the container, you didn't use the -i
flag which helps Keep STDIN open even if not attached
. This practically means that when the container starts, it uses the CMD
set in the official ubuntu Dockerfile, which is bash
and then exits immediately.
您可以使用 nginx
这样的图像对其进行测试。如果运行新的nginx容器并尝试将其附加到该容器,则会看到nginx的日志已被打印出,并且您无法在shell中键入任何命令。发生这种情况的原因是图像的CMD如下:
You can test this with an image like nginx
. If you run a new nginx container and try to attach to it, you will see that logs from nginx are being printed out and you are not able to type any command in the shell. This happens because the CMD of the image is the following:
# Define default command.
CMD ["nginx"]
要能够附加至类似容器而且还能够使用外壳程序(其他人可能还会提到这一点,例如对容器执行与 ssh
等效的操作),您将必须运行:
To be able to "attach" to a container like that but also be able to use the shell (some others may also mention this like doing something equivalent to ssh
to the container), you will have to run:
docker container exec -it your_container_name bash
我建议您还阅读:
〜jpetazzo:如果您在Docker容器中运行SSHD,那么您做错了!
这篇关于Docker刚启动就退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!