47

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.

7 Answers 7

48

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
Sign up to request clarification or add additional context in comments.

Comments

34

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

20 Comments

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".
I'm very sorry, I must've switched up directories... Try /usr/local/bin
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
how to build image as root?
Is your script executable? If not, you should change that with chmod +x capturing.py.
|
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.

9 Comments

what is /usr/local/share ? should i change it according to path? or make it run as it is?
Just put it somewhere, where the PATH variable can reach it, so your script can get executed
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"
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.
my docker file and script is in same directory. and i am running docker in that directory only.
|
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.

1 Comment

I think it should be "CMD ["python","capturing.py"]" without the /, at least this worked for me.
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

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.