How to run my python script on docker?(如何在 docker 上运行我的 python 脚本?)
问题描述
我正在尝试在 docker 上运行我的 python 脚本.我尝试了不同的方法来做到这一点,但无法在 docker 上运行它.我的python脚本如下:
I am trying to run my python script on docker. I tried different ways to do it but not able to run it on docker. My python script is given below:
import os
print ('hello')
我已经在我的 Mac 上安装了 docker.但是我想知道如何制作图像然后将其推送到 docker 之后我想在 docker 本身上拉并运行我的脚本.
I have already installed docker on my mac. But i want to know how i can make images and then push it to docker after that i wanna pull and run my script on docker itself.
推荐答案
好了,先为你的docker镜像创建一个具体的项目目录.例如:
Alright, first create a specific project directory for your docker image. For example:
mkdir /home/pi/Desktop/teasr/capturing
在那里复制你的 dockerfile 和脚本并将当前上下文更改为该目录.
Copy your dockerfile and script in there and change the current context to this directory.
cp /home/pi/Desktop/teasr/capturing.py /home/pi/Desktop/teasr/dockerfile /home/pi/Desktop/teasr/capturing/
cd /home/pi/Desktop/teasr/capturing
这是为了最佳实践,因为 docker 引擎在构建时所做的第一件事就是读取整个当前上下文.
This is for best practice, as the first thing the docker-engine does on build, is read the whole current context.
接下来我们将查看您的 dockerfile.它现在应该是这样的:
Next we'll take a look at your dockerfile. It should look something like this now:
FROM python:latest
WORKDIR /usr/local/bin
COPY capturing.py .
CMD ["capturing.py", "-OPTIONAL_FLAG"]
接下来你需要做的是用一个聪明的名字来构建它.通常不鼓励使用点.
The next thing you need to do is build it with a smart name. Using dots is generally discouraged.
docker build -t pulkit/capturing:1.0 .
接下来就是像以前一样运行映像.
Next thing is to just run the image like you've done.
docker run -ti --name capturing pulkit/capturing:1.0
脚本现在在容器内执行,完成后可能会退出.
The script now get executed inside the container and will probably exit upon completion.
编辑后发现导致以下错误的问题:
Edit after finding the problem that created the following error:
standard_init_linux.go:195: exec 用户进程导致exec 格式错误"
树莓派下面有一个不同的架构(ARM 而不是 x86_64),这可能是问题所在,但不是.如果这是问题所在,将父图像切换到 FROM armhf/python
就足够了.
There's a different architecture beneath raspberry pi's (ARM instead of x86_64), which COULD'VE BEEN the problem, but wasn't. If that would've been the problem, a switch of the parent image to FROM armhf/python
would've been enough.
来源
但是!错误不断发生.
所以这个问题的解决方案是在 python 脚本之上简单地缺少 Sha-Bang.脚本中的第一行需要是 #!/usr/bin/env python
应该可以解决问题.
So the solution to this problem is a simple missing Sha-Bang on top of the python script. The first line in the script needs to be #!/usr/bin/env python
and that should solve the problem.
来源
这篇关于如何在 docker 上运行我的 python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 docker 上运行我的 python 脚本?


基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01