介绍
Docker Compose通过一个单独的docker-compose.yml模板文件(YAML格式)来定义一组相关联的应用容器,帮助我们实现多个相互关联的Docker容器的快速部署。
如:springboot+mysql+nginx 如果一个个去部署他会非常的麻烦,这时候可以选择Docker Compose一键部署
文件名称:docker-compose.yml
MySQL
powershell
docker run \
--name=mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-v mysql_data:/var/lib/mysql \
--restart unless-stopped \
-d \
mysql:8.0
Nginx
powershell
docker run -d --name nginx \
-v /etc/local/nginx/dist:/usr/share/nginx/html \
-p 80:80 \
--restart always \
nginx
SpringBoot
powershell
docker run -d --name java-app -p 8080:8080 --restart=always app
如果一个个去进行部署会非常麻烦,这时候可以打包成一个Docker Compose一键部署
yaml
version: "3.8"
services:
mysql:
image: mysql
container_name: mysql
ports:
- "3306:3306"
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123
volumes:
- "./mysql/conf:/etc/mysql/conf.d"
- "./mysql/data:/var/lib/mysql"
- "./mysql/init:/docker-entrypoint-initdb.d"
networks:
- hm-net
hmall:
build:
context: .
dockerfile: Dockerfile
container_name: hmall
ports:
- "8080:8080"
networks:
- hm-net
depends_on:
- mysql
nginx:
image: nginx
container_name: nginx
ports:
- "18080:18080"
- "18081:18081"
volumes:
- "./nginx/nginx.conf:/etc/nginx/nginx.conf"
- "./nginx/html:/usr/share/nginx/html"
depends_on:
- hmall
networks:
- hm-net
networks:
hm-net:
name: hmall
创建并启动
需要在当前目录下
powershell
docker-compose up -d
查看容器的状态
powershell
docker-compose ps
停止容器
powershell
docker-compose top
删除并移除网络
powershell
docker-compose down