Docker container - rapid orchestration of compose container clusters

Table of contents

introduction

1. Introduction to Docker-compose

2. YAML file format and writing considerations

1. YAML file format

2. Things to note about YAML format

3. YAML data structure

3. Docker Compose configuration common fields

4. Docker Compose

5. Compose deployment

 6. Summary


introduction

Dockerfile allows users to manage a single application container; while Compose allows users to define a group of associated application containers (called a project) in a template (YAML format), such as a Web service container plus The database service container on the back end, etc.

1. Introduction to Docker-compose

The Docker-Compose project is an official open source project of Docker developed based on python. It is responsible for realizing the rapid orchestration of Docker container clusters.

Docker-Compose divides the managed containers into three layers, namely project, service and container.

  1. All files in the Docker-Compose running directory (docker-compose.yml, extends files or environment variable files, etc.) form a project. If there is no special specification, the project name is the current directory name.
  2. A project can contain multiple services, and each service defines the image, parameters, and dependencies for container running.
  3. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools are needed to achieve service discovery and load balancing, such as Consul.

The default project configuration file of Docker-Compose is docker-compose.yml . The configuration file can be customized through the environment variable COMPOSE_FILE or the -f parameter, which defines multiple dependent services and the containers in which each service runs.

Using a Dockerfile template file allows users to easily define a separate application container . At work, we often encounter situations where multiple containers need to cooperate with each other to complete a certain task. For example, to implement a Web project, in addition to the Web service container itself, it is often necessary to add a back-end database service container and even a load balancing container.

Compose allows users to define a set of associated application containers as a project through a separate docker-compose.yml template file (YAML format).

The Docker-Compose project is written in Python and calls the API provided by the Docker service to manage containers. Therefore, as long as the platform you are operating on supports the Docker API, you can use Compose for orchestration management.

2. YAML file format and writing considerations

1. YAML file format

  1. YAML is a markup language that can display data serialization format very intuitively and is highly readable.
  2. Similar to json data description language, the syntax is much simpler than json.
  3. YAML data structures are represented by indentation, consecutive items are represented by minus signs, key-value pairs are separated by colons, arrays are enclosed in square brackets [ ], and hashes are enclosed in curly braces { }.

2. Things to note about YAML format

  1. Tab key indentation is not supported, only spaces can be used for indentation
  2. Usually indent 2 spaces at the beginning
  3. Indent 1 space after the character, such as colon: , comma, , horizontal bar -
  4. Comment with # sign
  5. If special characters are included, enclose them in single quotes '' as ordinary characters; if they are enclosed in double quotes "", they represent the meaning of the special characters themselves.
  6. Boolean values ​​must be enclosed in quotation marks ""
  7. case sensitive

3. YAML data structure

对象: 键值对的字典
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',
  ]
}
wAAACH5BAEKAAAAAAAAAABAAEAAAICRAEAOw==

3. Docker Compose configuration common fields

Field describe
build dockerfile context Specify Dockerfile file name and image context path
images

Specify image

command Execute the command, overwriting the default command
container name Specify the container name. The container name is unique. If you specify a custom name, you cannot scale it.
deploy Specify configurations related to deployment and running services, which can only be used in Swarm mode.
environment Add environment variables
networks Join the network
ports Expose container port, same as -p, but port cannot be lower than 60
volumes Mount host path or command ticket
restart Restart strategy, default no, always, no-failure, unless-stopped
hostname Container hostname

4. Docker Compose

Field describe
build Rebuild the service (batch update images)
ps list containers
up Create and start containers
exec Execute commands inside the container
scale Specify the number of service containers to start
top Show container processes
logs View container output
down Delete containers, networks, volumes and images
stop/start/restart Stop/start/restart services

5. Compose deployment

Docker-Compose is an independent product of Docker, so you need to install Docker and then install Docker Compose separately.

//环境部署所有主机安装docker环境(内容为docker基础)
yum install docker-ce -y
//下载compose(上传docker_compose)
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
cp -p docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose

mkdir /root/compose_nginx

yum install -y tree

vim /root/compose_nginx/docker-compose.yml

version: '3'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
       - 1216:80
       - 1217:443
    networks:
       - cluster     
    volumes:
       - ./wwwroot:/usr/local/nginx/html
   networks:
      cluster:
wAAACH5BAEKAAAAAAAAAABAAEAAAICRAEAOw==

mkdir nginx
mkdir wwwroot
echo "shenxianhaishiniude" > wwwroot/index.html
cat wwwroot/index.html
cd nginx
wAAACH5BAEKAAAAAAAAAABAAEAAAICRAEAOw==

vim Dockerfile 

FROM centos:7 as build
ADD nginx-1.20.2.tar.gz /mnt
WORKDIR /mnt/nginx-1.120.2
RUN yum install -y gcc pcre pcre-devel devel zlib-devel make &> /dev/null && \
 yum clean all && \
 sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && \
 ./configure --prefix=/usr/local/nginx &> /dev/null && \
 make &>/dev/null && \
 make install &>/dev/null && \
 rm -rf /mnt/nginx-1.20.2

FROM centos:7
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
COPY --from=build /usr/local/nginx /usr/local/nginx
CMD ["/usr/local/nginx/sbin/nginx","daemon off;"]
wAAACH5BAEKAAAAAAAAAABAAEAAAICRAEAOw==

tree ./

  

docker-compose -f docker-compose.yml up -d
docker network ls

 6. Summary

  1. The project configuration file of Docker-Compose defaults to docker-compose.yml
  2. Using a Dockerfile template file allows users to easily define a separate application container.
  3. Compose allows users to define a set of associated application containers as a project through a separate docker-compose.yml template file (YAML format).

Guess you like

Origin blog.csdn.net/weixin_71429844/article/details/127429332