skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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
    
    Login or Signup to reply.
  3. 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 
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search