Using Docker to Run Python Script - NETSEC

Latest

Learning, Sharing, Creating

Cybersecurity Memo

Monday, May 4, 2020

Using Docker to Run Python Script

In this post, I will create a python container and run python script inside docker container and run python script directly through docker.


Pull and Start Docker Image


docker pull python
Start a python container
docker run -it -d --name mypython -p 8888:80 -v /data/www/python/:/usr/src/myapp/python bash
Explanation:
--name mypython container name
-p 8888:80 host port 8888 mapped to container port 80
-v /data/www/python/:/usr/src/myapp/ mapping directory, container directory /usr/src/myapp/ persistent to the host /data/www/python/ directory




    Execute Python Script In Docker

    Then write a hello.py file on the host machine, and then enter the container
    docker exec -it mypython bash
    Run Python program in the container
    python /usr/src/myapp/hello.py

    $ docker run -it -d --name python -p 82:80 -v /data/www/python/:/usr/src/myapp/ mypython bash
    Unable to find image 'python:latest' locally
    latest: Pulling from library/python
    90fe46dd8199: Pull complete 
    35a4f1977689: Pull complete 
    bbc37f14aded: Pull complete 
    74e27dc593d4: Pull complete 
    4352dcff7819: Pull complete 
    deb569b08de6: Pull complete 
    98fd06fa8c53: Pull complete 
    7b9cc4fdefe6: Pull complete 
    512732f32795: Pull complete 
    Digest: sha256:ad7fb5bb4770e08bf10a895ef64a300b288696a1557a6d02c8b6fba98984b86a
    Status: Downloaded newer image for python:latest
    2374355d7d50455008de0de70d62fc69ca018c3661bfbce78755bb480ec83628
    [node1] (local) [email protected] ~
    $ cd /data/www/python/
    [node1] (local) [email protected] /data/www/python
    $ vi hello.py
    print ("Hello World!!!")
    
    
    $ ls
    hello.py
    [node1] (local) [email protected] /data/www/python
    $ docker exec -it mypython bash
    root@2374355d7d50:/# python /usr/src/myapp/hello.py
    Hello World!!!
    root@2374355d7d50:/# exit
    exit

    Execute Python Script using Docker


    [node1] (local) [email protected] /data/www/python
    $ docker exec -it python python /usr/src/myapp/hello.py
    Hello World!!!
    [node1] (local) [email protected] /data/www/python
    $ 








    No comments:

    Post a Comment