compose说明
- 原生docker命令操作容器比较麻烦
- compose可以把操作容器的命令写入到配置文件中,让我们更方便的操作容器
compose的安装
yum install epel-release -y yum install python-pip -y cd /usr/local/src wget https://github.com/docker/compose/releases/download/1.26.2/docker-compose-Linux-x86_64 mv docker-compose-Linux-x86_64 /usr/bin/docker-compose chmod 755 /usr/bin/docker-compose docker-compose version
或者通过升级python的方式
升级 Python
方法有很多,能正常升级就可以
- 通过 yum 下载最新 python
yum install -y epel-release yum install -y python36
ls -l /usr/bin/python*
rm -f /usr/bin/python
ln -s /usr/bin/python3.6 /usr/bin/python
sed -i '1d' /usr/bin/yum sed -i '1i#!/usr/bin/python2.7' /usr/bin/yum sed -i '1d' /usr/libexec/urlgrabber-ext-down sed -i '1i#!/usr/bin/python2.7' /usr/libexec/urlgrabber-ext-down
python -V
安装 docker-compose
- 安装
curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
- 测试
docker-compose version
compose配置文件:/docker/compose/redis/docker-compose.yml
version: '2' services: redis: image: centos7:redis
compose操作容器,cd进入到docker-compose.yml 所在的目录
- 后台启动容器:docker-compose up -d
- 查看容器:docker-compose ps
- 停止并删除容器:docker-compose down
- 停止并删除容器并且删除所使用的volumes:docker-compose down -v
- 停止启动容器: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
进入到docker-compose.yml所在的目录,compose同个镜像启动多个容器
cd /docker/compose/redis/ docker-compose up -d --scale redis=3 docker-compose exec --index=2 redis bash