What is Docker Compose?

» What is Docker Compose?

  1. Docker Compose is a tool you can use to define and share multi-container applications.

  2. Docker Compose simplifies the process of managing complex applications that require multiple containers, enabling you to define and control your application’s entire environment using a single configuration file.

  3. With Docker Compose, you can define multiple objects like services, networks, and volumes required for your application.

  4. This all is mentioned in a YAML file called docker-compose.yml. This file describes the containers like our front-end or back-end that make up our application, and their dependencies, configuration options, and other related settings.

» Install Docker Compose

  1. The one way to get Docker Compose is to install Docker Desktop. If you have Docker Desktop installed, Docker Compose should already be available.

  2. Open a Command Prompt or PowerShell window, and Verify the installation by running the command:

docker-compose --version
  1. If the installation is successful, you should see the version information for Docker Compose.
Docker Compose version v2.18.1

OR

  1. If you don’t have a docker desktop and just want to install docker-compose as a standalone visit Docker website. and just Follow the process.

» What Is YAML?

  1. YAML stands for YAML Ain’t Markup Language.

  2. YAML uses indentation and simple syntax, relying on spaces and new lines, making it easy for to read and write.

  3. YAML supports various data types, including scalars (strings, numbers, booleans), sequences (arrays/lists), and mappings (key-value pairs).

  4. It also supports more complex structures like nested data structures and references.

» Create Docker File For Frontend and Backend.

  1. This is to-do list application that uses node.js as backend, Mongodb as the database and react as a frontend.

  2. Create a docker file for backend, like this

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3001

CMD ["node", "server.js"]
  1. Create a docker file for frontend, like this
FROM node:14-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

» Create a Docker-Compose File in root Directory.

version: "3.8"
services:
  web:
    build: ./frontend
    depends_on:
      - api
    ports:
      - "3000:3000"
    networks:
      - network-backend
  api:
    build: ./backend
    depends_on:
      - mongo
    ports:
      - "3001:3001"
    networks: 
     - network-backend
  
  mongo:
    image: mongo
    restart: always
    volumes: 
      - mongodb_data:/data/db
    environment: 
      MONGODB_INITDB_ROOT_USERNAME: username
      MONGODB_INITDB_ROOT_PASSWORD: password
    ports:
      - 27017:27017
    networks: 
     - network-backend

networks:
  network-backend:

volumes: 
  mongodb_data:

» Run the Application

  1. To run the application, we have a command:
docker compose up

OR

docker compose up -d
  1. To stop the application, we have command
docker compose down

OR

  1. Just Click CTRL + C in the terminal where you started the application.

» Conclusion

  1. And That’s it! You’ve successfully created a docker compose file. One thing I wanted to share is that You can use a docker-compose file even if you only have a frontend or backend application.

  2. Docker Compose is a tool for defining and running multi-container Docker applications, where you can specify the services, networks, and volumes required for your application.

  3. While it is commonly used to define and manage multi-service architectures, where multiple containers work together, Docker Compose can still be useful for single-service applications, such as a frontend-only or backend-only application.


I hope this cleared - What is Docker Compose, and how to create a docker-compose file Thank You and Happy coding!😊