python - 如何在一个命令中运行容器并在 docker 上执行 python?
问题描述
我们有一个非常小的 Python 代码,需要在容器上运行。以下两个命令工作正常,我想将它们合并为一个?最终目标是容器应该启动(基于图像),运行python并自行退出。
docker run --name mycontainer -v /opt/testuser/pythoncode/:/usr/src/app/ -t -d pythonimage:latest
docker exec -it mycontainer python3 /usr/src/app/subfolder/createfile.py
我试过 -c "/bin/bash python3 /usr/src/app/subfolder/createfile.py" 这没用,它只是。
解决方案
您可以添加一个名为Dockerfile
(文件名前后没有点,正是这个)的文件,其内容如下:
from ubuntu:20.04
env run=/usr/src/app/subfolder/createfile.py
cmd ["/bin/bash", "-c", "${run}"]
然后运行:
docker build YOUR_IMAGE_NAME # this should be a new name, like pythonimage2
现在运行它:
docker run --name mycontainer -v /opt/testuser/pythoncode/:/usr/src/app/ -t -d pythonimage2:latest
所以,现在让容器运行的 docker 执行命令是 now /usr/src/app/subfolder/createfile.py
。
关于您的答案,您可以添加一个变量,每次只需修改该行。
即使您可以对您的Dockerfile
:
from ubuntu:20.04
env path=/usr/src/app/subfolder/
env file=$path/createfile.py
cmd ["/bin/bash", "-c", "${file}"]
现在你只需要修改file
变量。如果您的任何子目录发生更改,您可以将其从path
变量中删除并将其添加到file
变量中。
推荐阅读
- ios - Swift 4:无法在包中加载 NIB:自定义 UITableViewCell 的“NSBundle”(无 IB)
- node.js - 使用 Gradle 构建 NodeJS
- laravel - 在 Facade.php 第 237 行:调用未定义的方法 Illuminate\Database\Schema\MySqlBuilder::defaultStringLenght()
- node.js - 如何在 Node.js 中使用 Sequelize 运行 AND 和 Or 运算符
- c++ - 通过在后台运行另一个项目来提高 C++ 项目性能?
- video - Gstreamer - 如何使用多个 src 构建应用程序?
- typescript - 使用 Object.assign() 时如何忽略只有 getter 的属性?
- node.js - 如何在纱线中导入本地依赖项
- html - 我可以从网页上的嵌入 Excel 工作表中抓取数据以发送到我的 SQL Server 数据库吗
- python - Discord Bot 返回“HTTPException: BAD REQUEST (status code: 400): Cannot send an empty message”虽然我发送的是非空消息