Beginner’s Guide: Docker for Python Application Development and Deployment

Aditya Mangal
6 min readMar 24, 2023

I know you are also curious to know how to make APIs with Flask and live it on web page using streamlit. Don’t worry, i have already written a blog on it.You can check it here.

Tenor

As a Python developer, you’ve likely come across Docker — the popular open-source platform that enables the creation, deployment, and running of applications in containers. If you’re new to Docker, you may be wondering how to get started with using it for Python application development and deployment. Fear not, because in this article, we’ll cover the basics of Docker for Python and provide an easy-to-follow guide for beginners. By the end of this article, you’ll have a solid understanding of how to use Docker to streamline your Python development and deployment processes. So, let’s dive in!

Giphy

Why Use Docker for Python Development and Deployment?

Before we dive into the details, let’s discuss why using Docker for Python application development and deployment is a good idea. Docker has many benefits for developers, including:

  1. Consistent Environment: Docker provides a consistent environment for your application. This ensures that your application runs the same way on different machines, making it easier to develop and deploy.
  2. Isolation: Docker allows you to isolate your application from the host machine. This means that changes to the host machine are less likely to affect your application.
  3. Portability: Docker containers can be easily moved between different machines, making it easier to deploy your application.
  4. Scalability: Docker allows you to scale your application easily by adding or removing containers.
Giphy

Now that we have discussed the benefits of using Docker for Python development and deployment, let’s dive into the details of how to use Docker and build a flask app inside a docker.

Step 1: Install Docker

The first step to using Docker is to install it on your machine. You can find detailed instructions for installing Docker on your operating system on the Docker website.

Step 2: Create a Dockerfile

The next step is to create a Dockerfile. A Dockerfile is a text file that contains instructions for building a Docker image. In the Dockerfile, you specify the base image, any dependencies, and the commands needed to run your application.

FROM ubuntu:20.04
RUN apt update
RUN apt install python3 -y
RUN apt install python3-pip -y
RUN apt-get install vim -y
RUN pip install flask==2.2.2
WORKDIR /home
COPY app.py /home/app.py

Note: Save this file as Dockerfile without any extension.

File Explorer

Step 3: Build the Docker Image

Once you have created the Dockerfile, you can build a Docker image. To make the image, you run the docker build command and specify the location of the Dockerfile.

docker build -t python_project .
  • -t : Name and optionally a tag in the name:tag format
  • . : Current Directory
  • python_project : Image name of Docker

For more to know about arguments, you can read here.

Terminal Output

We can verify the image whether is built or not.

Terminal Output

Step 4: Run the Docker Container

Once you have built the Docker image, you can run it as a container. To run the container, you use the docker run command and specify the image name.

docker run -p 1111:1111 -it --name python_container python_project:latest
  • -p : Publish or expose port
  • -it : Instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container
  • — name : Assign a name to the container

For more to know about arguments, you can read here.

Terminal Output

Step 5: Develop Flask App inside Container

Now, we get a ubuntu terminal. In this, we can develop any application as we do in our Linux system.

Let’s add a simple flask app code using vim and run it inside this container.

Terminal Output

Press i to insert the code in the app.py file.

Terminal Output
from flask import Flask, render_template_string

app = Flask(__name__)

youtube_code = """<iframe width="560" height="315" src="https://www.youtube.com/embed/jDEYYZRWX_Q" title="YouTube
video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope;
picture-in-picture; web-share" allowfullscreen></iframe>"""


@app.route("/")
def index():
return render_template_string("<h1>Hello, Don't forget to check out my vlog channel<h1> <br>" + youtube_code)


if __name__ == '__main__':
app.run(host='0.0.0.0', port=1111, debug=True)

To save this file, press ESC and press : and write wq and hit enter.

Now, we can run this file and check its output in our browser by exposing port 1111.

Terminal Output

We can check the output in our browser with the below URL

http://localhost:1111/
Chrome Browser — my Vlog Channel

Step 6: Deploy our flask app to run directly

Now, we have to update Dockerfile and app.py o that it can run Flask App directly when we run a container.

Dockerfile and app.py

FROM ubuntu:20.04
RUN apt update
RUN apt install python3 -y
RUN apt install python3-pip -y
RUN apt-get install vim -y
RUN pip install flask==2.2.2
WORKDIR /home
COPY app.py /home/app.py

CMD python3 app.py
from flask import Flask, render_template_string

app = Flask(__name__)

youtube_code = """<iframe width="560" height="315" src="https://www.youtube.com/embed/jDEYYZRWX_Q" title="YouTube
video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope;
picture-in-picture; web-share" allowfullscreen></iframe>"""

@app.route("/")
def index():
return render_template_string("<h1>Hello, Don't forget to check out my vlog channel<h1> <br>" + youtube_code)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=1111, debug=True)

We have to build the Dockerfile again.

docker build -t python_project .

Run the Docker container

docker run -d -p 1111:1111 python_project

We can check the running Docker Container

Terminal Output

Now, we can check whether our Flask App is running or not in our browser

http://localhost:1111/
Chrome Browser
Tenor

Hurray, You can develop and deploy your application using Docker. Docker makes it easy to deploy your application to different environments, such as production or staging. You can also use Docker Compose to manage multiple containers that make up your application.

Conclusion

Docker is a powerful tool for Python application development and deployment. It provides a consistent environment, isolation, portability, and scalability. In this article, I discussed the benefits of using Docker and the steps involved in using Docker for Python application development and deployment. By following these steps, you can easily create and deploy your Python applications.

--

--

Aditya Mangal

My Personal Quote to overcome problems and remove dependencies - "It's not the car, it's the driver who win the race".