Docker Advanced: Compose Container Orchestration

insert image description here

1. Overview of Docker Compose

Compose is a software launched by Docker, which can manage multiple Docker containers to form an application. We only need to define a YAMLconfiguration file in a format docker-compose.yaml to configure the calling relationship between multiple containers , and finally only need one command to control the startup/shutdown of these containers at the same time. Compose allows users to define a set of containers as a project through a single docker-compose.yamltemplate file.

for example:

Suppose we use many component technologies such as Redis, Mysql, nginx, etc. in a project, then the corresponding instance of the Docker container will become very messy. If one of the microservices of the order needs to run, then the order service can run normally only after all the containers in front of Redis, Mysql... must be started. So far, two problems have been faced:

①There are many and complex container instances
②The start and stop of containers and the start sequence between them need reasonable management

At this time, we need a medium to manage the collaboration between container instances, and Docker Compose reasonably solves the problems we face. When we want to start the container, we only need to execute one command to start all the instances, and we don’t need to go to the docker run again for each container...

PS: The latest version of Docker itself already comes with a tool called Compose, which can be used directly.

insert image description here

2. Use Docker Compose

The steps used by Docker Compose are as follows:

① Write a Dockerfile to define each microservice application and build a corresponding image file

② Use docker-compose.yaml to define a complete business unit and arrange each container service in the overall application

③ Execute the docker-compose up command to start and run the entire program to complete one-click deployment

insert image description here

3. Commonly used commands

  • Create and start the docker-compose service
docker-compose up
# 后台运行
docker-compose up -d
  • Stop and delete containers, networks, volumes, images
docker-compose down
  • Enter inside the container instance
docker-compose exec <yml里面的服务id> /bin/bash
  • Display all containers currently orchestrated by docker-compose
docker-compose ps
  • Show the current container process orchestrated by docker-compose
docker-compose top
  • View container output logs
docker-compose log <yml里面的服务id>
  • check configuration
docker-compose config
# 有问题才输出
docker-compose config -q
  • restart service
docker-compose restart
  • Start the service: (similar to docker start)
docker-compose start
  • Out of service
docker-compose stop

4. Orchestrating Microservices

Next, through a case study to use Compose to arrange microservices, the specific operations are as follows:

Ⅰ. Build microservices

① Create a test database table: t_user

CREATE TABLE `t_user` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '用户名',
  `password` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '密码',
  `sex` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '性别 0=女 1=男 ',
  `deleted` TINYINT(4) UNSIGNED NOT NULL DEFAULT '0' COMMENT '删除标志,默认0不删除,1删除',
  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='用户表'

② Build a SpringBoot test project

insert image description here
Write a configuration file

server.port=6001
# ========================alibaba.druid相关配置=====================
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://47.109.24.39:3306/docker_boot?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.druid.test-while-idle=false
# ========================redis相关配置=====================
spring.redis.database=0
spring.redis.host=47.109.24.39
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
# ========================mybatis相关配置===================
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.zhao.docker.entities
# ========================swagger=====================
spring.swagger2.enabled=true

After building the service, we write two test Controllers (here mainly introduces the use of Compose, and the specific Java code is not too tangled):

@Api(description = "用户User接口")
@RestController
@Slf4j
public class UserController {
    
    
    @Resource
    private UserService userService;

    @ApiOperation("新增用户 新增3条")
    @RequestMapping(value = "/user/add",method = RequestMethod.POST)
    public void addUser(){
    
    
        for (int i = 0; i <=3 ; i++) {
    
    
            User user = new User();
            user.setDeleted((byte)0);
            user.setUpdateTime(new Date());
            user.setCreateTime(new Date());
            user.setUsername("zp"+i);
            user.setPassword(IdUtil.simpleUUID().substring(0,6));
            user.setSex((byte) new Random().nextInt(2));
            userService.addUser(user);
        }
    }

    @ApiOperation("查询用户")
    @RequestMapping(value = "/user/find/{id}",method = RequestMethod.GET)
    public User findUser(@PathVariable Integer id){
    
    
        return userService.findUserById(id);
    }
}

