📜  docker-compose.yml (1)

📅  最后修改于: 2023-12-03 14:40:50.677000             🧑  作者: Mango

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a stack of services using a simple YAML file, called docker-compose.yml. This file describes the services, networks, and volumes required for your application.

YAML Syntax

Docker Compose uses YAML syntax for defining the services in your application stack. YAML is a human-readable data serialization format that is commonly used for configuration files. Below is an example of a basic docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "80:80"
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=secret
Services

Services are the building blocks of a Docker Compose application. Each service represents a containerized application, such as a web server or a database. The services section in the docker-compose.yml file defines the different services in your application stack.

In the example above, we have two services:

  1. web: This service builds an image from the Dockerfile in the current directory and exposes port 80 on the host system.
  2. db: This service uses the mysql:5.7 image from Docker Hub and sets the environment variable MYSQL_ROOT_PASSWORD to "secret".
Networks

Docker Compose allows you to define networks that connect the services in your application. By default, a network named default is created for you. You can also define your own custom networks to separate different parts of your application.

Volumes

Volumes are used to persist data generated by containers or share data between containers. With Docker Compose, you can define named volumes or mount host directories as volumes.

Running Docker Compose

To run your Docker Compose application, simply use the docker-compose up command in the same directory as your docker-compose.yml file. Docker Compose will create the necessary network and volumes, and start the services defined in the YAML file.

You can also use other Docker Compose commands, such as docker-compose down to stop and remove the containers, or docker-compose ps to list the running services.

For more details and advanced usage of Docker Compose, refer to the official documentation.

Remember to save the docker-compose.yml file in your project repository to ensure consistent deployment and easy collaboration with other developers.