Photo by Ian Taylor on Unsplash

Using a Docker container to run a python application

Aqib Mehmood

--

In this article we are going to go through the step by step procedure of how to make a docker container to run a basic python application using pandas library.

We will use the following in this tutorial

1- Docker
2- Python
3- Pandas

Often in the development stage of your applications you need a consistent environment across all your devices and all the devices of your fellow developers. It can be cumbersome to install all the dependencies from scratch whenever you try to run your code on a new device or in a new environment. Docker is here to solve exactly this problem.

Docker provides a way to package everything you need to run an app or a simple script in a unit called a Container which you can download or pull into all the devices you use and just start working right away.

Following are the steps that we will follow to make and run a Docker container.

1- Installing python and pandas onto our system and making a python script.
2- Creating a Docker Image file which will be used to make a Container.
3- Testing the container.

So let’s get started.

1- Installing python and pandas onto our system and making a python script

1- I’m assuming that you already have python installed on your computer. If not, you can visit their website and download the installer file and complete the installation process.
2- Open your windows CMD and install Pandas using Pip install pandas command.
3- Create a project directory and give it a name. For this tutorial we’ll call it python-docker.
4- In this folder location make a new python file and give a name demo app.py.
5- Next we will write a very basic program that reads and prints a CSV file using pandas DataFrame. First click on this link and download the CSV file that we will be using for this and save it in your project folder.
6- Next just copy and paste the script below.

import pandas as pddf = pd.read_csv("SampleCSVFile.csv",encoding='latin1')
print(df.head(1000))

7- Run the script by typing python ‘demo app.py’ in your windows CMD and see if it works. It should.

2- Creating a Docker Image file which will be used to make a Container

1- Install Docker by going to their official website and following the installation steps.
2- Make a new file in your project directory and give it a name Dockerfile. Be careful to not give it an extension.
3- Copy and paste the below code. We will go through every line next.

# syntax=docker/dockerfile:1FROM python:3.8-slim-busterRUN /usr/local/bin/python -m pip install --upgrade pipRUN pip install pandasCOPY . .CMD [ "python", "demo app.py"]
  • First line tells the docker engine on how to read/parse the lines coming after it. This line always has to be the first line before any spaces or characters.
  • Second line downloads a base python image from Dockerhub. It is an environment that already has python installed on it.
  • Third line upgrades the Pip as the above image is running an old version of it.
  • Fourth line installs Pandas library in the image. This is a crucial step that we will also use to test our Docker image next
  • Fifth line copies all the files in the current directory and stores them onto the file system attached with the docker image.
  • Finally the sixth line tells Docker image on what to do when the Run command is given.

3- Now go to your windows CMD and make a docker image by typing

docker build --tag python-docker .

python-docker will be the repository that the image will be in.

3- Testing the container

Now it’s finally time to test the container and to do that we will have to uninstall the pandas library from our system.

1- Type Pip uninstall pandas in your windows CMD and uninstall the pandas library.
2- Run the docker image by typing Docker run python-docker in your windows CMD and see the results.

Now you will see a pandas dataframe printed on your screen despite not having pandas library installed on your system. Where is the pandas library coming from? This is the power of Docker.

Hope you found this informative and fun.

--

--