Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

docker
python

How to create a Dockerfile for running a Python application

Muhammad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Dockerfile allows the user to create their application images. This file describes the instructions that specify what environment to use and which commands to run to execute the user's file.

There are four steps in dockerizing any application:

  1. Create the docker file
  2. Create the desired application to dockerize
  3. Build an image using the docker file
  4. Run the image

Let's understand this by dockerizing a Python application:

Create the docker file

Start with the new empty directory, which will contain all the essential things you need to build an image for your application.

Create a file with the name, Dockerfile, without any extension.

Note: No extension is required for the Dockerfile. And you can use any text editor to edit it.

We'll use the Python3 image as our launching point in this Answer.

FROM python:3

Add this line to your Dockerfile. Since we want to run some Python script, myScript.py, you also need to add the script to your DockerFile.

ADD myScript.py /

You'll add this script to our Dockerfile as well. Now, it's time to add the dependencies of your application. For example, our Python script depends on the pyStrichpyStrich library to generate the barcode; we need to install it before actually running the script, myScript.py.

RUN pip install pystrich

Add this line to the Dockerfile to install this library. You can use the following command is used to run the script.

CMD [ "python", "./myScript.py" ]

Finally, the Dockerfile will look like this:

FROM python:3
ADD myScript.py /
RUN pip install pystrich
CMD [ "python", "./myScript.py" ]
Dockerfile

All the elements are self-explanatory. Now, create your application :

Create Python application to dockerize

Our Python application will look like this:

from pystrich.datamatrix import DataMatrixEncoder
encoder = DataMatrixEncoder('This is a DataMatrix.')
encoder.save('./datamatrix_test.png')
print(encoder.get_ascii())
myScript.py

As our docker file is ready and our application is prepared, you need to build an image using your Dockerfile.

Build an image using the docker file

You'll use the following command to build the image of your application:

docker build -t python-app .
Build docker image

Run the image locally

You can also run this image as a container in your local terminal.

Note: To view all docker images, we can use the docker images command.

As we build our application's image with the name python-app, you can run it by running the following command:

docker run python-app
To run python applications image

To run a single file script, you can prevent writing a complete Dockerfile. In the examples below, assume you store myScript.py in /my-first-docker-app/, and you want to name the container my-first-python-script:

docker run -it --rm --name my-first-python-script -v "$PWD":/my-first-docker-app/ python:3 python myScript.py

RELATED TAGS

docker
python

CONTRIBUTOR

Muhammad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring