DevOps

Create Multi-Container Docker Application from Scratch

In this tutorial, we’ll walk through the process of containerizing a multi-container Docker application using “Docker Compose.” We’ll create a simple web application consisting of a Python Flask backend, a Redis database, and an Nginx web server.

Note: This post has been updated to reflect the usage change from “fig” , which is the predecessor to “docker-compose”

Prerequisites:

  • Docker installed on your machine (version 20.10 or later)
  • Docker Compose installed on your machine (comes bundled with Docker)
  • Basic knowledge of Docker and containerization. Refer to docker guides for more details on how to get started.

Step 1: Create the Project Structure

Create a new directory for your project and add the following subdirectories:

mkdir myapp
cd myapp
mkdir app redis nginx

Step 2: Create the Flask Application

In the app directory, create a new file called app.py with the following content:

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='redis', port=6379)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)Code language: JavaScript (javascript)

Step 3: Create the Redis Configuration

In the redis directory, create a new file called redis.conf with the following content:

port 6379

Step 4: Create the Nginx Configuration

In the nginx directory, create a new file called nginx.conf with the following content:

http {
    server {
        listen 80;
        server_name localhost;

        location / {
            proxy_pass http://app:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}Code language: PHP (php)

Step 5: Create the Dockerfiles

Create a new file called Dockerfile in each of the app, redis, and nginx directories:

Location: app/Dockerfile

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]Code language: JavaScript (javascript)

Location: redis/Dockerfile

FROM redis:alpine

COPY redis.conf /etc/redis/redis.conf

CMD ["redis-server", "/etc/redis/redis.conf"]Code language: JavaScript (javascript)

Location: nginx/Dockerfile

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

CMD ["nginx", "-g", "daemon off;"]Code language: JavaScript (javascript)

Step 6: Create the Docker Compose File

In the main application root directory, create a new file called docker-compose.yml (or just compose.yml if you are using the new “docker compose” command) with the following content:

services:
  app:
    build: ./app
    ports:
      - "5000:5000"
    depends_on:
      - redis
    environment:
      - REDIS_HOST=redis

  redis:
    build: ./redis
    ports:
      - "6379:6379"

  nginx:
    build: ./nginx
    ports:
      - "80:80"
    depends_on:
      - appCode language: JavaScript (javascript)

Step 7: Let’s Run the Multi-Container Docker Application!

Run the following command to build and start the containers:

docker-compose up
#or
docker compose up
Code language: CSS (css)

Open your web browser and navigate to http://localhost to see the application in action.

Note:

  • If port 80 is occupied by another service running on your machine, change the Nginx port from “80” to something else(e.g., “8000,” etc.).
  • If you are confused about whether to use “docker-compose ” instead of “docker-compose,” please refer to the forum discussion for more information.

Conclusion

In this tutorial, we’ve successfully orchestrated a multi-container application using Docker and Docker Compose. We’ve created a Flask application, a Redis database, and an Nginx web server, and demonstrated how to manage and orchestrate them using Docker Compose. Also, as you continue developing your application, don’t forget to auto-reload your docker environment. If there are any follow-up questions/comments, please comment below.

Rana Ahsan

Rana Ahsan is a seasoned software engineer and technology leader specialized in distributed systems and software architecture. With a Master’s in Software Engineering from Concordia University, his experience spans leading scalable architecture at Coursera and TopHat, contributing to open-source projects. This blog, CodeSamplez.com, showcases his passion for sharing practical insights on programming and distributed systems concepts and help educate others. Github | X | LinkedIn

View Comments

Recent Posts

Automation With Python: A Complete Guide

Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…

2 weeks ago

Python File Handling: A Beginner’s Complete Guide

Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…

4 weeks ago

Service Worker Best Practices: Security & Debugging Guide

You've conquered the service worker lifecycle, mastered caching strategies, and explored advanced features. Now it's time to lock down your implementation with battle-tested service worker…

1 month ago

This website uses cookies.