前言
本例演示一个通过 Docker Compose 部署的 demo 项目。其中分别有两个 Java Web 容器服务,以及一个 Nginx 用于反向代理的容器服务。
项目架构
项目模块服务
demo项目结构
demo项目分为demo-admin和demo-api两个模块,分别是运行在1010端口和2020端口的Web服务。
demo-admin的Dockerfile
FROM openjdk:8u151-jre-alpine
RUN mkdir -p /project/demo/demo-admin
WORKDIR /project/demo/demo-admin
COPY ./target/demo-admin-0.0.1-SNAPSHOT.jar ./
EXPOSE 1010
CMD java -jar demo-admin-0.0.1-SNAPSHOT.jar
demo-api的Dockerfile
FROM openjdk:8u151-jre-alpine
RUN mkdir -p /project/demo/demo-api
WORKDIR /project/demo/demo-api
COPY ./target/demo-api-0.0.1-SNAPSHOT.jar ./
EXPOSE 2020
CMD java -jar demo-api-0.0.1-SNAPSHOT.jar
docker-compose.yml
build: context
,指定docker build
时的上下文路径build: dockerfile
,上下文路径下的Dockerfile文件image
,编排创建的来源镜像,如果有使用build指令,那么将作为docker build -t
后构建的镜像名container_name
,运行镜像容器时,命名的容器的名称ports
,端口映射depends_on
,容器依赖,指定后需要已运行启动依赖的容器服务links
,容器连接,相当于docker run --link
version: "3"
services:
demo-admin:
build:
context: ./demo-admin
dockerfile: ./Dockerfile
image: demo-admin:1.0
container_name: demo-admin
ports:
- 1001:1010
demo-api:
build:
context: ./demo-api
dockerfile: ./Dockerfile
image: demo-api:1.0
container_name: demo-api
ports:
- 1002:2020
nginx:
image: nginx:1.21.5-alpine
container_name: nginx
ports:
- 80:80
depends_on:
- demo-admin
- demo-api
links:
- "demo-admin"
- "demo-api"
编排容器服务
docker-compose up
### 定位到docker-compose.yml文件所在目录
cd ~/IdeaProjects/demo
### 执行docker-compose up -d,-d后台运行
docker-compose up -d
执行结果:
Creating demo-api ... done
Creating demo-admin ... done
Creating nginx ... done
Attaching to demo-admin, demo-api, nginx
docker-compose images
查看创建的容器对应使用的镜像:
### docker-compose images
Container Repository Tag Image Id Size
-----------------------------------------------------------------
demo-admin demo-admin 1.0 4238324fca85 95.35 MB
demo-api demo-api 1.0 28620dbffffd 95.35 MB
nginx nginx 1.21.5-alpine e08a7adafd85 22.06 MB
停止/启动容器服务
docker-compose stop
### 定位到docker-compose.yml文件所在目录
cd ~/IdeaProjects/demo
### 执行docker-compose stop
docker-compose stop
执行结果:
### docker-compose stop
Stopping nginx ... done
Stopping demo-api ... done
Stopping demo-admin ... done
docker-compose start
### 定位到docker-compose.yml文件所在目录
cd ~/IdeaProjects/demo
### 执行docker-compose start
docker-compose start
执行结果:
### docker-compose start
Starting demo-admin ... done
Starting demo-api ... done
Starting nginx ... done
修改docker-compose.yml重编排
如果容器服务已编排运行后修改了新的docker-compose.yml,那么需要重新编排容器服务;
对于有变动的容器服务,在重编排后会重新创建镜像或容器。
执行结果示例:
### docker-compose up -d
Recreating demo-api ... done
Recreating demo-admin ... done
Recreating nginx ... done
参考
© 版权声明
文章版权归作者所有,未经允许请勿转载,侵权请联系 admin@trc20.tw 删除。
THE END