二、通过Docker-compose来实现(Docker官网实例)
(1)首先第一步需要安装Docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
(2) 编辑docker-compose.yaml
version: '3' services: web: build: . ports: - "5000:5000" networks: - app-net redis: image: "redis:alpine" networks: - app-net networks: app-net: driver: bridge
docker-compose up -d
总结:一二节是实例 约等于官网的实例
详解docker-compose.yml文件
(1)version: ‘3’
表示docker-compose的版本
(2)services
一个service表示一个container
(3)networks
相当于docker network create app-net
(4)volumes
相当于-v v1:/var/lib/mysql
(5)image
表示使用哪个镜像,本地build则用build,远端则用image
(6)ports
相当于-p 8080:8080
(7)environment
相当于-e
docker-compose常见操作(1)查看版本
docker-compose version
(2)根据yml创建service
docker-compose up
指定yaml:docker-compose up -f xxx.yaml
后台运行:docker-compose up
(3)查看启动成功的service
docker-compose ps
也可以使用docker ps
(4)查看images
docker-compose images
(5)停止/启动service
docker-compose stop/start
(6)删除service[同时会删除掉network和volume]
docker-compose down
(7)进入到某个service
docker-compose exec redis sh
scale扩缩容
(1)修改docker-compose.yaml文件,主要是把web的ports去掉,不然会报错
version: '3' services: web: build: . networks: - app-net redis: image: "redis:alpine" networks: - app-net networks: app-net: driver: bridge
(2)创建service
docker-compose up -d
(3)若要对python容器进行扩缩容
docker-compose up --scale web=5 -d docker-compose ps docker-compose logs web
从之前的操作单机向操作集群转变