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')

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.

解决方案

Alright, first create a specific project directory for your docker image. For example:

mkdir /home/pi/Desktop/teasr/capturing

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

This is for best practice, as the first thing the docker-engine does on build, is read the whole current context.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值