将字符串的 CLA 传递到 Docker 图像中 - Python
Pass CLA of a String into a Docker Image - Python
我想将一个字符串传递到命令行参数中,以便在启动 docker 容器时在 python 文件中设置 class。我正在使用 python:3.9.
的基本图像
构建 docker 容器时如何传递字符串的 CLA
Python 脚本
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Pass the variable needed')
parser.add_argument('var', type=str, help='variable')
args = parser.parse_args()
ENVPath = '/usr/local/bin/chromedriver'
if sys.argv[1] == 'prod':
currentvar = TestPrerequisiteService(ENVPath, prod)
elif sys.argv[1] == 'test':
currentvar = TestPrerequisiteService(ENVPath, test)
else:
print("Invalid variable")
exit()
Docker 文件
FROM python:3.9
ADD /app/main.py .
ADD /app/Connect.py .
# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
rm /tmp/chromedriver_linux64.zip && \
chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver
# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
apt-get -yqq update && \
apt-get -yqq install google-chrome-stable && \
rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install -r requirements.txt
WORKDIR /microservices
COPY ./app ./app
CMD ["python","./app/main.py"]
一些帮助将不胜感激:)
CMD 定义传递给容器入口点的参数。我相信您容器的当前 ENTRYPOINT 是 /bin/sh
,它以 ["python","./app/main.py"]
作为参数。当您 运行 带有 docker run -p 5000:5000 microservicestesting prod
的容器时,prod
会用 CMD 覆盖 docker 文件中定义的参数。
要获得您想要的功能,请将 CMD 更改为 ENTRYPOINT,然后当您 运行 时 docker 运行 命令 prod
将作为参数传递给 ["python","./app/main.py"]
我想将一个字符串传递到命令行参数中,以便在启动 docker 容器时在 python 文件中设置 class。我正在使用 python:3.9.
的基本图像构建 docker 容器时如何传递字符串的 CLA
Python 脚本
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Pass the variable needed')
parser.add_argument('var', type=str, help='variable')
args = parser.parse_args()
ENVPath = '/usr/local/bin/chromedriver'
if sys.argv[1] == 'prod':
currentvar = TestPrerequisiteService(ENVPath, prod)
elif sys.argv[1] == 'test':
currentvar = TestPrerequisiteService(ENVPath, test)
else:
print("Invalid variable")
exit()
Docker 文件
FROM python:3.9
ADD /app/main.py .
ADD /app/Connect.py .
# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
rm /tmp/chromedriver_linux64.zip && \
chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver
# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
apt-get -yqq update && \
apt-get -yqq install google-chrome-stable && \
rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install -r requirements.txt
WORKDIR /microservices
COPY ./app ./app
CMD ["python","./app/main.py"]
一些帮助将不胜感激:)
CMD 定义传递给容器入口点的参数。我相信您容器的当前 ENTRYPOINT 是 /bin/sh
,它以 ["python","./app/main.py"]
作为参数。当您 运行 带有 docker run -p 5000:5000 microservicestesting prod
的容器时,prod
会用 CMD 覆盖 docker 文件中定义的参数。
要获得您想要的功能,请将 CMD 更改为 ENTRYPOINT,然后当您 运行 时 docker 运行 命令 prod
将作为参数传递给 ["python","./app/main.py"]