Real-world Examples

This section provides practical examples of Docker Compose applications, showcasing its usage in diverse scenarios like web applications, microservices, and databases. It offers a valuable opportunity to analyze the structure and configuration of real-world Docker Compose projects, enhancing understanding and learning through practical application.

Examples:

  • Wordpress (wordpress): A straightforward example demonstrating the deployment of a WordPress website using Docker Compose.

    • Source: tree/master/wordpress
    • Configuration: The docker-compose.yml file defines the services for the WordPress application, including the database, web server, and PHP interpreter.
    • Code:
    version: "3.7"
              services:
                db:
                  image: mysql:5.7
                  restart: always
                  environment:
                    MYSQL_ROOT_PASSWORD: "root"
                    MYSQL_DATABASE: "wordpress"
                  volumes:
                    - db_data:/var/lib/mysql
                wordpress:
                  image: wordpress:latest
                  restart: always
                  depends_on:
                    - db
                  environment:
                    WORDPRESS_DB_HOST: db
                    WORDPRESS_DB_NAME: wordpress
                    WORDPRESS_DB_USER: root
                    WORDPRESS_DB_PASSWORD: root
                  ports:
                    - "80:80"
                  volumes:
                    - wp_data:/var/www/html
              volumes:
                db_data:
                wp_data:
              
  • Node.js Application (node-app): This example showcases the deployment of a Node.js application using Docker Compose.

    • Source: tree/master/node-app
    • Configuration: The docker-compose.yml file defines the services for the Node.js application, including the Node.js application container and a PostgreSQL database.
    • Code:
    version: '3.7'
              services:
                db:
                  image: postgres:12
                  restart: always
                  environment:
                    POSTGRES_USER: myuser
                    POSTGRES_PASSWORD: mypassword
                    POSTGRES_DB: mydatabase
                  ports:
                    - "5432:5432"
                  volumes:
                    - db_data:/var/lib/postgresql/data
                app:
                  image: node:14
                  restart: always
                  depends_on:
                    - db
                  ports:
                    - "3000:3000"
                  volumes:
                    - ./:/usr/src/app
                  command: bash -c "npm install && npm start"
              volumes:
                db_data:
              
  • Microservice Architecture (microservices): This example demonstrates a microservices architecture using Docker Compose, showcasing the deployment of multiple services with their dependencies.

    • Source: tree/master/microservices
    • Configuration: The docker-compose.yml file defines the services for the microservices architecture, including a database, a front-end application, a user service, and a product service.
    • Code:
    version: '3'
              services:
                db:
                  image: postgres:12
                  restart: always
                  environment:
                    POSTGRES_USER: myuser
                    POSTGRES_PASSWORD: mypassword
                    POSTGRES_DB: mydatabase
                  ports:
                    - "5432:5432"
                  volumes:
                    - db_data:/var/lib/postgresql/data
                frontend:
                  image: nginx:latest
                  restart: always
                  ports:
                    - "80:80"
                  depends_on:
                    - user-service
                    - product-service
                  volumes:
                    - ./frontend:/usr/share/nginx/html
                user-service:
                  image: node:14
                  restart: always
                  depends_on:
                    - db
                  environment:
                    DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase
                  ports:
                    - "5001:5001"
                  volumes:
                    - ./user-service:/usr/src/app
                  command: bash -c "npm install && npm start"
                product-service:
                  image: node:14
                  restart: always
                  depends_on:
                    - db
                  environment:
                    DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase
                  ports:
                    - "5002:5002"
                  volumes:
                    - ./product-service:/usr/src/app
                  command: bash -c "npm install && npm start"
              volumes:
                db_data:
              
  • Multi-Stage Build (multi-stage): This example demonstrates the use of multi-stage builds in Docker Compose for optimizing the Docker image size.

    • Source: tree/master/multi-stage
    • Configuration: The docker-compose.yml file defines the services for the multi-stage build, including the build stage for compiling the application and the final stage for creating the production image.
    • Code:
    version: "3.7"
              services:
                app:
                  build:
                    context: .
                    dockerfile: Dockerfile
                  image: my-app:latest
                  ports:
                    - "8080:8080"
              
  • Docker Compose with Traefik (traefik): This example showcases the integration of Traefik with Docker Compose to achieve reverse proxy and load balancing functionality.

    • Source: tree/master/traefik
    • Configuration: The docker-compose.yml file defines the services for the Traefik integration, including the Traefik service and the backend applications.
    • Code:
    version: '3.7'
              services:
                traefik:
                  image: traefik:v2.4
                  command: --api.insecure --providers.docker --providers.docker.exposedbydefault=false --providers.docker.network=traefik-net --entrypoints.http.address=:80 --entrypoints.https.address=:443
                  ports:
                    - "80:80"
                    - "443:443"
                  volumes:
                    - ./traefik.toml:/etc/traefik/traefik.toml
                    - ./acme.json:/etc/traefik/acme.json
                  networks:
                    - traefik-net
                app1:
                  image: nginx:latest
                  restart: always
                  ports:
                    - "8080:80"
                  networks:
                    - traefik-net
                  labels:
                    - traefik.enable=true
                    - traefik.http.routers.app1.rule=Host(`app1.example.com`)
                    - traefik.http.services.app1.loadbalancer.server.port=80
                app2:
                  image: nginx:latest
                  restart: always
                  ports:
                    - "8081:80"
                  networks:
                    - traefik-net
                  labels:
                    - traefik.enable=true
                    - traefik.http.routers.app2.rule=Host(`app2.example.com`)
                    - traefik.http.services.app2.loadbalancer.server.port=80
              networks:
                traefik-net:
              
  • Next.js Application (nextjs): This example showcases the deployment of a Next.js application using Docker Compose.

    • Source: tree/master/nextjs
    • Configuration: The docker-compose.yml file defines the services for the Next.js application, including the Next.js application container and a PostgreSQL database.
    • Code:
    version: "3.8"
              services:
                db:
                  image: postgres:14-alpine
                  restart: always
                  environment:
                    POSTGRES_USER: myuser
                    POSTGRES_PASSWORD: mypassword
                    POSTGRES_DB: mydatabase
                  ports:
                    - "5432:5432"
                  volumes:
                    - db_data:/var/lib/postgresql/data
                app:
                  build:
                    context: .
                    dockerfile: Dockerfile
                  restart: always
                  depends_on:
                    - db
                  environment:
                    DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase
                  ports:
                    - "3000:3000"
                  volumes:
                    - ./:/usr/src/app
                  command: bash -c "npm install && npm run dev"
              volumes:
                db_data:
              
  • Flask Application (flask): This example showcases the deployment of a Flask application using Docker Compose.

    • Source: tree/master/flask
    • Configuration: The docker-compose.yml file defines the services for the Flask application, including the Flask application container and a PostgreSQL database.
    • Code:
    version: '3.7'
              services:
                db:
                  image: postgres:12
                  restart: always
                  environment:
                    POSTGRES_USER: myuser
                    POSTGRES_PASSWORD: mypassword
                    POSTGRES_DB: mydatabase
                  ports:
                    - "5432:5432"
                  volumes:
                    - db_data:/var/lib/postgresql/data
                app:
                  build:
                    context: .
                    dockerfile: Dockerfile
                  restart: always
                  depends_on:
                    - db
                  environment:
                    DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase
                  ports:
                    - "5000:5000"
                  volumes:
                    - ./:/usr/src/app
                  command: bash -c "pip install -r requirements.txt && flask run --host=0.0.0.0"
              volumes:
                db_data:
              
  • Django Application (django): This example showcases the deployment of a Django application using Docker Compose.

    • Source: tree/master/django
    • Configuration: The docker-compose.yml file defines the services for the Django application, including the Django application container, a PostgreSQL database, and a Redis instance for caching.
    • Code:
    version: '3.7'
              services:
                db:
                  image: postgres:12
                  restart: always
                  environment:
                    POSTGRES_USER: myuser
                    POSTGRES_PASSWORD: mypassword
                    POSTGRES_DB: mydatabase
                  ports:
                    - "5432:5432"
                  volumes:
                    - db_data:/var/lib/postgresql/data
                redis:
                  image: redis:latest
                  restart: always
                app:
                  build:
                    context: .
                    dockerfile: Dockerfile
                  restart: always
                  depends_on:
                    - db
                    - redis
                  environment:
                    DATABASE_URL: postgres://myuser:mypassword@db:5432/mydatabase
                    REDIS_URL: redis://redis:6379
                  ports:
                    - "8000:8000"
                  volumes:
                    - ./:/usr/src/app
                  command: bash -c "pip install -r requirements.txt && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
              volumes:
                db_data:
              
  • Golang Application (golang): This example showcases the deployment of a Go application using Docker Compose.

    • Source: tree/master/golang
    • Configuration: The docker-compose.yml file defines the services for the Go application, including the Go application container and a MySQL database.
    • Code:
    version: '3'
              services:
                db:
                  image: mysql:5.7
                  restart: always
                  environment:
                    MYSQL_ROOT_PASSWORD: "root"
                    MYSQL_DATABASE: "mydatabase"
                  volumes:
                    - db_data:/var/lib/mysql
                app:
                  build:
                    context: .
                    dockerfile: Dockerfile
                  restart: always
                  depends_on:
                    - db
                  environment:
                    DATABASE_HOST: db
                    DATABASE_NAME: mydatabase
                    DATABASE_USER: root
                    DATABASE_PASSWORD: root
                  ports:
                    - "8080:8080"
                  volumes:
                    - ./:/usr/src/app
                  command: bash -c "go mod vendor && go build && ./my-app"
              volumes:
                db_data:
              

These examples illustrate how Docker Compose can be effectively used to deploy various applications and services. They provide a starting point for building your own Docker Compose applications and understanding the best practices for configuration and deployment.