Docker-Compose是Docker官方的开源项目负责实现对Docker容器集群的快速编排。
官网地址:https://docs.docker.com/compose/compose-file/compose-file-v3
官网下载地址:https://docs.docker.com/compose/install
curl-L."htps:/github com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -version
一文件:docker-compose.yml
两要素:
服务(service)
工程(project)
写好自己的微服务项目,maven package 打成jar包上传到linux服务器上[项目中用到了MySQL、redis]
编写dockerfile
构建镜像
可以参考(https://blog.csdn.net/weixin_49456013/article/details/135409632)
不用Compose有什么缺点?
用Compose
编写docker-compose.yml
version:"3
services:
microService:
image: test_docker:1.6
container_name: ms01
ports: -"6001:6801"
volumes :
- /app/microService:/data
networks :
- test
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 :
- test
command: redis-server /etc/redis/redis.conf
mysql :
imagel: mysq1 :5.7
environment:
MYSOL_ROOT_PASSWORD: '123456'
MYSOL_ALLOW_EMPTY_PASSWORD: 'no'
MYSOL_DATABASE: 'db2021'
MYSOL_USER :'root'
MYSQL_PASSWORD:'123456'
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 :
- test
command: --default-authentication-plugin=mysql_native_password #解决外部无法访问
networks:
test:
将项目中的MySQL 和redis的ip地址换成服务名,重新打包上传,打镜像
即可启动三个容器。
dockers-compose stop
一键启停。