我的docker-compose.yml
中有两项服务。使用MailHog和MongoDB进行存储的邮件服务。
问题在于,在MailHog之前,MongoDB服务需要启动并运行。否则,MailHog将回退并使用其内存存储。
简单的depends_on
是不够的,因为MongoDB服务需要一些时间才能启动。
[我知道wait-for-it
等脚本,但是它们都需要修改Dockerfile
,在本例中,我使用的是未经修改的MailHog Docker镜像。
是否有任何“内置”机制或解决方法,可以在MongoDB准备就绪之前将邮件服务延迟?
mail:
image: mailhog/mailhog:v1.0.0
deploy:
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 60s
mail-db:
image: mongo:4.2.6
environment:
MONGO_INITDB_DATABASE: mailhog
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017
deploy:
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.5"
memory: 500M
一种方法是提供自己的entrypoint
脚本,您可以将其与volumes
添加到容器中。在脚本中,等待与MongoDB的成功连接,然后执行原始入口点。
堆栈:
volumes:
- /path/to/entrypoint.sh:/tmp/entrypoint.sh
entrypoint: /bin/bash
command: /tmp/entrypoint.sh
entrypoint.sh:
# Wait for service or whatever
exec /path/to/original/entrypoint
不,没有。但是,无需修改原始Dockerfile。您可以使用jwilder/dockerize(为此目的开发的工具(以及其他工具))extend。
FROM mailhog/mailhog:v1.0.0 # If required, you can change MONGOURL via docker -e [...] ENV MONGOURL mail-db:27017 # The dockerize version used. You can set a different version with # docker build --build-arg DOCKERIZE_VERSION=[...] ARG DOCKERIZE_VERSION=v0.6.1 # Change to root to be able to install dockerize USER root # 1: Ensure the image is up to date, while we are at it RUN apk update && apk upgrade \ # 2: Install curl and its dependencies as the virtual package ".deps" && apk add --virtual .deps curl \ # 3: Get dockerize && curl -L -O https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \ # 4: Unpack it and put it to the appropriate location as per FHS && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \ # 5: Remove the tarball && rm dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \ # 6: Cleanup && rm -rf /var/cache/apk/* \ # 7: Remove the virtual package ".deps" && apk del .deps # Switch back to the user mailhog is supposed to run under USER mailhog # Run dockerize, which will start mailhog as soon as it was able to connect to $MONGOURL ENTRYPOINT ["/bin/sh","-c","/usr/local/bin/dockerize -wait tcp://$MONGOURL MailHog"]
注意:无论出于何种原因,语法突出显示在Dockerfile上均无法正常工作
[使用以下docker-compose.yaml测试(deploy
部分显然被docker-compose忽略了:]
version: "3"
services:
mail:
image: robertstauch/mailhog:v1.0.0-dockerized-v0.6.1
build: .
ports:
- "8025:8025"
- "1025:1025"
deploy:
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 60s
mail-db:
image: mongo:4.2.6
environment:
MONGO_INITDB_DATABASE: mailhog
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
deploy:
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.5"
memory: 500M