Docker Image with Python Application

We shall learn to build a docker image with python application, and save the docker image to a file for usage in different computer.

Docker Image with Python Application Example

Build Docker Image with Python Application

1. Create a directory

A separate directory is useful to organise docker applications. For this Python Example, create a directory somewhere with name of your choice. We shall use the name python-application

2. Create Python Application

Create a simple Python File, in the directory python-application, with name containing the following content :

print("HelloWorld from Python Applicaiton in Docker")

3. Create Dockerfile

Create a file with name Dockerfile. Dockerfile contains instructions to prepare Docker image with our Python Application.

Following is the content of Dockerfile.

FROM python
COPY . /src
CMD ["python", "/src/"]

4. Build Docker Image

Run the following command in Terminal, from python-application directory, to create Docker Image with Python Application.

$ docker build -t python-application .
root@arjun-VPCEH26EN:/home/arjun/workspace/docker/python-application# docker build -t python-application .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM python
latest: Pulling from library/python
aa18ad1a0d33: Pull complete 
15a33158a136: Pull complete
f67323742a64: Pull complete
c4b45e832c38: Pull complete
b71152c33fd2: Pull complete
d8eff2a5d585: Pull complete
f80b3bd3dbda: Pull complete
e5b3e7f65920: Pull complete
Digest: sha256:2cbebfb9309a8b7b31c2fea8813436e297855f91cedd9cc28582678da905e5e0
Status: Downloaded newer image for python:latest
---> 26acbad26a2c
Step 2/3 : COPY . /src
---> 702fd061cbd3
Removing intermediate container f9a28d25df1e
Step 3/3 : CMD python /src/
---> Running in 4e9ad3906a79
---> 71deae3e7961
Removing intermediate container 4e9ad3906a79
Successfully built 71deae3e7961
Successfully tagged python-application:latest

Docker image with python application is created successfully.

5. Check the docker image

To display available docker images, run the following command.

$ docker images
root@arjun-VPCEH26EN:/home/arjun/workspace/docker/python-application# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
python-application   latest 71deae3e7961 41 minutes ago 690MB
<none> <none> ce17e2d430ef 29 hours ago 643MB
python               latest 26acbad26a2c 4 days ago 690MB
java 8 d23bdf5b1b1b 8 months ago 643MB
hello-world          latest              c54a2cc56cbb 14 months ago 1.85kB
docker/whalesay      latest 6b362a9f73eb 2 years ago 247MB

Run Docker Image with Python Application

root@arjun-VPCEH26EN:/home/arjun/workspace/docker/python-application# docker run python-application
HelloWorld from Python Applicaiton in Docker

Save Docker Image to a tar file

Save the Docker Image file to a tar file, so that the image file could be copied to other machines through disk storage devices like pen-drive, etc.

Run the following command to save Docker image as a tar file.

$ docker save -o /home/arjun/workspace/docker/python-application.tar python-application

Saving might take few seconds. Wait for the command to complete.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker# ls
java-application  python-application  python-application.tar

Now you may copy or ship the Docker Image file that is having Python Application.


In this Docker Tutorial – Docker Python Application Example, we have learnt to build a Docker Image with Python Application and also how to save the image to a file and transfer it to other computers or servers.