如何将命令行参数传递给Python Docker容器
在谈论Docker容器的参数之前,我们必须了解Python命令行参数及开发者如何访问它们。当我们希望在程序外部控制我们的Python脚本时,命令行参数非常有用。
访问Python脚本的命令行参数
步骤1:创建一个名为main.py的Python脚本
示例
# sys will allow us to access the passed arguments
import sys
# sys.argv[0] access the first argument passed that is the python script name
print("\nFile or Script Name is :", sys.argv[0])
# print arguments other than the file name
print("\nArguments passed:", end = " ")
for i in range(1, len(sys.argv)):
print(sys.argv[i], end = " ")
# Lowercase operation on the passed arguments
for i in range(1, len(sys.argv)):
print(sys.argv[i].lower(), end = " ")
步骤2:在终端上带参数运行
#python3 main.py HELLO THIS IS TUTORIALSPOINT
输出
File or Script Name is : main.py
Arguments passed: HELLO THIS IS TUTORIALSPOINT
hello this is tutorialspoint
将这些参数传递给Docker容器
我们有不同的方法来传递命令行参数给Docker容器。下面是一些步骤:
使用Docker参数和ENTRYPOINT
诀窍是使用entrypoints和dockerfile,并将entrypoint重定向到Python文件的执行。在运行Docker容器的过程中,只需传递所需的Python参数。
步骤1: 创建一个Dockerfile
示例
FROM python
WORKDIR /app
COPY . /app/
ENTRYPOINT ["python3", "main.py"]
步骤2:构建图像
示例
#docker build -t arg_py .
输出
Sending build context to Docker daemon 8.192kB
Step 1/4 : FROM python
---> fa9122485d1d
Step 2/4 : WORKDIR /app
---> Using cache
---> 9e9708fe1d43
Step 3/4 : COPY . /app/
---> aea9ecf32f55
Step 4/4 : ENTRYPOINT ["python3", "main.py"]
---> Running in 864440fa7988
Removing intermediate container 864440fa7988
---> d6e31e5606b8
Successfully built d6e31e5606b8
Successfully tagged arg_py:latest
步骤3:运行容器
#docker run --name mycontainer arg_py HELLO FROM TUTORIALSPOINT
示例
File or Script Name is : main.py
Arguments passed: HELLO FROM TUTORIALSPOINT
hello from tutorialspoint
使用Docker环境变量与入口点
Docker run命令提供了一些非凡的功能,其中之一就是环境变量。在这里,我们将使用这些环境变量将数据传递给Docker容器内部的Python脚本。
步骤1:创建用于访问环境变量的Python脚本
这次创建的Python脚本与第一个示例非常相似。我们将导入“os”模块而不是“sys”模块以获取环境变量。创建一个Python文件,并粘贴下面的代码。
示例
import os
#declare some variables for environment variable
#os.getenv will fetch the environment variables from the container.
userName = os.getenv("User_Name")
passWord = os.getenv("Pass_Word")
#Now print the variables that has been fetched.
print("Running with user: %s" % userName)
print("Your password: %s" % passWord)
#Apply some operation
print(userName.upper())
print(passWord.upper())
将上述文件保存为main.py
步骤2:构建dockerfile
构建dockerfile以创建一个包含此python代码的新镜像。该dockerfile与之前创建的相同,只是main.py中的python代码已更改。
示例
#docker build -t env_img .
输出
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM python
---> fa9122485d1d
Step 2/4 : WORKDIR /app
---> Using cache
---> 9e9708fe1d43
Step 3/4 : COPY . /app/
---> 31f98d53c161
Step 4/4 : ENTRYPOINT ["python3", "main.py"]
---> Running in bec1681a6842
Removing intermediate container bec1681a6842
---> 5dd89b0c7985
Successfully built 5dd89b0c7985
Successfully tagged env_img:latest
步骤3:运行容器
在运行容器时使用Python脚本中提到的环境变量。Docker run有一个“-e”标志用于指定任何环境变量,我们可以同时指定多个环境变量。
示例
#docker run -e User_Name="TutorialsPoint" -e Pass_Word="secret" --name env_cont env_img
输出
Running with user: TutorialsPoint
Your password: secret
TUTORIALSPOINT
SECRET
这是我们如何通过docker客户端将命令行参数和环境变量传递给运行在docker守护进程上的python容器。