调用时将参数传递给docker-compose yml

时间:2019-01-05 17:42:40

标签: docker docker-compose

我有一个docker-compose.ymldocker-compose up --build -d执行的命令:

(...)
command: certonly --webroot -w /var/www/certbot \
         -d test.apps.example.com

但是我想要这样的东西:

(...)
command: certonly --webroot -w /var/www/certbot \
         -d ${arg}.example.com

和:docker-compose up --build -d "test.apps"

有什么想法可以实现吗?

2 个答案:

答案 0 :(得分:0)

看起来您正在寻找构建参数:

https://docs.docker.com/compose/reference/build/

Usage: docker-compose build [options] [--build-arg key=val...] [SERVICE...]

Options:
--compress              Compress the build context using gzip.
--force-rm              Always remove intermediate containers.
--no-cache              Do not use cache when building the image.
--pull                  Always attempt to pull a newer version of the image.
-m, --memory MEM        Sets memory limit for the build container.
--build-arg key=val     Set build-time variables for services.
--parallel              Build images in parallel.

How to pass arguments within docker-compose?

答案 1 :(得分:0)

Docker-compose支持变量替换。它从您的工作外壳读取env。 https://docs.docker.com/compose/compose-file/#variable-substitution

在撰写文件中:

command: certonly --webroot -w /var/www/certbot \
         -d ${SUB_DOMAIN}.example.com

在您的shell中,导出变量:

export SUB_DOMAIN=test.apps

然后

docker-compose up --build