Docker容器未启动,显示为“ n秒钟前退出” [英] Docker container doesn't start, showing as 'Exited n seconds ago'

查看:191
本文介绍了Docker容器未启动,显示为“ n秒钟前退出”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解docker在启动/进入容器时的工作方式。我对此很陌生,请耐心等待。我尝试在SO中找到我需要的答案,但到目前为止还没有找到。

I am trying to understand how docker works when it comes to starting / getting into a container. I am new to this so bear with me. I tried finding the answer to my need in SO but could not so far.

我正在尝试实现3个基本目标:

I am trying to achieve 3 basic things:


  • 启动容器

  • 确保所有端口都映射到我的容器(即,我可以从容器外部看到我的容器的apache)

  • SSH或将控制台插入我的容器

这是我用来启动容器的内容:

Here is what I am using to start a container:

caldav@caldav:~/docker/caldav-server$ sudo docker run -it -d -P  caldav-server service apache2 start

48fbea1865e302768a863767bf01b08f35f0221a4b29e5f2208d57e54660ef42

48fbea1865e302768a863767bf01b08f35f0221a4b29e5f2208d57e54660ef42

但是当我运行ps时,我将其视为 n秒前退出:

But when I run ps, I see it as 'exited n seconds ago':

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
48fbea1865e3        caldav-server       "service apache2 sta   2 seconds ago       Exited (0) 1 seconds ago 

这甚至意味着什么?显然,当我进入 http:// [IP_ADDRESS]

What does this even mean? And obviously, I dont get to see it up and running when I go to http://[IP_ADDRESS]

更新:好的,当我删除apache start命令时,它似乎仍然存在:

UPDATE: Ok, when I remove the apache start command it seems to stay up:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
20d5daca6237        caldav-server       "/bin/bash"         10 seconds ago      Up 9 seconds                            thirsty_ritchie     

但是后来我似乎无法将控制台加入其中。这是我尝试的方法:

But then I dont seem to be able to get console into it. This is what I tried:

docker attach 20d5daca6237

哪个还可以(允许我使用bash)。但是,当我尝试通过键入退出

Which is fine (gives me bash access). However, when I try to exit it by typing

$ exit

然后将容器显示为ps中的退出状态。我做错了吗?

it then shows the container as the 'exited' status in ps. Am I doing it wrong?

推荐答案

Docker容器仅在其初始命令运行的情况下运行。在这种情况下,该命令是 service apache2 start ,该命令在不超过几秒钟的时间内退出,此时容器也将停止。如果要在容器中运行守护程序,则需要使该守护程序成为主程序并使使其在前台运行,或者使用带有可处理守护程序的命令的映像,例如< a href = https://github.com/phusion/baseimage-docker rel = nofollow> phusion / baseimage

A Docker container runs only as long as its initial command does. In this case, that command is service apache2 start, which exits after no more than a couple seconds, at which point the container stops as well. If you want to run a daemon in a container, you need to either make the daemon the primary program and make it run in the foreground or else use an image with a command that can handle daemons, like phusion/baseimage.

这篇关于Docker容器未启动,显示为“ n秒钟前退出”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