Linux 在 Docker 容器中自动启动服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18047931/
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-08-07 00:27:57  来源:igfitidea点击:

Automatically Start Services in Docker Container

linuxubuntudockerlinux-containers

提问by Pedro

I'm doing some initial tests with docker. At moment i have my images and I can put some containers running, with:

我正在用 docker 做一些初步测试。目前我有我的图像,我可以运行一些容器,使用:

docker ps

I do docker attach container_idand start apache2 service.

我做docker attach container_id并启动 apache2 服务。

Then from the main console I commit the container to the image.

然后从主控制台将容器提交到图像。

After exiting the container, if I try to start the container or try to run one new container from the committed image, the service is always stopped.

退出容器后,如果我尝试启动容器或尝试从提交的映像运行一个新容器,该服务总是停止。

How can create or restart one container with the services started, for example apache?

如何在服务启动的情况下创建或重新启动一个容器,例如 apache?

采纳答案by damick

EDIT: I've learned a lot about Docker since originally posting this answer. "Starting services automatically in Docker containers" is not a good usage pattern for Docker. Instead, use something like fleet, Kubernetes, or even Monit/SystemD/Upstart/Init.d/Cron to automatically start services that execute inside Docker containers.

编辑:自从最初发布这个答案以来,我已经学到了很多关于 Docker 的知识。“在 Docker 容器中自动启动服务”对于 Docker 来说不是一个好的使用模式。相反,使用诸如fleetKubernetes甚至Monit/SystemD/Upstart/Init.d/Cron 之类的东西来自动启动在Docker 容器内执行的服务。

ORIGINAL ANSWER: If you are starting the container with the command /bin/bash, then you can accomplish this in the manner outlined here: https://stackoverflow.com/a/19872810/2971199

原始答案:如果您使用命令启动容器/bin/bash,那么您可以按照此处概述的方式完成此操作:https: //stackoverflow.com/a/19872810/2971199

So, if you are starting the container with docker run -i -t IMAGE /bin/bashand if you want to automatically start apache2 when the container is started, edit /etc/bash.bashrcin the container and add /usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf(or whatever your apache2 start command is) to a newline at the end of the file.

因此,如果您正在启动容器,docker run -i -t IMAGE /bin/bash并且希望在容器启动时自动启动 apache2,请在容器中进行编辑/etc/bash.bashrc并添加/usr/local/apache2/bin/apachectl -f /usr/local/apache2/conf/httpd.conf(或任何您的 apache2 启动命令)到文件末尾的换行符。

Save the changes to your image and restart it with docker run -i -t IMAGE /bin/bashand you will find apache2 running when you attach.

保存对图像的更改并重新启动它,docker run -i -t IMAGE /bin/bash您会在附加时发现 apache2 正在运行。

回答by qkrijger

I guess you can't. What you can do is create an image using a Dockerfile and define a CMD in that, which will be executed when the container starts. See the builder documentation for the basics (https://docs.docker.com/reference/builder/) and see Run a service automatically in a docker containerfor information on keeping your service running.

我想你不能。您可以做的是使用 Dockerfile 创建一个镜像并在其中定义一个 CMD,它将在容器启动时执行。有关基础知识,请参阅构建器文档 ( https://docs.docker.com/reference/builder/),并参阅在 docker 容器中自动运行服务以获取有关保持服务运行的信息。

You don't need to automate this using a Dockerfile. You could also create the image via a manual commit as you do, and run it command line. Then, you supply the command it should run (which is exactly what the Dockerfile CMD actually does). You can also override the Dockerfiles CMD in this way: only the latest CMD will be executed, which is the command line command if you start the container using one. The basic docker run -i -t base /bin/bashcommand from the documentation is an example. If your command becomes too long you could create a convenience script of course.

您不需要使用 Dockerfile 自动执行此操作。您还可以通过手动提交创建映像,然后在命令行中运行它。然后,您提供它应该运行的命令(这正是 Dockerfile CMD 实际执行的操作)。您也可以通过这种方式覆盖 Dockerfiles CMD:只会执行最新的 CMD,如果您使用一个来启动容器,这就是命令行命令。docker run -i -t base /bin/bash文档中的基本命令是一个示例。如果您的命令变得太长,您当然可以创建一个方便的脚本。

回答by bacongobbler

An option that you could use would to be use a process manager such as Supervisord to run multiple processes. Someone accomplished this with sshd and mongodb: https://github.com/justone/docker-mongodb

您可以使用的一个选项是使用诸如 Supervisord 之类的进程管理器来运行多个进程。有人用 sshd 和 mongodb 完成了这个:https: //github.com/justone/docker-mongodb

回答by Rodrigo Lima

By design, containers started in detached mode exit when the root process used to run the container exits. You need to start a Apache service in FOREGROUND mode.

按照设计,当用于运行容器的根进程退出时,容器以分离模式启动。您需要在 FOREGROUND 模式下启动 Apache 服务。

docker run -p 8080:80 -d ubuntu/apache apachectl -D FOREGROUND

Reference: https://docs.docker.com/engine/reference/run/#detached-vs-foreground

参考:https: //docs.docker.com/engine/reference/run/#detached-vs-foreground

回答by Grzegorz Brz?czyszczykiewicz

Try to add start script to entrypoint in dockerfile like this;

尝试像这样将启动脚本添加到 dockerfile 中的入口点;

ENTRYPOINT service apache2 restart && bash