docker-compose tutorial

Introduction

  • Docker Compose is a tool for defining and running multiple Docker containers. It uses simple YAML files to configure the application's service, network, and volume settings.
  • Use Docker Compose to easily define and manage multi-container applications. You can place different components of the entire application (such as databases, web servers, and application servers) in different containers and use Compose to define the relationships and configuration between them.
  • In a Compose file, you can specify the image, port mapping, environment variables, volume mounting and other settings for each service. You can also define networks so that containers can communicate with each other, and you can use dependencies to ensure that services start in the correct order.
  • Using Docker Compose is very simple. You just create a docker-compose.yaml file and use the docker-compose up command to start your application. Compose will automatically download the required images and start the container according to the configuration.
  • In addition to starting applications, Compose also provides some other useful commands, such as docker-compose down for stopping and removing all related containers, docker-compose ps for View the status of running containers, etc.
  • Overall, Docker Compose is a simple yet powerful tool for defining and managing multi-container applications. It makes it easier to build and manage complex containerized applications in development, testing, and deployment environments.

docker-compose basic concepts

Docker Compose is a tool for defining and running multiple Docker containers. It allows you to use YAML files to configure resources such as services, networks, and volumes required by your application, and manage the entire containerized application life cycle through simple commands.

Basic concepts include the following aspects:

  1. Services: A service in Compose corresponds to a container. A service can contain multiple replicas.
  2. Containers: Compose creates and manages containers based on service configuration. Each container is an instance of a service.
  3. Networks: Compose defines a default network for applications so that services can communicate with each other. You can also create custom networks to isolate services.
  4. Volumes: Compose can mount volumes to containers so that data can be shared and persisted between containers.
  5. Dependencies: You can specify dependencies between services in the Compose file to ensure correct startup order.
    通过编写Compose文件,您可以定义应用程序所需的所有服务、其镜像、端口映射、环境变量、卷等配置。然后,使用docker-compose命令运行、停止、重启或删除整个应用程序。

Why use docker-compose

Docker Compose is a tool for defining and running multiple Docker containers, which simplifies the deployment process of multi-container applications. Using Docker Compose, you can write a YAML file to define the configuration of the application's services, network, and storage volumes, and start, stop, and manage these containers through a command.

Benefits of using Docker Compose include:

  1. Simplified deployment: By defining the configuration of multiple containers in one file, applications can be easily deployed in different environments without having to manually configure multiple containers.
  2. Highly portable: Docker Compose files contain all the dependencies and configuration required for your application, making it easy to recreate the same development and production environments on different hosts.
  3. Flexibility and scalability: Multiple services can be defined and communication between services can be achieved through links or shared networks, supporting custom network configurations and connections between containers.
  4. Consistency and maintainability: Unified management of multiple containers through Docker Compose makes the deployment, starting, stopping, and debugging of the entire application more consistent and maintainable.
    总之,使用 Docker Compose 可以简化多容器应用程序的部署,提高开发效率,增强应用程序的可移植性和可扩展性。

YAML file format and writing considerations

1. YAML file format

YAML (YAML Ain’t Markup Language) is a human-readable data serialization format. It uses space indentation to indicate hierarchical relationships and colons to mark key-value pairs. YAML files usually have .yaml or .yml as the file extension.

The following is an example YAML file format:

# 注释以#开头
key1: value1
key2: value2
# 列表使用短横线 -
list:
  - item1
  - item2
  - item3
# 嵌套结构
nested:
  key3: value3
  key4:
    - item4
    - item5

In this example, there are two key-value pairs key1: value1 and key2: value2, and a list named list , and a nested structurenested containing key-value pairs key3: value3 and a list named key4.

  • The YAML file format is concise and easy to read, and is often used in scenarios such as configuration files, data serialization, and storage of structured data.
  • YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed by square brackets [ ], and bash is enclosed by curly braces { }.

