简体   繁体   English

Postgres Docker容器:启动时运行命令

[英]Postgres Docker container: Run command on startup

I have following Dockerfile to create a Postgres container with a third party app: 我有以下Dockerfile使用第三方应用程序创建Postgres容器:

FROM postgres

COPY ./setup/third-party-app.deb /opt/third-party-app.deb
COPY ./setup/init_extensions-db.sql /docker-entrypoint-initdb.d/init_extensions-db.sql

RUN apt-get update -y
RUN apt-get install -y default-jre-headless
RUN dpkg -i /opt/third-party-app.deb
RUN service third-party-app init
RUN service third-party-app start

But I noticed that the service third-party-app start was successfully executed during the build phase, but than the service was not started in the running container. 但是我注意到, service third-party-app start已在构建阶段成功执行,但是该服务并未在运行的容器中启动。
I figured the reason is that Docker images don't snapshot the running process. 我发现原因是Docker映像无法快照正在运行的进程。 So I am trying to find a way to automatically execute this command after the container has started. 因此,我试图找到一种在容器启动后自动执行此命令的方法。

I tried following code at the end of the Dockerfile: 我在Dockerfile的末尾尝试了以下代码:

CMD postgres && service third-party-app start

But the container exits with error status code 1. 但是容器以错误状态代码1退出。

I also tried to copy following shell script into the docker-entrypoint-initdb.d of the Postgres image: 我还尝试将以下shell脚本复制到Postgres映像docker-entrypoint-initdb.d中:

COPY ./setup/third-party-app-start.sh /docker-entrypoint-initdb.d/third-party-app-start.sh

third-party-app-start.sh: third-party-app-start.sh:

#!/bin/bash
set -e
service third-party-app start

But the same happens. 但是,同样的事情发生了。 The container exits with error status code 1. 容器退出,错误状态代码为1。

But when I execute the command manually via PowerShell after the container has started it works perfectly fine. 但是,当我在容器启动后通过PowerShell手动执行命令时,它可以正常运行。

Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?
Or is there a way to get a detailed error log of what went wrong with that command? 还是有办法获取该命令出了什么问题的详细错误日志?

A container's main running process is the ENTRYPOINT and/or CMD at the end of the Dockerfile. 容器的主要运行过程是Dockerfile末尾的ENTRYPOINT和/或CMD。 It is generally recommended that you separate areas of concern by using one service per container. 通常建议您通过每个容器使用一项服务来分离关注区域。 That service may fork into multiple processes (for example, Apache web server starts multiple worker processes). 该服务可以派生到多个进程中(例如,Apache Web服务器启动多个工作进程)。 It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. 可以有多个进程,但是要从Docker中获得最大收益,请避免一个容器负责整个应用程序的多个方面。 You can connect multiple containers using user-defined networks and shared volumes. 您可以使用用户定义的网络和共享卷连接多个容器。

In order to achieve your goal, you will need to use a wrapper script or supervisor, more details: https://docs.docker.com/config/containers/multi-service_container/ 为了实现您的目标,您将需要使用包装脚本或主管,更多详细信息: https : //docs.docker.com/config/containers/multi-service_container/

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

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