From Code to Container: Mastering Docker with a Simple Flask App!

From Code to Container: Mastering Docker with a Simple Flask App!

Creating a Docker project involves several steps, from setting up a development environment to deploying a containerized application. I'll guide you through a simple Docker project scenario where we will create a Docker container for a Python web application using Flask. This step-by-step guide will cover the following:

  1. Setting up the Project Directory

  2. Creating a Simple Flask Application

  3. Writing a Dockerfile

  4. Building a Docker Image

  5. Running the Docker Container

  6. Cleaning Up

Scenario: Containerizing a Simple Flask Application

NB: Before you dive into the steps, I will like you to know that you can download the Python application used in this project from my GitHub repo. Click here.

Step 1: Setting up the Project Directory

  1. Create a new directory for your project:

     mkdir flask-docker-app
     cd flask-docker-app
    
  2. Inside this directory, create a subdirectory named app to hold the application code:

     mkdir app
    

Step 2: Creating a Simple Flask Application

  1. Navigate to the app directory and create a Python file named app.py:

     cd app
     touch app.py
    
  2. Open app.py and add the following simple Flask application code:

     from flask import Flask
    
     app = Flask(__name__)
    
     @app.route('/')
     def home():
         return "Hello, Docker!"
    
     if __name__ == '__main__':
         app.run(host='0.0.0.0', port=5000)
    
  3. Create a requirements.txt file in the same directory to specify the dependencies:

     touch requirements.txt
    
  4. Add Flask to the requirements.txt file:

     Flask==2.0.1
    

NB: You need to install the following in order to follow along:

A) GitBash (if you are using Windows OS)

B) Virtual Studio Code

  1. Locate and open the flask-docker-app directory in VS Code (Code editor)

  1. Open a bash terminal in VS Code, by clicking on Terminal, then New Terminal

    Select the " + " icon in the right corner of your VS Code terminal, the select Git Bash

    Your terminal should be similar to mine at this point

Step 3: Writing a Dockerfile

  1. Ensure you are in your project directory (flask-docker-app), and create a file named Dockerfile:

  2. Open the Dockerfile and add the following instructions:

     # Use an official Python runtime as a parent image
     FROM python:3.9-slim
    
     # Set the working directory in the container
     WORKDIR /app
    
     # Copy the current directory contents into the container at /app
     COPY app/ /app
    
     # Install any needed packages specified in requirements.txt
     RUN pip install --no-cache-dir -r requirements.txt
    
     # Make port 5000 available to the world outside this container
     EXPOSE 5000
    
     # Define environment variable
     ENV NAME World
    
     # Run app.py when the container launches
     CMD ["python", "app.py"]
    

Step 4: Building a Docker Image

  1. Open a terminal and navigate to the root of your project directory (flask-docker-app), if you are following me so far, we are still in our project directory.

  2. Build the Docker image using the docker build command:

     docker build -t flask-docker-app .
    

    • The -t flag names the image flask-docker-app. Also, the " . " is part of the command, it's not a mistake. You should have a similar output like this

      Run this command to verify the docker image has been created:

      docker image ls

      Now you can see our Docker image "flask-docker-app" has been created as shown above.

Step 5: Running the Docker Container

  1. Run a container based on your image using the docker run command:

     docker run -p 5000:5000 flask-docker-app
    
    • The -p 5000:5000 flag maps port 5000 on your host to port 5000 in the container.

  2. Open a web browser and navigate to http://localhost:5000. You should see the message "Hello, Docker!"

Step 6: Cleaning Up

  1. Stop the running container:

     docker ps
     docker stop <container_id>
    

    Our Container ID in this example is 75d6953f245c

  2. Remove the container:

     docker rm <container_id>
    

  3. Optionally, remove the Docker image:

     docker rmi flask-docker-app
    

Final Thought

You've successfully created a simple Python Flask application, containerized it using Docker, ran it locally. This project demonstrates how to use Docker to package an application and its dependencies, making it easy to distribute and run anywhere.

Thanks

:Valentine Stephen