2. Things to note about YAML format

  • Tab key indentation is not supported, only spaces can be used for indentation.
  • Usually indent 2 spaces at the beginning
  • Indent 1 space after the character, such as colon [:], comma [,], horizontal bar [-]
  • Use # sign to indicate comments
  • If special characters are included, use single quotation marks [’ '] to indicate the meaning of the special character itself. If double quotation marks [“ ”] are used to indicate the meaning of the special character itself,
  • Boolean values ​​must be enclosed in [" "]
  • YAML is case sensitive

3. YAML data structure case

#键值对表示
animal:pets
#数组:一组按次序排列的列表
- cat
- dog
- goldfish
#布尔值
debug: "true"
debug: "false"
#yaml实例
languages:        #序列的映射 
  - java
  - Golang
  - Python
websites:         #映射的映射
  Baidu: www.baidu.com
  Wangyi: www.163.com
  Souhu: www.souhu.com
#或者
languages: ["java","Golong","Python"]
websites:
  Baidu:
    www.baidu.com
  Wangyi:
    www.163.com
  Souhu:
    www.souhu.com
#Json格式
{
  languages: [
    'Java',
    'Golong',
    'Python',
  ],
  websites: [
    Baidu: 'www.baidu.com',
    Wangyi: 'www.163.com',
    Souhu: 'www.souhu.com',
  ]
}

Docker Compose configuration common fields

1. Docker Compose configuration common fields

Docker Compose is a tool for defining and running multiple Docker containers. It uses YAML files to configure various parameters of the container. The following are commonly used configuration fields for Docker Compose:

  1. version: Specifies the version number of the Docker Compose file.
  2. services: Define the services to be run, which can contain multiple services.
  3. image: Specify the image to use.
  4. build: Specify the Dockerfile path required to build the image (the Dockerfile file to be specified needs to be specified with the dockefile tag in the sub-label of the build tag).
  5. dockerfile: build image context path.
  6. context: It can be the dockerfile path, or the URL address of the git repository when executed.
  7. hostname: Set kernel parameters within the container.
  8. ports: Specify the port mapping of the container and expose the container port, which is the same as -p, but the port cannot be lower than 60.
  9. volumes: Mount a host directory or command volume to the container. The command volume must define the volume name in top-level volumes.
  10. volumes_from: Mount volumes from another service or container, optional parameters: ro and rw (only supported in version '2')
  11. networks: Specifies the network to which the container is connected.
  12. network-mode: Set the network mode of the container
  13. environment: Set the environment variables of the container.
  14. command: Specifies the command to be executed when the container is started, which will override the command executed by default after the container is started (it will override the CMD instruction in the dockefile)
  15. container_name: Specify the container name. Since the container name is unique, if you specify a custom name, you cannot scale the specified number of containers.
  16. privileged: used to give root permissions to the container. Note that it is unsafe. true.
  17. depends_on: Specifies the dependency relationship between services. This label is used to solve container dependencies and startup issues. If you want to start the application container, you need to start the database container first. php:depends_on:- apache- mysql
  18. restart: Specify whether the container will automatically restart after exiting. Restart strategy: 1. no, default policy, do not restart the container when the container exits. 2. on-failure, will restart only when the container exits abnormally (exit status is non-0). Container 3. on-failure: 3. When the container exits abnormally, restart the container, up to 3 times. 4. always, always restart the container when the container exits, 5. unless-stopped, always restart the container when the container exits, But containers that have been stopped when the Docker daemon is started are not considered.
  19. links: Specify aliases to connect to other services.
  20. expose: Specifies the port exposed by the container, but does not perform host port mapping.
  21. healthcheck: Specifies the health check configuration of the container.
  22. labels: Add labels to the container for metadata description.
  23. logging: Configure container logging.
  24. tmpfs: Mount the temporary file system into the container.
  25. cap_add: Add additional Linux privileges.
  26. cap_drop: Limit the Linux privileges of the container.
  27. devices: Specifies the devices used by the container.
  28. sysctls: Set the kernel parameters of the container.
  29. ulimits: Set resource limits for containers.
  30. cgroup_parent: cgroup parent belonging to the specified container.
  31. stop_grace_period: Specifies the graceful period for the container to stop.
  32. deploy: Specifies configuration related to deployment and running services. It can only be used in swarm mode.
  33. configs: Specifies the configuration file used by the service.
  34. secrets: Specify sensitive data used by the service.
  35. scale: Set the number of replicas of the service.
  36. external_links: Connect to external containers.
  37. extends: Inherits the configuration of other services.

These are some commonly used Docker Compose configuration fields that can be configured according to specific needs. Please note that this is only part of it, Docker Compose has many more advanced features and configuration options.

2. Docker-compose common commands

Running these commands needs to be used in conjunction with docker-compose.
And it must be used in the directory containing the docker-compose.yml file, otherwise an error will be reported.

  1. docker-compose up: Create and start containers
  2. docker-compose down: Stop the container, delete the container, network, data volume and image
  3. docker-compose ps: List containers and view container status
  4. docker-compose logs: View container logs
  5. docker-compose build: Rebuild the service
  6. docker-compose restart: restart the container
  7. docker-compose stop: Stop the container
  8. docker-compose start: start the container
  9. docker-compose rm: delete container
  10. docker-compose exec: Execute commands within the container
  11. docker-compose pull: Pull the image
  12. docker-compose push: push image
  13. docker-compose config: Check the configuration file
  14. docker-compose events: View container events
  15. docker-compose scale: Specify the number of service containers to start
  16. docker-compose top: displays running container processes

Install docker-compose

1. Download and install docker-compose

Usually follow the download address from the official website to downloadcurl -L https://github.com/docker/compose/releases/download/2.17.2/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-composeor use the domestic addresssudo curl -L https://get.daocloud.io/docker/compose/releases/download/2.17.2/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-composeAlthough the download is very fast, it is prone to errors and cannot be used Official website address:docker-compose Link:
Therefore, I specially upload the latest version so far, which is: docker-compose v2.17.2-linux-x86-64

Official website
https://docs.docker.com/compose/compose-file/compose-file-v3/
Official website download
https://docs.docker.com/compose/install/

After downloading, upload it to the directory that has been created on the server: /usr/local/bin/, and then grant permissions:
sudo chmod +x /usr/local/bin/docker -compose
Check the version: docker-compose -v. If the path cannot be found, create a soft link:
sudo ln -s /usr/local/bin /docker-compose /usr/bin/docker-compose
Verify whether the installation is successful:

docker-compose --version  #之前的v1版本命令是docker-compose --version

2.docker-compose uninstall

rm $DOCKER_CONFIG/cli-plugins/docker-compose

Guess you like

Origin blog.csdn.net/feritylamb/article/details/132690056