如何在 docker 上运行我的 python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47202705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run my python script on docker?
提问by Pulkit
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:
我正在尝试在 docker 上运行我的 python 脚本。我尝试了不同的方法来做到这一点,但无法在 docker 上运行它。我的python脚本如下:
import os
print ('hello')
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.
我已经在我的 mac 上安装了 docker。但我想知道如何制作图像,然后将其推送到 docker,之后我想在 docker 上拉取并运行我的脚本。
采纳答案by samprog
Alright, first create a specific project directory for your docker image. For example:
好的,首先为您的 docker 镜像创建一个特定的项目目录。例如:
mkdir /home/pi/Desktop/teasr/capturing
Copy your dockerfile and script in there and change the current context to this directory.
将您的 dockerfile 和脚本复制到那里,并将当前上下文更改为此目录。
cp /home/pi/Desktop/teasr/capturing.py /home/pi/Desktop/teasr/dockerfile /home/pi/Desktop/teasr/capturing/
cd /home/pi/Desktop/teasr/capturing
This is for best practice, as the first thing the docker-engine does on build, is read the whole current context.
这是最佳实践,因为 docker-engine 在构建时所做的第一件事就是读取整个当前上下文。
Next we'll take a look at your dockerfile. It should look something like this now:
接下来,我们将查看您的 dockerfile。它现在应该是这样的:
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 disencouraged.
您需要做的下一件事是使用智能名称构建它。通常不鼓励使用点。
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 insidethe container and will probably exit upon completion.
该脚本现在在容器内执行,完成后可能会退出。
Editafter finding the problem that created the following error:
找到导致以下错误的问题后进行编辑:
"standard_init_linux.go:195: exec user process caused "exec format error"
"standard_init_linux.go:195: exec user process caused "exec format error"
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.
在 raspberry pi 下有一个不同的架构(ARM 而不是 x86_64),这可能是问题所在,但不是。如果那是问题所在,那么将父图像切换为FROM armhf/python
就足够了。
BUT! The error kept occurring.
但!错误不断发生。
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.
所以这个问题的解决方案是在python脚本之上简单地缺少Sha-Bang。脚本中的第一行必须是#!/usr/bin/env python
,这应该可以解决问题。
回答by samprog
You need to create a dockerfile in the directory your script is in.
您需要在脚本所在的目录中创建一个 dockerfile。
You can take this template:
你可以拿这个模板:
FROM python:latest
COPY scriptname.py /usr/local/share/
CMD ["scriptname.py", "-flag"]
Then simply execute docker build -t pulkit/scriptname:1.0 .
and your image should be created.
然后只需执行即可docker build -t pulkit/scriptname:1.0 .
创建您的图像。
Your image should be visible under docker images
. If you want to execute it on your local computer, use docker run
.
您的图像应该在 下可见docker images
。如果要在本地计算机上执行它,请使用docker run
.
If you want it to upload to the DockerHub, you need to log into the DockerHub with docker login
, then upload the image with docker push
.
如果要上传到DockerHub,需要用 登录DockerHub docker login
,然后用 上传镜像docker push
。
回答by Nitendra
I Followed @samprog (most accepted) answer on my machine running on UBUNTU VERSION="14.04.6".
and was getting "standard_init_linux.go:195: exec user process caused "exec format error"
我在运行 UBUNTU VERSION="14.04.6" 的机器上关注了@samprog(最被接受的)答案。并且得到"standard_init_linux.go:195: exec user process caused "exec format error"
None of the solution worked for me mentioned above.
上面提到的任何解决方案都不适合我。
Fixed the error after changing my Dockerfile as follows
修正了我的 Dockerfile 更改后的错误如下
FROMpython:latest
来自蟒蛇:最新
COPYcapturing.py ./capturing.py
复制capture.py ./capturing.py
CMD["python","/capturing.py"]
CMD["python","/capturing.py"]
Note:If your script import some other module then you need to modify COPY statement in your Dockerfile as follows - COPY*.py ./
注意:如果您的脚本导入了一些其他模块,那么您需要按如下方式修改 Dockerfile 中的 COPY 语句 - COPY*.py ./
Hope this will be useful for others.
希望这对其他人有用。
回答by anne
Another way to run python script on docker can be:
copy the local python script to docker:
在 docker 上运行 python 脚本的另一种方法可以是:
将本地 python 脚本复制到 docker:
docker cp yourlocalscript.path container_id:/dst_path/
container id can be found using:
可以使用以下方法找到容器 ID:
docker ps
run the python script on docker:
在 docker 上运行 python 脚本:
docker exec -it python /container_script_path.py