Docker Compose | 8lovelife's life
0%

Docker Compose

WHAT

Docker Compose是用Python编写的工具,并不包含在Docker Engine安装包中。Docker Compose用于解析YML文件并运行YML中定义的容器服务。Docker Compose命令通过调用Docker API完成多容器的创建启动,使用Docker Compose需要自行安装

Docker Compose VS Docker Stack

  • docker stack 包含在 swarm mode中,swarm mode 已经被集成到Docker Engine中,由GO语言编写。docker compose 需要自行安装,由Python编写
  • docker stack 只支持 version >= 3 的YML定义且部分命令无法生效如 build。docker compose 对YML的不同version处理都没有问题
  • docker stack 可以将服务分发到不同的主机上,docker compose 只能将服务分发到同一台主机上

WHY

为什么要使用Docker Compose ?

服务发布

以下是需要容器化的服务

  • product-service 商品服务
  • shopping-service 商店服务,两套界面
  • redis-service 数据缓存
  • front-balance 商店服务负载

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
BEFORE COMPOSE :

docker run -itd --name redis-service --network=backend -p 16379:6379 redis
docker run -itd --name product-service --network=backend -p 15000:5000 8lovelife/product:latest
docker network connect frontend product-service
docker run -itd --name shopping-service-blue --network=frontend -p 28080:80 8lovelife/shopping:blue
docker run -itd --name shopping-service-red --network=frontend -p 18080:80 8lovelife/shopping:red
docker run -itd --name front-balance --network=frontend -p 80:80 8lovelife/frontbalance:latest

AFTER COMPOSE :

docker-compose -f docker-compose-product.yml up -d


HOW

如何使用Docker Compose ?

服务拆分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
SHOPPING Dockerfile:

FROM node:11.1.0-alpine as build
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY ./ /app/
RUN npm run build

FROM nginx:1.17.3-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY --from=build /app/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]


PRODUCT Dockerfile:

FROM maven:3-jdk-8 as build
RUN mkdir -p /build
WORKDIR /build
COPY pom.xml /build
RUN mvn -B dependency:resolve dependency:resolve-plugins
COPY src /build/src
RUN mvn package -Dmaven.test.skip=true

FROM openjdk:8-jdk-alpine as runtime
EXPOSE 5000
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY --from=build /build/target/*.jar app.jar
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]

。。。

YML定义

本文事例使用的docker-compose-product.yml文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
version: "3"

services:

product-service:
image: "8lovelife/product:latest"
ports:
- "15000:5000"
networks:
- frontend
- backend
environment:
- REDIS_HOST=redis-service
- REDIS_PORT=6379

shopping-service-red:
image: "8lovelife/shopping:red"
ports:
- "18080:80"
networks:
- frontend

shopping-service-blue:
image: "8lovelife/shopping:blue"
ports:
- "28080:80"
networks:
- frontend

redis-service:
image: "8lovelife/redis:products"
ports:
- "16379:6379"
volumes:
- redis_data:/data
networks:
- "backend"

front-balance:
image: "8lovelife/frontbalance:latest"
ports:
- "80:80"
networks:
- "frontend"

networks:
frontend: {}
backend: {}

volumes:
redis_data: {}

构建或拉取镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 绝对路径
build: .

# 相对路径
build: ../path/to/app/

# Git仓库
build: https://url.git

# 指定分支的Git仓库
build: https://url.git#develop

# 含access_token的Git仓库链接
build: https://<ACCESS_TOKEN>:@github.com/demo.git

配置环境变量

1
2
3
4
5
product-service:
image: "8lovelife/product:latest"
environment:
- REDIS_HOST=redis-service
- REDIS_PORT=6379

配置网络

1
2
3
4
5
6
7
8
9
10
11
product-service:
image: "8lovelife/product:latest"
ports:
- "15000:5000"
networks:
- frontend
- backend

networks:
frontend: {}
backend: {}

配置数据卷

1
2
3
4
5
6
7
8
9
10
11
redis-service:
image: "redis:latest"
ports:
- "16379:6379"
volumes:
- redis_data:/data
networks:
- "backend"

volumes:
redis_data: {}

构建运行

1
2
3
docker-compose -f docker-compose-product.yml up -d

docker-compose --help compose命令帮助

If you can’t fix it, you gotta stand it. - Jack

Inception