What is Docker Compose?
» What is Docker Compose?
-
Docker Compose is a tool you can use to define and share multi-container applications.
-
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.
-
With Docker Compose, you can define multiple objects like services, networks, and volumes required for your application.
-
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
-
The one way to get Docker Compose is to install Docker Desktop. If you have Docker Desktop installed, Docker Compose should already be available.
-
Open a Command Prompt or PowerShell window, and Verify the installation by running the command:
docker-compose --version
- If the installation is successful, you should see the version information for Docker Compose.
Docker Compose version v2.18.1
OR
- 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?
-
YAML stands for YAML Ain’t Markup Language.
-
YAML uses indentation and simple syntax, relying on spaces and new lines, making it easy for to read and write.
-
YAML supports various data types, including scalars (strings, numbers, booleans), sequences (arrays/lists), and mappings (key-value pairs).
-
It also supports more complex structures like nested data structures and references.
» Create Docker File For Frontend and Backend.
-
This is to-do list application that uses node.js as backend, Mongodb as the database and react as a frontend.
-
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"]
- 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
- To run the application, we have a command:
docker compose up
OR
docker compose up -d
- To stop the application, we have command
docker compose down
OR
- Just Click CTRL + C in the terminal where you started the application.
» Conclusion
-
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.
-
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.
-
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!😊