In this post I’ll tell you how to run a simple python Hello World Program on Docker Container. In my last post I have already tell you how to Run Java Hello World Program On Docker Container
If you don’t know what is docker then you can check my previous post What is Docker? | How Docker Works? | Learn Docker Basic Commands
How Will We Do?
I’ll try to keep this tutorial as simple as possible. First of all I’ll create an docker image(say test-hello-python) with the help of Dockerfile(say Dockerfile) then I’ll run this docker image on docker container and it will print “Hello World”.
I am an Linux user so I’ll use Linux command here and if you are a window user then you can use git bash to execute Linux command on your machine. Let’s be a root user in Linux machine.
devopsrider@del1-lhp-n02552:~$ sudo su
Create a fresh folder devopsrider in which we’ll create our Dockerfile.
root@del1-lhp-n02552:~# mkdir devopsrider
Then enter into devopsrider folder.
root@del1-lhp-n02552:~# cd devopsrider
Now we’ll create a Dockerfile with name “Dockerfile”. You can give any name to your Dockerfile but make sure that you use same name when you will create build this Dockerfile to create an image.
root@del1-lhp-n02552:~/devopsrider# vi Dockerfile
Paste the following code into Dockerfile. In next paragraphs I’ll explain these three lines of code line by line.
FROM python:3 RUN echo 'print("Hello World")' > testPython.py ENTRYPOINT ["python3", "testPython.py"]
To print “Hello World” in python, first of all, we need to install python in our container. So we’ll tell Dockerfile to pull python image from docker Hub. In my case I am pulling python3 by writing python:3 in first line. If you want to pull python2 the write python:2 in first line of Dockerfile.
Second line in Dockerfile can be broken into three parts. First RUN will tell container to execute Linux command on container, second echo ‘print(“Hello World”)’ will save this print(“Hello World”) in file and third part testPython.py will tell the file name in which print(“Hello World”) will be saved.
So finally second line tells to create a python file testPython.py that will print “Hello World”.
Now Third line will tell container to run this python file whenever container will come up. It;s like an entry point.
Create Docker Image from Dockerfile
Now our Dockerfile is ready. Let’s build image from it. I’ll give this image a name “hello-python-image”.
root@del1-lhp-n02552:~/devopsrider# docker build -f Dockerfile -t hello-python-image . Sending build context to Docker daemon 50.72MB Step 1/3 : FROM python:3 ---> 0a3a95c81a2b Step 2/3 : RUN echo 'print("Hello World Python")' > testPython.py ---> Running in bb0998f28e22 Removing intermediate container bb0998f28e22 ---> e9be916bd61a Step 3/3 : ENTRYPOINT ["python3", "testPython.py"] ---> Running in 33f68b0bd685 Removing intermediate container 33f68b0bd685 ---> ff4fdfe0bb0b Successfully built ff4fdfe0bb0b Successfully tagged hello-python-image:latest
To see created image “hello-python-image” and an python3 image on your machine type following command.
root@del1-lhp-n02552:~/devopsrider# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE hello-python-image latest 918b69e014ff 11 seconds ago 932MB python 3 0a3a95c81a2b 8 days ago 932MB
Now to run this image on docker container run this image. It will print “Hello World”
Run Docker Image on Docker Container
root@del1-lhp-n02552:~/devopsrider# docker run hello-python-image Hello World
Now if you will check this container by following command –
root@del1-lhp-n02552:~/devopsrider# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You will see no hello-python-image container is running because your container ran and printed Hello World and stopped(exited) as it has completed given task. If I would have run to run a long running program(for example a program that take input or infinite loop) then it would run continuously and you could see that running container here.
You can see your exited container with following command –
root@del1-lhp-n02552:~/devopsrider# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e1d1c0fd393 hello-python-image:latest "python3 testPython.…" 31 seconds ago Exited (0) 30 seconds ago clever_shtern
I hope you liked this tutorial, share this post with your friends and colleagues. Please let me know in comment box if you have any doubt or query.