2

I downloaded a python script and a docker image containing commands to install all the dependencies. How can I run the python script using the docker image?

4 Answers 4

3

Copy python file in Docker image then execute -

docker run image-name PATH-OF-SCRIPT-IN-IMAGE/script.py

Or you can also build the DockerFile by using the RUN python PATH-OF-SCRIPT-IN-IMAGE/script.py inside DockerFile.

How to copy container to host

docker cp <containerId>:/file/path/within/container /host/path/target

How to copy host to the container

docker cp /host/local/path/file <containerId>:/file/path/in/container/file
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain how to copy the python file in the docker image?
2

Run in interactive mode:

 docker run -it image_name python filename.py

or if you want host and port to be specified:

 docker run -it -v filename.py:filename.py -p 8888:8888 image_name python filename.py 

8 Comments

Getting this error
No such file or directory
For this docker run -it image_name python filename.py
could you please write down which command were you running on the shell? and attach the error snapshot?
Command: docker run -it image_name python filename.py
|
0

Answer

First, Copy your python script and other required files to your docker container.

docker cp /path_to_file <containerId>:/path_where_you_want_to_save

Second, open the container cli using docker desktop and run your python script.

Comments

0

The best way, I think, is to make your own image that contains the dependencies and the script.

When you say you've been given an image, I'm guessing that you've been given a Dockerfile, since you talk about it containing commands.

Place the Dockerfile and the script in the same directory. Add the following lines to the bottom of the Dockerfile.

# Existing part of Dockerfile goes here    
COPY my-script.py .
CMD ["python", "my-script.py"]

Replace my-script.py with the name of the script.

Then build and run it with these commands

docker build -t my-image .
docker run my-image

1 Comment

@RishabGupta If it doesn't work, then please edit your post and add the Dockerfile you've got to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.