39

enter image description hereenter image description hereI 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.

Pulkit
  • 606
  • 1
  • 9
  • 19

7 Answers7

35

Going by question title, and if one doesn't want to create docker image but just want to run a script using standard python docker images, it can run using below command

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3.7-alpine python script_to_run.py
Shambu
  • 2,612
  • 1
  • 21
  • 16
32

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

Source

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.

Source

vvvvv
  • 25,404
  • 19
  • 49
  • 81
samprog
  • 2,454
  • 1
  • 13
  • 18
  • 1
    now i am getting one new error docker: Error response from daemon: oci runtime error: container_linux.go:295: starting container process caused "exec: \"/usr/local/share/capturing.py\": permission denied". – Pulkit Nov 14 '17 at 09:14
  • I'm very sorry, I must've switched up directories... Try `/usr/local/bin` – samprog Nov 14 '17 at 09:22
  • Alright, create a directory in your dockerfile, where you can put the script and then hopefully have permission to execute it lol. I'll update the dockerfile in my answer to help you – samprog Nov 14 '17 at 09:46
  • how to build image as root? – Pulkit Nov 14 '17 at 10:38
  • Is your script executable? If not, you should change that with `chmod +x capturing.py`. – samprog Nov 14 '17 at 11:08
  • lol a got new error that standard_init_linux.go:195: exec user process caused "exec format error"... – Pulkit Nov 14 '17 at 11:30
  • This apparently is because of the underlying structure of the raspberry pi. [link](https://github.com/ethereum/go-ethereum/issues/3775). It's weird though, as the DockerHub page of the python says that ARM structures get supported – samprog Nov 14 '17 at 11:33
  • Try using the armhf/python image in the `FROM` tag – samprog Nov 14 '17 at 11:35
  • What the hell. I get the same error, but I have a x86_64 architecture. I'll see if I can find a solution, but at the moment I'm pretty much clueless on why it won't work^^ – samprog Nov 14 '17 at 12:53
  • Alright, I found the error. You need to start off your script with a Sha-Bang. For python it should be `#!/usr/local/bin/python`. – samprog Nov 14 '17 at 12:58
  • does it works for u? because for me i am having same error. – Pulkit Nov 15 '17 at 05:50
  • First off, the Shebang I told you is wrong. For python you should use `#!/usr/bin/env python` or `#!/usr/bin/env python3`. [Source](https://stackoverflow.com/questions/17846908/proper-shebang-for-python-script). Second, what image did you use? The official python one should be applicable to your infrastructure as well and it works on my side. Didn't get it to work on `armhf/python` though... – samprog Nov 15 '17 at 07:34
  • Oh and I get the `exec format error` on the `armf/python` no matter what command it should execute. Try to start a container with bash running? Nope, `exec format error`. Basically that image is broken lol – samprog Nov 15 '17 at 07:38
  • what to do next to run this on docker? – Pulkit Nov 15 '17 at 08:05
  • Do you _need_ the `armhf/python` image or does the official one work? If you _need_ a different image, we need to search for a new image that supports your infrastructure and has python installed. – samprog Nov 15 '17 at 08:06
  • Try `resin/raspberry-pi-python`, `jaymoulin/rpi-python` or `jbrisbin/rpi-python3` as parentimage. Haven't tested them, but they look _somewhat_ up-to-date. – samprog Nov 15 '17 at 08:47
  • i am trying all these and let u know soon – Pulkit Nov 15 '17 at 11:14
  • resin/raspberry-pi-python is working but i am having new errors . i am adding terminal image. – Pulkit Nov 17 '17 at 05:15
  • Now my image not able to import the modules in my script. `from time import sleep import datetime import os import shutil import cv2 import io import numpy as np import glob from threading import Thread import urllib2 import requests import json import boto3 opvideo = str(datetime.datetime.now().time().microsecond)` – Pulkit Nov 17 '17 at 07:13
  • Alright, sadly I can't help you with python errors, as I have close to no understanding of that^^ I suggest you open a new question with the new errors – samprog Nov 17 '17 at 07:54
8

You need to create a dockerfile in the directory your script is in.

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.

Your image should be visible under docker images. If you want to execute it on your local computer, use 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.

samprog
  • 2,454
  • 1
  • 13
  • 18
  • what is /usr/local/share ? should i change it according to path? or make it run as it is? – Pulkit Nov 14 '17 at 06:10
  • Just put it somewhere, where the `PATH` variable can reach it, so your script can get executed – samprog Nov 14 '17 at 06:37
  • 1
    i am getting this error whenever i run my docker : container_linux.go:295: starting container process caused "exec: \"capturing.py\": executable file not found in $PATH" – Pulkit Nov 14 '17 at 06:50
  • Where did you put your script? Either check if the directory is in the `PATH` variable or change the `CMD` instruction to use the full path of the script. – samprog Nov 14 '17 at 06:55
  • my docker file and script is in same directory. and i am running docker in that directory only. – Pulkit Nov 14 '17 at 07:09
  • Do you get the error with `docker build` or `docker run`? With `docker run` it depends on where the script is in the container, not the host. With `docker build` you need to execute the command inside the directory your script lies in. – samprog Nov 14 '17 at 07:11
  • Like I said, change the `COPY` instruction inside the dockerfile to either reference a directory that's in the `PATH` instruction (weird that `/usr/local/share` isn't in there) or change the scriptname of the `CMD` instruction to the full path of where the script is. – samprog Nov 14 '17 at 07:15
  • /usr/local/share i have changed this to /home/pi/Desktop/teasr where in file is present but still having the same issue. – Pulkit Nov 14 '17 at 07:21
  • i just uploaded the error image please review it and help me to solve it – Pulkit Nov 14 '17 at 07:42
7

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"

None of the solution worked for me mentioned above.

Fixed the error after changing my Dockerfile as follows

FROM python:latest

COPY capturing.py ./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 ./

Hope this will be useful for others.

Nitendra
  • 484
  • 4
  • 17
4

Another way to run python script on docker can be:
copy the local python script to docker:

docker cp yourlocalscript.path container_id:/dst_path/ 

container id can be found using:

docker ps 

run the python script on docker:

docker exec -it python /container_script_path.py
anne
  • 127
  • 2
  • 4
2

its very simple

1- go to your Python script directory and create a file with this title without any extension

Dockerfile

enter image description here

2-now open the docker file and write your script name instead of sci.py

( content of Dockerfile )

FROM python:slim #i choice slim version you can choose another tag for example python:3

WORKDIR /usr/local/bin

COPY sci.py .    #replace you scrip name with sci.py

CMD [ "python", "sci.py" ] #replace you scrip name with sci.py

save it and now you should create image file from this dockerfile and script py

and next run it

3-in path address folder write CMD and press Enter key : enter image description here

4-When the cmd window opens for you, type in it :

docker build -t my-python-app .  #this create image in docker by this title my-python-app

5- and findly run image:

docker run -it --rm --name my-running-app my-python-app 

enter image description here

mamal
  • 1,791
  • 20
  • 14
2

I've encountered this problem recently, this dependency HELL between python2 and python3 got me. Here is the solution.

Bind your current working directory to a Docker container with python2 and pip2 running.

  1. Pull the docker image.

docker pull frolvlad/alpine-python2

  1. Add this alias into /home/user/.zshrc or /home/user/.bashrc

alias python2='docker run -it --rm --name python2 -v "$PWD":"$PWD" -w "$PWD" frolvlad/alpine-python2'

Once you type python2 into your CMD you'll be thrown into the Docker instance.

J.Vincent
  • 41
  • 1
  • 4