我在DAG中使用DockerOperator
的Apache Airflow。使用旧的Windows Subsystem for Linux后端(WSL1),我将卷提供给DockerOperator
如下:
docker_op = DockerOperator(
command=cmd,
task_id=task_id,
image="{}/{}:{}".format(self.docker_server, self.docker_repo_name, self.docker_image_tag),
api_version="auto",
auto_remove=True,
network_mode=self.docker_network,
force_pull=False,
volumes=self.docker_volumes,
dag=self.dag
)
其中self.docker_volumes = ['/c/Users/kevin/dev/myproject/app:/app']
不幸的是,当Airflow试图执行docker_op
时,出现以下错误:
python: can't open file '/app/main.py': [Errno 2] No such file or directory
因此,似乎该卷未正确安装。在旧的WSL1后端上,它确实起作用了,因为DockerOperator
创建的容器被构建为“同级”容器,因此与主机(在本例中为笔记本电脑)共享相同的docker deamon。在阅读this和this文章之后,我还尝试过调整卷的主机端路径,例如self.docker_volumes = ['/mnt/c/Users/kevin/dev/myproject/app:/app']
,但这也给我带来了同样的错误。
所以,那么我认为这个问题通常与WSL2后端有关。为了验证这种想法,我尝试了一个非常简单的工作流程:我创建了一个文件main.py
,内容如下:
print('Hello from inside the container!!!')
我将该文件放在C://Users/kevin/dev/wsl2test/main.py
处,并从一个简单的Windows Powershell执行以下命令:
docker run -v /c/Users/kevin/ks_dev/wsl2test:/wsl2test python:3 python /wsl2test/main.py
结果是:
Hello from inside the container!!!
我还从WSL2内部运行的Ubuntu外壳执行了类似的命令:
docker run -v /mnt/c/Users/kevin/ks_dev/wsl2test:/wsl2test python:3 python /wsl2test/main.py
结果也是:
Hello from inside the container!!!
因此,似乎通常可以通过指定卷的正确主机路径将文件安装到具有WSL2后端的Docker容器中。然后,自然的问题是:为什么这与DockerOperator
的Apache气流不兼容?