8. Docker-compose container orchestration

1. What is Docker compose?
Insert image description here

Compose is a tool software launched by Docker Company, which can manage multiple Docker containers to form an application. You need to define a configuration file docker-compose.yml in YAML format and write the calling relationships between multiple containers. Then, with just one command, you can start/stop these containers simultaneously.

Docker Compose is Docker's official open source project, responsible for realizing the rapid orchestration of Docker container clusters.

2. What can Docker compose do?
Docker recommends that we only run one service in each container. Because the docker container itself takes up very few resources, it is best to separate each service separately. But then we face another problem?

If I need to deploy multiple services at the same time, do I have to write a separate Dockerfile for each service and then build the image and container? This is exhausting, so docker officially provides us with the docker-compose multi-service deployment tool.

For example, to implement a Web microservice project, in addition to the Web service container itself, it is often necessary to add the back-end database mysql service container, redis server, registration center eureka, and even load balancing containers, etc.

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

You can easily define a multi-container application with a configuration file, then use a single command to install all the dependencies of the application and complete the build. Docker-Compose solves the problem of how to manage and orchestrate containers.

3. Install Compose

1. Docker Compose 环境安装
Docker ComposeDocker 的独立产品,因此需要安装 Docker 之后在单独安装 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
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

Compose core concepts

4. Compose core concept
One file
docker-compose.yml

Two elements
: service: each application container instance, such as order microservice, inventory microservice, mysql container, nginx container or redis container

Project: A complete business unit consisting of a set of associated application containers, defined in the docker-compose.yml file.

5. Three steps for using Compose:
Write Dockerfile, define each microservice application and build the corresponding image file.
Use docker-compose.yml to define a complete business unit and arrange each container service in the overall application.
Finally, execute the docker compose up command. to start and run the entire application and complete one-click deployment and go online

6. Compose common commands

docker compose -h         # 查看帮助

docker compose up         # 启动所有docker-compose服务


docker compose up -d      # 启动所有docker-compose服务并后台运行


docker compose down       # 停止并删除容器、网络、卷、镜像。

docker compose exec  yml里面的服务id      # 进入容器实例内部  docker-compose exec docker-compose.yml文件中写的服务id /bin/bash

docker compose ps   # 展示当前docker-compose编排过的运行的所有容器

docker compose top    # 展示当前docker-compose编排过的容器进程

docker compose logs  yml里面的服务id      # 查看容器输出日志

docker compose config        # 检查配置

docker compose config -q    # 检查配置,有问题才有输出


docker compose restart      # 重启服务


docker compose start        # 启动服务

docker compose stop         # 停止服务

Write docker-compose.yml to implement microservice publishing steps

1. Create the folder /app in the root directory
2. Create a new docker-compose.yml file and three folders nacos, user, and order in the app directory
3. Build the nacos service in the three folders nacos, user, and order. Image, user service image, order service image, take building nacos service image as an example, create a new dockerfile file in the nacos folder and upload the runnable jar package of the naco service to this directory (Note: By default, Compose uses the service name as hostname is accessed by other containers), the content of the dockerfile file is as follows

# 基于哪个镜像
From java:8
# 将本地文件夹挂载到当前容器
VOLUME /tmp
# 复制文件到容器
ADD nacos-server-0.0.1-SNAPSHOT.jar /app.jar
# 声明需要暴露的端口
EXPOSE 8761
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-jar","/app.jar"]

Copy after login

version: '2'              #docker的文件格式版本
services:
  eureka:                 #docker服务名
    image: nacos    #docker镜像
    ports:
      - "8848:8848"
  user: 
    image: user
    ports:  
      - "8000:8000" 
  order:    
    image: order
    ports: 
      - "8010:8010"

Start all microservices

Add -d after the command to start it in the background:
  docker-compose up

Example

1. Package a springboot project as an image
1. Prepare a springboot project
and write a test Controller for testing.

@RestController
@RequestMapping
public class TestController {
    
    

    @GetMapping("/test")
    public String test(){
    
    
        return "启动成功 ===================test";
    }
}

2. Manually build the jar package.
We use the maven package command to manually package and upload it to the server.

[root@localhost ~]# cd mydocker/
[root@localhost mydocker]# ll
total 17084
-rw-r--r--. 1 root root 17491177 Apr  9 14:18 demo-0.0.1-SNAPSHOT.jar

3. Write Dockerfile

# 基础镜像使用java
FROM centosjava8:1.0
# 作者
MAINTAINER cxf
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为cxf_docker.jar
ADD demo-0.0.1-SNAPSHOT.jar /cxf_docker.jar
# 运行jar包
RUN bash -c 'touch /cxf_docker.jar'
ENTRYPOINT ["java","-jar","/cxf_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 8088

4. Build the image

docker build -t mydemo:1.0 .

[root@localhost mydocker]# docker images
REPOSITORY                    TAG       IMAGE ID       CREATED          SIZE
mydemo                        1.0       7124eca083ad   26 seconds ago   764MB
centosjava8                   1.0       4dbff0755585   7 hours ago      747MB

5. Run the container
docker run -d -p 8088:8088 7124eca083ad

2. Writing the docker-compose.yml file
requires a separate folder! It is equivalent to each parameter of the corresponding docker run command, and it is very simple to understand!

# 版本
version: "3"
# 写死,所有的服务
services:
  # 服务名,这是我们的springboot项目,需要使用redis+mysql
  microService:
    # 镜像名
    image: cxf_docker:1.0
    # 如果不加的话,会目录前缀+服务名+1
    container_name: ms01
    ports:
      - "6001:6001"
    # 容器数据卷映射
    volumes:
      - /app/microService:/data
    # 网络,会默认加上目录前缀
    networks: 
      - my_network
    # 依赖于mysql与redis
    depends_on: 
      - redis
      - mysql
  # 服务名,可以用该名字访问网络
  redis:
    image: redis:6.0.8
    ports:
      - "6379:6379"
    volumes:
      - /app/redis/redis.conf:/etc/redis/redis.conf
      - /app/redis/data:/data
    networks: 
      - my_network
    command: redis-server /etc/redis/redis.conf
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'db2021'
      MYSQL_USER: 'zzyy'
      MYSQL_PASSWORD: 'zzyy123'
    ports:
       - "3306:3306"
    volumes:
       - /app/mysql/db:/var/lib/mysql
       - /app/mysql/conf/my.cnf:/etc/my.cnf
       - /app/mysql/init:/docker-entrypoint-initdb.d
    networks:
      - my_network
    command: --default-authentication-plugin=mysql_native_password #解决外部无法访问
# 网络,会默认加上目录前缀
networks: 
   my_network: 


3. Start docker-compose
and execute it in the directory where docker-compose.yml is located.

# 检查配置,有问题才有输出
docker-compose config -q
# 执行 
docker-compose up
# 或者后台执行 
docker-compose up -d

If the image does not exist locally, it will be automatically pulled and then run! Very convenient!

4. Stop

# 停止服务
docker-compose stop

Guess you like

Origin blog.csdn.net/weixin_45817985/article/details/133386899