24 Feb 2024




Beginner

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 named web that uses the nginx:alpine image, exposes port 80 on the container to port 8080 on the host, and mounts the ./html directory on the host to /usr/share/nginx/html in the container.
    • app: Another service named app that uses the node:alpine image, 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 ./app directory on the host to /app in 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.

docker
docker-compose