docker-compose,容器启动后运行脚本? [英] docker-compose, run a script after container has started?

查看:192
本文介绍了docker-compose,容器启动后运行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 docker-compose 通过 Rancher 提供了一项服务.我遇到的问题是部署容器后需要设置密码.

I have a service that I am bringing up through Rancher via docker-compose. The issue I am running into is that I need to set a password after the container has been deployed.

rancher secret 的工作方式是,我设置我的 secret,rancher 将在我的容器上安装一个卷,其中包含一个包含我的 secret 的文件.我希望能够执行一个脚本来获取该秘密,并将其设置为我的配置文件中的密码.

The way rancher secrets work, is that I set my secret in and rancher will mount a volume on my container with a file containing my secret. I was hoping to be able to execute a script to grab that secret, and set it as a password on my config file.

我不相信我有办法通过 Dockerfile 获取该秘密,因为我不希望秘密存在于 git 中,所以我打算通过 docker-compose 来实现.

I don't believe I have a way to get that secret in through the Dockerfile as I don't want the secret to be in git, so I'm left looking at doing it via docker-compose.

有谁知道这是否可行?

推荐答案

诀窍是在调用原始命令之前覆盖 compose COMMAND 以执行您需要的任何初始化操作.

The trick is to overwrite the compose COMMAND to perform whatever init action you need before calling the original command.

  1. 在您的映像中添加一个脚本,该脚本将执行您想要的初始化工作,例如设置密码、更改内部配置文件等.我们称之为 init.sh.您将其添加到您的图像中.
  1. Add a script in your image that will perform the init work that you want like set password, change internal config files, etc. Let's call it init.sh. You add it to your image.

Dockerfile:

Dockerfile:

FROM: sourceimage:tag
COPY init.sh /usr/local/bin/
ENTRYPOINT []

以上内容会覆盖 sourceimage 中定义的任何 ENTRYPOINT.这是为了让这个例子更简单.确保您从 sourceimage 了解 ENTRYPOINT 在 Dockerfile 中的作用,并在 docker-compose.yml 文件的 command: 中调用它.

The above overrides whatever ENTRYPOINT is defined in the sourceimage. That's to make this example simpler. Make sure you understand what the ENTRYPOINT is doing in the Dockerfile from the sourceimage and call it in the command: of the docker-compose.yml file.

docker-compose.yml:

docker-compose.yml:

services:
  myservice:
    image: something:tag
    ...
    command: sh -c "/usr/local/bin/init.sh && exec myexecutable"

在调用主命令之前使用 exec 很重要.这会将命令安装为第一个进程 (PID1),这将使其接收 STOP、KILL(键盘上的 Ctrl-C)或 HUP 等信号.

It's important to use exec before calling the main command. That will install the command as the first process (PID1) which will make it receive signals like STOP, KILL (Ctrl-C on keyboard) or HUP.

这篇关于docker-compose,容器启动后运行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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