Back to Docker Course

Docker Course

What is Docker Compose?

Docker Compose is a tool designed to simplify the management of applications that use multiple containers. Nowadays, many projects are built using microservices architecture, which often requires running several containers simultaneously, such as those for frontend applications, backend APIs, and databases.

Manually creating and managing these containers can be complex and time-consuming. Docker Compose addresses this challenge by allowing you to define and manage all your containers in a single configuration file called docker-compose.yml.

This YAML file includes the version of Docker Compose being used, a list of services (each representing a container), network configurations, and volume settings. The docker-compose.yml file simplifies setup and deployment by providing all necessary container information in one place.

Installing Docker Compose in Amazon Linux

  1. download docker compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

  1. Give permission

$ sudo chmod +x /usr/local/bin/docker-compose

  1. How to check docker compose is installed or not

$ docker-compose --version

Creation of docker-compose.yml file

version: "3" services: backend: build: context: . dockerfile: Dockerfile ports: - "8080:8080" depends_on: - mysql-db environment: SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db:3306/taskmanagementdb SPRING_DATASOURCE_USERNAME: root SPRING_DATASOURCE_PASSWORD: password-1 mysql-db: image: mysql:latest restart: always environment: MYSQL_ROOT_PASSWORD: password-1 MYSQL_DATABASE: taskmanagementdb ports: - "3306:3306" volumes: - mysql_data:/var/lib/mysql volumes: mysql_data:

Build Command

docker-compose up --build is the correct command to build and start the services defined in your docker-compose.yml file.

Running and Managing Compose Applications

  • Start containers: docker-compose up or docker-compose up -d for detached mode.
  • List running containers: docker-compose ps
  • Display Docker images: docker-compose images
  • Stop and remove containers: docker-compose down.
  • View logs: docker logs -f <container-name>.