③ Use maven to package the compiled SpringBoot project and put it in the linux server

insert image description here

Ⅱ. Write Dockerfile to build image

Write a Dockerfile to build the SpringBoot project as a mirror, the command is as follows:

# 基础镜像 java
FROM java:8
# 作者 xiaozhao
MAINTAINER xiaozhao
# 指定临时文件位 /temp
VOLUME /temp
# 将jar包添加到容器 更名为zp_docker
ADD docker_boot-0.0.1-SNAPSHOT.jar zp_docker.jar
# 运行 jar
RUN bash -c 'touch /zp_docker.jar'
ENTRYPOINT ["java","-jar","/zp_docker.jar"]
# 暴露6001端口
EXPOSE 6001

Put the prepared Dockerfile in the same directory as the SpringBoot project

insert image description here
Execute the command to build the image

insert image description here

insert image description here

Ⅲ. Start the container and test the service

Start the container and use Swagger for testing

insert image description here

The access to Swagger is successful, and the microservice is built.

insert image description here

Ⅳ. Use Compose to arrange containers

① Write the docker-compose.yml file in the same directory as the Dockerfile

# 版本
version: "3"

#服务容器实例
services:
# 服务名
  microService:
  	# 镜像名 + 版本号
    image: zp_docker:1.0
    # 容器名称
    container_name: my01
    # 端口配置
    ports:
      - "6001:6001"
    # 容器数据卷
    volumes:
      - /app/microService:/data
    # 网络(一下容器运行在同一个网段内)
    networks: 
      - xiaozhao_net 
     # 依赖的容器,服务的启动依赖
    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: 
      - xiaozhao_net 
    command: redis-server /etc/redis/redis.conf


  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
      MYSQL_DATABASE: 'docker_boot'
    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:
      - xiaozhao_net 
    command: --default-authentication-plugin=mysql_native_password #解决外部无法访问
networks: 
   xiaozhao_net : 

insert image description here

② Modify the configuration in the microservice, and replace the IP addresses of mysql and redis with the service names in the docker-compose.yml file. Avoid problems caused by downtime and restart IP changes.

server.port=6001
# ========================alibaba.druid相关配置=====================
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://47.109.24.39:3306/docker_boot?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.url=jdbc:mysql://mysql:3306/docker_boot?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.druid.test-while-idle=false
# ========================redis相关配置=====================
spring.redis.database=0
#spring.redis.host=47.109.24.39
spring.redis.host=redis
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
# ========================mybatis相关配置===================
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.zhao.docker.entities
# ========================swagger=====================
spring.swagger2.enabled=true

③ Repackage the microservices into the linux server

insert image description here

④ Rebuild the image

insert image description here
⑤ Execute the command and check the docker-compose.yml configuration

docker-compose config -q

insert image description here

No errors were reported and the configuration was normal.

⑥ Execute the startup command to start all containers with one click

# 后台启动所有容器
docker-compose up -d

insert image description here
When the container name is not set in the docker-compose.yml file, the default is:当前文件所处的目录名称_镜像名_容器数量

⑦ Check the container startup status, you can see that all containers start successfully

insert image description here

⑧ Use Swagger to test the service

Test add user function

insert image description here
insert image description here

Test query function

insert image description here

The test is successful, the functional interface is normal, Mysql and redis services are normal! !

V. Summary

In the above test projects, we used redis, Mysql components, plus the built SpringBoot project image, a total of three corresponding containers. When we don't use Compose to arrange services, we always encounter the following problems:

  1. The sequence of starting containers is required to be fixed. Mysql and Redis must be started first to successfully access microservices.
  2. Multiple run commands are executed on each startup.
  3. The start-stop or downtime between containers may cause the container instance corresponding to the IP address to change and cause mapping errors.

With Compose, the management between containers is very smooth. This is the end of the shared article. I hope it will be helpful to everyone. Brothers, use it quickly!

insert image description here

Guess you like

Origin blog.csdn.net/Zp_insist/article/details/130276552