Understanding Docker basics with Python and flask example

Saurabh Dorle
Omni Data Science
Published in
5 min readMay 11, 2023

Docker is a platform for building, shipping, and running applications in containers. A container is a lightweight, standalone executable package that includes everything needed to run an application, including code, libraries, system tools, and runtime. Docker enables developers to easily package and distribute their applications, and to run them in a consistent and reliable way across different environments.

Why Docker is Required?

Traditionally, software applications were built and deployed on physical servers, each with its own operating system, hardware, and software dependencies. This made it difficult to develop and deploy software applications, as they had to be customized for each environment. Furthermore, it was challenging to manage multiple applications running on the same server, as they could interfere with each other and cause performance issues.

Docker solves these challenges by providing a way to package an application and its dependencies into a container, which can be run on any system that supports Docker. Containers are isolated from each other and from the host system, ensuring that applications can run in a consistent and reliable way across different environments. This makes it easier to develop and deploy software applications, as developers can build and test applications in a containerized environment that closely mimics the production environment.

Basic Concepts of Image and Container

In Docker, an image is a lightweight, stand-alone, and executable package that includes everything needed to run an application, including code, libraries, system tools, and runtime. An image is essentially a snapshot of a Docker container at a particular point in time. Docker images are stored in a registry, which is a centralized location where images can be shared and downloaded.

To run an application in Docker, you need to create a container from an image. A container is a running instance of an image, which includes the application code and its dependencies. Containers are isolated from each other and from the host system, providing a secure and reliable environment for running applications. Multiple containers can be run on the same host, each with its own set of resources and dependencies.

When you create a container from an image, you can specify various parameters, such as the port mappings, environment variables, and volume mounts. This allows you to customize the container to meet the specific needs of your application.

Working of Docker:

Docker is based on a client-server architecture, where the Docker client communicates with the Docker daemon via a REST API. The Docker daemon manages the lifecycle of containers, including creating, starting, stopping, and deleting containers. Docker images are used to define the application and its dependencies, and Docker containers are instantiated from these images.

To use Docker, you need to install the Docker client on your local machine, and the Docker daemon on the host where you want to run your containers. You can then use the Docker command-line interface (CLI) to interact with the Docker daemon, creating and managing containers and images.

Example:

Let's take a simple example to understand how Docker works. Suppose you have a Python Flask application that you want to run in a Docker container. Flask is a popular web framework for building Python web applications.

First, you need to create a Dockerfile that defines the Docker image for your application. The Dockerfile specifies the base image, installs the necessary dependencies, and copies the application code into the container.

Here is an example Dockerfile for a Flask application:

# syntax=docker/dockerfile:1

FROM python:3.9.2

WORKDIR python-docker

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Now let’s first understand what the lines of code in the Docker file above mean and what role they play.

FROM python:3.9.2

This Dockerfile uses the official Python 3.9 slim image as the base image.

WORKDIR python-docker

It sets the working directory to /python-docker, copies the current directory into the container.

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

Installs the dependencies specified in requirements.txt.

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Copies remaining files from directory and specifies the command to run when the container launches.

Next, you need to create a requirements.txt file that lists the dependencies for your application. For this example, we will use Flask.

Flask==2.3.2

Now that you have your Dockerfile and requirements.txt file, you can build the Docker image using the docker build command:

docker build --tag py-docker-app .

This command builds the Docker image using the Dockerfile in the current directory, and tags it with the name “py-docker-app”. The “.” at the end of the command specifies the build context, which includes the Dockerfile and all the files in the current directory.

To view images from the command line, execute the following:

docker images

Once the Docker image is built, you can run it in a container using the docker run command:

docker run -d -p 8050:5000 py-docker-app

This command runs the Docker container and maps port 8050 on the host to port 5000 in the container. The “py-docker-app” argument specifies the name of the Docker image to use.

We can use the following command to see which containers are currently running:

docker ps

To stop the currently running container, we can execute this command:

docker stop <container-name>

To run and test the docker on local machine, we first need to install docker on the system.

To install docker: Docker

Link to complete code: GitHub

Conclusion

Docker is a powerful platform for building, shipping, and running applications in containers. With Docker, you can easily package and distribute your applications, and run them in a consistent and reliable way across different environments. By understanding the basics of Docker and how it works, you can leverage this technology to streamline your development and deployment processes and improve the scalability and reliability of your applications.

--

--