Chapter 81 – Convert Python Web App to Images & Push Them Live using Docker

For more resiliently, flexibly, and cost-efficiently deploying and running Web apps, app containerization is one of the most popular approaches worldwide. In this article, I would walk through how to leverage Docker to convert a Python Flask web app to images and push them to the Docker hub. By the end of this piece, you can learn to deploy live images on the Docker hub which would be used to connect with Cloud.

For more resiliently, flexibly, and cost-efficiently deploying and running Web apps, app containerization is one of the most popular approaches worldwide. In this article, I would walk through how to leverage Docker to convert a Python Flask web app to images and push them to the Docker hub. By the end of this piece, you can learn to deploy live images on the Docker hub which would be used to connect with Cloud.

Table of Contents: Convert Python Web App to Images & Push Live using Docker

What is Docker

Docker is an open-source platform that allows developers to automate the deployment and running of applications inside containers. Containers are lightweight, portable, and isolated environments that package an application with its dependencies, such as libraries and configurations, enabling consistent and reliable execution across different computing environments.

Here are some key concepts related to Docker:

  1. Containerization: Docker utilizes containerization technology, which involves creating and running containers. Containers provide an isolated and reproducible environment, ensuring that applications can run consistently across various systems.
  2. Docker Image: An image is a lightweight, standalone, and executable package that includes everything needed to run an application, including the code, runtime, libraries, and dependencies. Images serve as the building blocks for containers.
  3. Docker Container: A container is an instance of an image. It runs as a separate process on the host system, isolated from other containers and the underlying infrastructure. Containers are portable and can be easily moved between different systems that have Docker installed.
  4. Dockerfile: A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, configuration, dependencies, and commands required to set up the application environment.
  5. Docker Registry: A Docker registry is a repository that stores Docker images. The most commonly used registry is Docker Hub, which is a public registry hosting a vast number of pre-built images. Additionally, you can set up private registries to store and share your own Docker images within your organization.
  6. Docker Compose: Docker Compose is a tool for defining and managing multi-container applications. It allows you to describe complex application environments using a YAML file and then deploy and manage them as a single unit.
  7. Orchestration: Docker Swarm and Kubernetes are orchestration platforms that help manage and scale containerized applications across multiple hosts or clusters. They provide features for automated deployment, scaling, load balancing, and self-healing of containers.

Docker has gained popularity due to its ability to simplify the packaging, deployment, and scaling of applications. It promotes a consistent and efficient development workflow, making it easier for teams to collaborate and deploy applications across different environments.

Create a Credential and Download Docker App

Docker provides a free tier that includes 1 private repository which is not public. Once a new account has been created, we need to download Docker app and install on a local device. It has Window, Linux version, OS version as well

Flask App Docs

First thing first, we need to prepare a full set of Flask app docs based on the Flask framework structure. One of the main things that we have to check is the port and host value in the app.py. Please be sure to add these parameters in your Flask main app script.

if __name__ == "__main__":

app.run(debug=True, host="0.0.0.0", port=8000)

Dockerfile Doc

At the root level of the Flask app which is on the same floor as requirements.txt, we need to add a Dockerfile. In this file, we mainly set up and configure several critical components for Docker images:

  • Language version
  • Directory path
  • COPY dependencies
  • RUN and Install dependencies
  • COPY Flask app docs and place in a location
  • Command and run

Here is a sample:

FROM python:3.9.6

WORKDIR /python-flaskdemo

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY ./democn ./democn

CMD ["python", "./democn/app.py"]

Command and build a new Docker Image tag locally

When all docs are ready in the folder, we can cd the folder and build the image using the docs by the command as follows:

docker build -t flaskdemo .

For flaskdemo here, it’s the image tag name locally. You can name whatever you like. The image name would show in the Docker Desktop local interface after having been built successfully

Once the image build is done, you can try to run locally and check if the web app runs as well as expected. This process is similar to the Flask app running locally

docker run -p 8000:8000 flaskdemo

Push Local Docker Image Tags to Docker Hub

Now it’s time to push your ready web app image to Docker Hub. Basically, it’s a process to kick it off. There are several steps in your terminal:

Step 1: docker login

Use this command to check if the Docker desktop has connected with the Docker hub already. Make sure it’s connected to the hub, or you need to log in first using the credential you created earlier.

Step 2: docker image

Input this command to look up the image tag you are going to push live. Copy and put it down first and it will be used in a moment.

Step 3: docker tag and push an existing image to Docker Hub

Type in this command with the tag ID you copy just now, the Docker Hub project path, and the new image name shown on Docker Hub. The image name can be different from your local one because Docker Desktop would push the rendered configurations and docs to the new image.

docker tag tagID dockerHubname/projectname:newimagename

Full Python Script of Convert Python Web App to Images & Push Live using Docker

If you are interested in Chapter 81 – Convert Python Web App to Images & Push Them Live using Docker, please subscribe to our newsletter by adding the message ‘Chapter 81 + Full Docker Image Convert scripts for Python Web Apps’. We would send you the script when the up-to-date app script is live.

I hope you enjoy reading Chapter 81 – Convert Python Web App to Images & Push Them Live using Docker. If you did, please support us by doing one of the things listed below, because it always helps out our channel.