简体   繁体   English

如何将参数传递给 docker-compose

[英]How to pass argument to docker-compose

import argparse
parser = argparse.ArgumentParser()

parser.add_argument(
    '--strat', 
    type=str, 
)
args = parser.parse_args()
strat = args.strat

I would like to right my docker-compose.yml file such as I would just pass my argument from there.我想纠正我的 docker-compose.yml 文件,例如我只是从那里传递我的论点。 I did我做了

version: "3.3"
services:
  mm:
    container_name: mm
    stdin_open: true
    build: .
       context: .
       dockerfile: Dockerfile
       args:
           strat: 1

and my docker file和我的 docker 文件

FROM python:3.10.7

COPY . .

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

CMD python3 main.py

But it does not work.但它不起作用。

Any idea what I should change pelase?知道我应该改变什么吗?

You need to update docker file to process the build arguments .您需要更新 docker 文件以处理构建 arguments Try something like the following:尝试以下操作:

ARG strat

# ...

CMD python3 main.py --strat=$strat

The args thingy is for "build time" only. args thingy 仅用于“构建时间”。 If you want to run your already built image with different arguments, use environment variables or just pass them as you would with a regular binary.如果您想使用不同的 arguments 运行已构建的映像,请使用环境变量或像使用常规二进制文件一样传递它们。

like: docker compose run backend./manage.py makemigrations here you see that the ./manage.py and makemigrations are two arguments passed to backend service defined in docker-compose.yml像: docker compose run backend./manage.py makemigrations在这里你看到./manage.pymakemigrations是两个 arguments 传递给 docker-compose.yml 中定义的backend服务

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

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