docker compose 的安装和入门实战

ujm0 2020-05-08

compose说明 

  • 原生docker命令操作容器比较麻烦 
  • compose可以把操作容器的命令写入到配置文件中,让我们更方便的操作容器

compose的安装 

yum install python-pip -y 
pip install docker-compose 
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ docker-compose 
docker-compose version

compose配置文件:/docker/compose/redis/docker-compose.yml 

version: ‘2‘ 
services: 
redis: 
image: centos7:redis

compose操作容器 

  • 后台启动容器:docker-compose up -d 
  • 查看容器:docker-compose ps 
  • 停止并删除容器:docker-compose down 
  • 停止启动容器:docker-compose stop; docker-compose start

docker-compose exec的使用 

docker-compose exec nginx bash

compose管理多个容器docker-compose.yml 

version: ‘2‘ 
services: 
redis: 
image: centos7:redis 
nginx: 
image: centos7:nginx

compose使用端口映射 

version: ‘2‘ 
services: 
redis: 
image: centos7:redis 
ports: 
- "6379:6379" 
nginx: 
image: centos7:nginx 
ports: 
- "8080:80" 
- "9090:80"

compose设置网络模式 

version: ‘2‘ 
services: 
redis: 
image: centos7:redis 
nginx: 
image: centos7:nginx 
network_mode: "host

compose设置文件共享 

version: ‘2‘
services: 
nginx: 
image: centos7:nginx 
network_mode: "host" 
volumes: 
- /centos7/logs:/usr/local/nginx/logs 
- /centos7/apps:/usr/local/nginx/html/apps

compose同个镜像启动多个容器 

  • docker-compose up -d --scale redis=3 
  • docker-compose exec --index=2 redis bash

相关推荐