Rishab Gupta
Rishab Gupta

Reputation: 45

Run python script using a docker image

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?

Upvotes: 2

Views: 8512

Answers (4)

Hans Kilian
Hans Kilian

Reputation: 25632

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

Upvotes: 0

Kartik Pandey
Kartik Pandey

Reputation: 65

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

Upvotes: 3

Rishab Gupta
Rishab Gupta

Reputation: 45

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.

Upvotes: 0

Manoj Kumar
Manoj Kumar

Reputation: 5647

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 

Upvotes: 2

Related Questions