- 前提须知: docker基础入门
概念
安装
- 必须先安装docker后 才可安装docker-compose
# 安装
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# 设置文件可执行权限
chmod +x /usr/local/bin/docker-compose
# 查看版本信息
docker-compose -version
操作
- 创建docker-compose目录
mkdir ~/docker-compose
cd ~/docker-compose
- 编写 docker-compose.yml 文件
- 演示模板
- 实际模板演示
- 这里的
nginx
和app
名是自定义的
version: '3'
services:
nginx:
image: nginx
ports:
- 80:80
links:
- app
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
app:
image: app
expose:
- "8080"
- 实战参考
version: '3'
services:
xk-admin:
image: xk/xk-admin:1.0-SNAPSHOT
container_name: xk-admin
ports:
- 7616:7616
volumes:
- /data/app/xk-admin/logs:/var/logs
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
- redis:redis
- elasticsearch:es
xk-portal:
image: xk/xk-portal:1.0-SNAPSHOT
container_name: xk-portal
ports:
- 7618:7618
volumes:
- /data/app/xk-portal/logs:/logs/
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
- redis:redis
- elasticsearch:es
xk-api:
image: xk/xk-api:1.0-SNAPSHOT
container_name: xk-api
ports:
- 7778:7778
volumes:
- /data/app/xk-api/logs:/logs/
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #可以用nacos-registry这个域名访问nacos服务
- redis:redis
- elasticsearch:es
xk-gateway:
image: xk/xk-gateway:1.0-SNAPSHOT
container_name: xk-gateway
ports:
- 7777:7777
volumes:
- /data/app/xk-gateway/logs:/logs/
environment:
- 'TZ="Asia/Shanghai"'
external_links:
- nacos-registry:nacos-registry #鍙互鐢╪acos-registry杩欎釜鍩熷悕璁块棶nacos鏈嶅姟
- 在该compose文件的目录下执行以下代码则执行服务编排(若后面加
-d
则后台运行)
docker-compose up
- 启动具体服务
docker-compose-service.yml
是compose配置文件,xk-api
是配置文件内对具体服务的自定义命名
docker-compose -f docker-compose-service.yml up -d xk-api
- 查看服务运行状态
docker-compose ps
- 重启所有服务
docker-compose restart
- 开启所有服务
docker-compose start
- 停止所有服务
docker-compose stop
- 开启 myApp 服务
docker-compose start myApp
- 停止 myApp 服务
docker-compose stop myApp
- 删除所有服务
docker-compose rm