Skip to main content

Command Palette

Search for a command to run...

Day 18: Docker Compose!

Published
•3 min read•View as Markdown

What is Docker Compose?

Docker Compose is a tool that simplifies the process of defining and running multi-container Docker applications. You use a YAML file to configure your application’s services, networks, and volumes, and then spin them up with a single command.

What is YAML?

YAML stands for "Yet Another Markup Language".
It’s a human-readable data serialization language used for configuration files. YAML files typically have .yml or .yaml extensions.

Today’s Task: Docker Compose Configuration

Docker Compose File

version: '3.8'

services:
  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
      MYSQL_DB: myDb
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: myDb
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:

Breakdown of the Configuration

Version

  • version: '3.8': Uses Docker Compose file format version 3.8, compatible with Docker Engine 19.03.0+.

Services

  1. Backend Service

     backend:
       build:
         context: .
       ports:
         - "5000:5000"
       environment:
         MYSQL_HOST: mysql
         MYSQL_USER: admin
         MYSQL_PASSWORD: admin
         MYSQL_DB: myDb
       depends_on:
         - mysql
    
    • build: Builds the Docker image for the backend service from the current directory (context: .).

    • ports: Maps port 5000 on the host to port 5000 in the container.

    • environment: Sets environment variables for the backend to connect to the MySQL database.

      • MYSQL_HOST: Hostname of the MySQL service (mysql).

      • MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB: Credentials and database name for connecting to MySQL.

    • depends_on: Ensures the MySQL service starts before the backend service.

  2. MySQL Service

     mysql:
       image: mysql:5.7
       ports:
         - "3306:3306"
       environment:
         MYSQL_ROOT_PASSWORD: root
         MYSQL_DATABASE: myDb
         MYSQL_USER: admin
         MYSQL_PASSWORD: admin
       volumes:
         - ./message.sql:/docker-entrypoint-initdb.d/message.sql
         - mysql-data:/var/lib/mysql
    
    • image: Uses the MySQL 5.7 image from Docker Hub.

    • ports: Maps port 3306 on the host to port 3306 in the container.

    • environment: Configures MySQL with root password, a default database, and a non-root user.

    • volumes:

      • ./message.sql:/docker-entrypoint-initdb.d/message.sql: Mounts a SQL file to initialize the database.

      • mysql-data:/var/lib/mysql: Persists MySQL data using a named volume.

Volumes

volumes:
  mysql-data:
  • How to Set Up and Use the Volume

    Create the Volume

      • Before using the volume in your Docker Compose file, create it using:

           docker volume create mysql-data
        

How to Use This Configuration

  1. Start Services

    • Run docker-compose up to start both backend and MySQL services. This command builds the backend image (if needed), starts the containers, and attaches them to a network

  2. Inspect Services

  3. Test the Application
    Open the browser and browse the Instace IP followed by port number 5000 mapped with the backend container

    Wow it's running

  4. Check Logs

    • Use docker-compose logs to view logs from both services and ensure everything is running smoothly.
  5. Stop Services

    • Run docker-compose down to stop and remove all containers, networks, and volumes defined in the docker-compose.yml file.

Happy Dockerizing! 😃


B

"Hey everyone,I want to share my experience with Catherine E. Russell as a trading manager. She truly stands out with her exceptional skills and expertise in the field. Catherine provides the best trading signals and her wealth of experience ensures success in the volatile trading market.I encourage you all to research her on Facebook and witness firsthand the positive impact she has made in the trading community. With Catherine, you can trust that your investments are in capable hands, and her guidance will lead you towards profitable outcomes.Don't miss out on the opportunity to work with the best. Reach out to Catherine E. Russell today and elevate your trading experience!

1

More from this blog

#90DaysOfDevOps Challenge - Day 1 Devops

21 posts