将参数传递给 docker-compose 命令
Pass parameters to docker-compose command
我有Docker文件:
FROM python:3.8
ARG CMD_ARGS=$CMD_ARGS
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /app
COPY . /app
WORKDIR /app
和Docker-compose.yml文件:
version: '3'
services:
selenium:
image: selenium/standalone-chrome
ports:
- 4444:4444
restart: always
app:
build:
context: .
volumes:
- .:/app
command: sh -c "python3 my_script.py -arg1 -arg2"
depends_on:
- selenium
有没有办法不在 docker-compose.yml 文件中对 arg1、arg2 进行硬编码,而是将其传递到 docker-compore up 命令或使用环境变量(包含空格的参数字符)不知何故?
您可以尝试 Variable substitution
处理您的情况。
来自文档的示例:
db:
image: "postgres:${POSTGRES_VERSION}"
When you run docker-compose up with this configuration, Compose looks
for the POSTGRES_VERSION environment variable in the shell and
substitutes its value in. For this example, Compose resolves the image
to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an
empty string. In the example above, if POSTGRES_VERSION is not set,
the value for the image option is postgres:.
或者将变量传递给 command
您可以使用 $$
.
You can use a $$ (double-dollar sign) when your configuration needs a
literal dollar sign. This also prevents Compose from interpolating a
value, so a $$ allows you to refer to environment variables that you
don’t want processed by Compose.
示例:
app:
command: sh -c "python3 my_script.py $$VAR_ARG_1"
并且在 cli 上,您需要设置 VAR_ARG_1
变量。
我有Docker文件:
FROM python:3.8
ARG CMD_ARGS=$CMD_ARGS
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /app
COPY . /app
WORKDIR /app
和Docker-compose.yml文件:
version: '3'
services:
selenium:
image: selenium/standalone-chrome
ports:
- 4444:4444
restart: always
app:
build:
context: .
volumes:
- .:/app
command: sh -c "python3 my_script.py -arg1 -arg2"
depends_on:
- selenium
有没有办法不在 docker-compose.yml 文件中对 arg1、arg2 进行硬编码,而是将其传递到 docker-compore up 命令或使用环境变量(包含空格的参数字符)不知何故?
您可以尝试 Variable substitution
处理您的情况。
来自文档的示例:
db:
image: "postgres:${POSTGRES_VERSION}"
When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.
If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.
或者将变量传递给 command
您可以使用 $$
.
You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don’t want processed by Compose.
示例:
app:
command: sh -c "python3 my_script.py $$VAR_ARG_1"
并且在 cli 上,您需要设置 VAR_ARG_1
变量。