如何在docker上运行我的python脚本? [英] How to run my python script on docker?

查看:696
本文介绍了如何在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-engine在构建时会读取整个当前上下文。

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 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 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.

来源

但是!错误不断发生。

因此,解决此问题的方法是简单地缺少S ha-Bang位于python脚本之上。脚本的第一行必须是#!/ 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脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