Docker -如何插入[重复]

56lgkhnf  于 2022-12-22  发布在  Docker
关注(0)|答案(1)|浏览(77)
    • 此问题在此处已有答案**:

Use environment variables in CMD(4个答案)
昨天关门了。
我有下面的Dockerfile,我需要插入要运行的文件名。

ARG PYTHON_VERSION=3.7-alpine

# Use the python image as the base image
FROM python:${PYTHON_VERSION}

# set environment to run
ENV ENVIRONMENT={ENVIRONMENT:-"prod"}

# Copy the python files
COPY requirements.txt /app/
COPY prod.py dev.py requirements.txt /app/

# Set the working directory 
WORKDIR /app

# Install the dependencies 
RUN pip install -r requirements.txt

# Set the command to run the python file when the container is started
CMD ["python", "${ENVIRONMENT}.py"]

但是当我构建映像并运行它时,我得到了以下错误:
python:无法打开文件"${ENVIRONMENT}. py":[Errno 2]没有此类文件或目录
为什么这个插补不正确?有人能帮我吗?

fnx2tebb

fnx2tebb1#

我认为最好的办法是使用run.py这样的函数来读取$ENVIRONMENT变量,然后加载正确的文件,或者,根据dev.pyprod.py的不同程度,它们甚至可以是一个文件。

相关问题