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:
Setting up the Project Directory
Creating a Simple Flask Application
Writing a Dockerfile
Building a Docker Image
Running the Docker Container
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
Create a new directory for your project:
mkdir flask-docker-app cd flask-docker-app
Inside this directory, create a subdirectory named
app
to hold the application code:mkdir app
Step 2: Creating a Simple Flask Application
Navigate to the
app
directory and create a Python file namedapp.py
:cd app touch app.py
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)
Create a
requirements.txt
file in the same directory to specify the dependencies:touch requirements.txt
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
- Locate and open the flask-docker-app directory in VS Code (Code editor)
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
Ensure you are in your project directory (
flask-docker-app
), and create a file namedDockerfile
: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
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.Build the Docker image using the
docker build
command:docker build -t flask-docker-app .
The
-t
flag names the imageflask-docker-app
. Also, the " . " is part of the command, it's not a mistake. You should have a similar output like thisRun 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
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.
Open a web browser and navigate to
http://localhost:5000
. You should see the message "Hello, Docker!"
Step 6: Cleaning Up
Stop the running container:
docker ps docker stop <container_id>
Our Container ID in this example is 75d6953f245c
Remove the container:
docker rm <container_id>
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