Saturday, 9 May 2020

Docker: Python: Hello World

Step 1: Create hello.py file
hello.py
print("Hello World")

Step 2: Execute the below command to download python image and run hello.py file.
docker run -v $(pwd):/src --rm python:3 python /src/hello.py

-v $(pwd):/src: Mount current working directory to /src folder of container
--rm: Remove the container after its execution.
python:3: I want to use the python image with tag 3
python hello.py: Run hello.py file.

$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Unable to find image 'python:3' locally
3: 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:3
Hello World

Since I do not have python:3 image in my system, docker downloads the image for the first time. Next time onwards, it will not download python:3 image and use the image from local registry.
$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Hello World
$
$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Hello World

You can see list of images using ‘docker image ls’ command.

$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              3                   4f7cd4269fa9        9 days ago          934MB



Previous                                                    Next                                                    Home

No comments:

Post a Comment