24 Feb 2024
Docker Compose is a tool used to define and run multi-container Docker applications. It allows you to define the services, networks, and volumes required by your application in a single YAML file, making it easier to manage and deploy complex Dockerized applications.
With Docker Compose, you can define the relationships and dependencies between different containers, specify environment variables, configure networking, and streamline the process of starting, stopping, and scaling your Docker containers as a cohesive unit.
Example of a Docker Compose file:
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
app:
image: node:alpine
command: sh -c "cd /app && npm install && npm start"
volumes:
- ./app:/app
ports:
- "3000:3000"
Explanation:
version: Specifies the version of the Docker Compose file format being used.services: Defines the services that make up your application.web: A service namedwebthat uses thenginx:alpineimage, exposes port 80 on the container to port 8080 on the host, and mounts the./htmldirectory on the host to/usr/share/nginx/htmlin the container.app: Another service namedappthat uses thenode:alpineimage, executes a command to install dependencies and start the application, exposes port 3000 on the container to port 3000 on the host, and mounts the./appdirectory on the host to/appin the container.
With this Docker Compose file, you can use the docker-compose command to manage your application:
- To start the application:
docker-compose up - To start the application in the background:
docker-compose up -d - To stop the application:
docker-compose down
These commands will handle the creation, starting, stopping, and removal of the containers defined in the Docker Compose file.