如何在 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.
来源
相关文章