Docker Compose container management orchestration

  Before you start, you need to understand what Docker Compose is.

  If we now have dozens or hundreds of microservices that need to be deployed, if we manually go to the buid to build the image, that workload; or if one of our services has a problem, it goes down, and needs to be restarted, but this service has Other dependencies, what should we do at this time; Docker Compose is to solve this kind of problem

  So what exactly is Docker Compose? The picture below is from the official document, which roughly means: define and configure multiple containers through yaml files, and start them through commands. We can use it to organize the containers.
Insert picture description here
  Then we will start using Docker Compose. Before using it, Need to know, Docker Compose is just an open source project of Docker, and it is not integrated in Docker, so we need to install Docker Compose first

# 下载Docker Compose,官方的下载地址太慢了,所以我重新找了一个
curl -L https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# 给 Docker Compose 文件可执行权限
chmod +x docker-compose
# 查看版本

  Then you can use Docker Compose, the official gave a quick start python single page application, interested friends can try ( Portal )

  Next, first understand the docker-compose.yaml rule, which is the core of the whole ( official document ), which can be divided into three layers to arrange

# 第一层 版本,与docker 引擎版本对应的,向下兼容
version: ''

# 第二层 服务
service:
	服务1:
		服务配置
	服务2:
		服务配置
	.....

# 第三层 其他配置 网络、卷、全局规则
volumes:
netwoeks:
configd:
.....

  Next, write your own service and go online. First, write your own service, just write a hello world.

# 接口
@RestController
public class HelloController {
    
    
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/")
    public String hello() {
    
    
        Long views = redisTemplate.opsForValue().increment("views");
        return "hello world! 当前浏览量:" + views;
    }
}

# yaml配置,注意,现在是连接redis,相当于在局域网内通过域名访问Redis,但是现在在本地肯定是运行不了的
server:
  port: 8080
spring:
  redis:
    host: redis

  Then write the dockerfile file to generate a mirror

FROM java:8
COPY *.jar /hello.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/hello.jar","&"]

  Now we also need a docker-compose.yaml file, through this file to build the image with one click, the service is started, just like the docker run container instructions are written to the file

# 版本
version: '3.8'
# 需要构建的镜像
services:
  # hello镜像,我们自己的服务
  hello:
    # 构建当前路径下的dockerfile
    build: .
    # 也可以执行构建的文件
#    dockerfile: Dockerfile
    # 指定镜像名
    image: hello
    # 依赖的服务镜像
    depends_on:
      - redis
    # 映射的端口
    ports:
      - "8083:8080"

  # redis 服务
  redis:
    # redis的镜像
    image: "library/redis:alpine"

  The next step is to package the service and upload it to the server. At least there are three files: service jar, dockerfile, and docker-compose.yaml. If you have other files and upload them,
Insert picture description here
  you can start building the image and run it now. Through the "docker-compose up" command, you can see that after execution, it will automatically pull the image and create the container. If there is no error or exited, the project is running successfully

# 构建镜像并在前台运行
docker-compose up
# 重新构建
docker-compose up --build
# 构建镜像并在后台运行
docker-compose up -d
# 停止,如果是前台运行,可以直接在当前控制台按 CTRL + C 停止
docker-compose down

Insert picture description here
  Next, we will test our interface. First look at the container. This is the container we created through docker-compose. Then we visit our interface. You can see that you can access and get data from Redis.
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/109188002