This content originally appeared on DEV Community and was authored by Avesh
Docker is one of the most widely used containerization tools in the DevOps world. It's essential for developing, deploying, and running applications efficiently. This article outlines three practical Docker projects, providing complete instructions and code for each. These projects will help solidify your understanding of Docker and container-based applications.
1. Building and Running a Flask Web Application in Docker
Project Overview:
In this project, you'll containerize a simple Flask web application and run it inside a Docker container. Flask is a lightweight Python web framework, perfect for demonstrating Docker’s capabilities with Python applications.
Steps:
Step 1: Setup Flask Application
First, let's create a basic Flask app.
- Create a project directory:
mkdir flask-docker-app
cd flask-docker-app
- Inside the project directory, create a Python file called
app.py
:
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Dockerized Flask App!"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
- Add a
requirements.txt
file to define the project dependencies:
Flask==2.0.3
Step 2: Create a Dockerfile
Create a file named Dockerfile
in the same directory to define the image for our Flask app.
# 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
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the outside world
EXPOSE 5000
# Define environment variable
ENV FLASK_APP=app.py
# Run the application
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]
Step 3: Build and Run the Docker Container
- Build the Docker image:
docker build -t flask-app .
- Run the Docker container:
docker run -d -p 5000:5000 flask-app
Now, if you navigate to http://localhost:5000
, you'll see your Flask app running inside a Docker container.
Explanation:
- We defined a simple Flask app that listens on port 5000.
- The
Dockerfile
pulls a Python image, sets up the environment, installs the dependencies, and runs the Flask app. - The
docker build
command creates an image from theDockerfile
, and thedocker run
command starts the container with port 5000 exposed to the host.
2. Multi-Container Node.js and MongoDB Application Using Docker Compose
Project Overview:
This project demonstrates how to use Docker Compose to run a Node.js web application with a MongoDB database in separate containers. Docker Compose makes it easy to manage multi-container Docker applications.
Steps:
Step 1: Setup Node.js Application
- Create a new directory for your project:
mkdir node-mongo-app
cd node-mongo-app
- Inside the directory, initialize a Node.js project and install Express and Mongoose:
npm init -y
npm install express mongoose
- Create
server.js
:
const express = require('express');
const mongoose = require('mongoose');
const app = express();
// MongoDB connection
mongoose.connect('mongodb://mongo:27017/dockerApp', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));
app.get('/', (req, res) => {
res.send('Hello from Node.js and MongoDB app in Docker!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Step 2: Create the Dockerfile for Node.js App
# Use an official Node.js runtime as the base image
FROM node:14
# Set working directory inside the container
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the app port
EXPOSE 3000
# Run the application
CMD ["node", "server.js"]
Step 3: Docker Compose File
Create a docker-compose.yml
file that defines the services (Node.js app and MongoDB):
version: '3'
services:
nodeapp:
build: .
ports:
- '3000:3000'
depends_on:
- mongo
volumes:
- .:/app
mongo:
image: mongo
ports:
- '27017:27017'
Step 4: Build and Run the Multi-Container Application
- Build and run the application using Docker Compose:
docker-compose up --build
- You can now visit
http://localhost:3000
to see the application running.
Explanation:
- The Node.js app and MongoDB run in separate containers.
-
Docker Compose helps orchestrate the multi-container setup, ensuring MongoDB starts before the Node.js app via the
depends_on
field. -
volumes
mount the source code directory, enabling live updates without rebuilding the image.
3. Deploying a WordPress and MySQL Application Using Docker Compose
Project Overview:
In this project, we'll deploy a WordPress site backed by a MySQL database using Docker Compose. This will demonstrate how to set up a production-like environment using containers.
Steps:
Step 1: Docker Compose File for WordPress and MySQL
Create a project directory and inside it, create a docker-compose.yml
file:
version: '3'
services:
wordpress:
image: wordpress:latest
container_name: wordpress_container
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress_user
WORDPRESS_DB_PASSWORD: wordpress_password
WORDPRESS_DB_NAME: wordpress_db
volumes:
- ./wp_data:/var/www/html
db:
image: mysql:5.7
container_name: mysql_container
restart: always
environment:
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: wordpress_password
MYSQL_ROOT_PASSWORD: root_password
volumes:
- ./db_data:/var/lib/mysql
Step 2: Create Volumes for Persistent Storage
In this setup, we’ve added volumes wp_data
and db_data
to store WordPress and MySQL data persistently on the host machine.
Step 3: Running the Application
To run the application:
- Execute:
docker-compose up -d
This will pull the WordPress and MySQL images from Docker Hub, set up the environment variables, and launch the containers.
- Once the containers are running, navigate to
http://localhost:8080
to complete the WordPress installation.
Explanation:
- WordPress and MySQL run in separate containers, connected through Docker’s internal network.
- MySQL environment variables define the database name, user, and password.
- The
volumes
section ensures that your WordPress data and MySQL database remain intact even if the containers are stopped or deleted.
Conclusion
These three projects provide practical, hands-on experience with Docker, covering various real-world scenarios. The Flask app demonstrates how to containerize a simple Python application, the Node.js and MongoDB project illustrates how Docker Compose can manage multi-container applications, and the WordPress-MySQL project shows how to set up a production-like environment with persistent storage.
Each project follows industry best practices like isolating services, defining dependencies in Dockerfile
and docker-compose.yml
, and utilizing container orchestration tools to streamline application management. These exercises will help you master Docker’s core concepts and provide valuable experience for real-world projects.
This content originally appeared on DEV Community and was authored by Avesh
Avesh | Sciencx (2024-10-24T15:23:10+00:00) 3 Docker Project Ideas: Step-by-Step with Code and Explanations. Retrieved from https://www.scien.cx/2024/10/24/3-docker-project-ideas-step-by-step-with-code-and-explanations/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.