前言
通过上一节的学习, 学会了如何在 Linux 环境下搭建 Docker 并且部署 Springboot 项目, 并且成功的跑了起来, 当然, 在生产环境中, 不只是需要一个后端的 web 项目, 还需要比如 Nginx 作为反向代理. 数据库也需要单独部署在一个容器里面, 要是我们像之前学过的那样一个个部署, 那岂不是很麻烦.
所以, 我们需要一套东西来帮助我们实现这个功能, 那就是今天要学习的 Docker Compose 容器编排技术.
Docker Compose
Docker Compose 用于定义和运行多容器 Docker 应用程序的工具, 通过 YAML 文件来定义应用程序里面的服务. 而后, 通过一个命令, 即可启动这些容器所产生的服务.
基本步骤:
定义 Dockerfile 自定义镜像
编辑 docker-compose.YAML 进行容器服务的编排
docker-compose up 创建和启动容器服务
安装 Compose
GitHub 安装
从 GitHub 拉取最新版本的 Compose 本地编译安装. 推荐方式, 错误少, 可直接安装.
GitHub https://github.com/docker/compose/releases
当前 GitHub 最新版本为 1.25.4
- ## 下载最新版的 Docker Compose 二进制包
- curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- ## 赋予可执行权限
- chmod +x /usr/local/bin/docker-compose
- ## 检查版本信息
- docker-compose -v
pip 安装
这是种比较保守的安装方法, 相较于上面的安装方法, 建议还是采用上面的安装方法.
- ## 安装 epel 额外的源
- yum -y install epel-release
- ## 安装 python-pip
- yum -y install python-pip
- ## 更新 pip 到最新版本
- pip install -U pip
- ## install
- pip install docker-componse
- ## 检查安装情况
- docker-compose -v
测试使用
确保我们已经正常的安装了 Docker Compose, 我们将采用官网的 DEMO 进行演示, 在此, 你不需要安装 Python 以及 Redis
1. 创建目录
- mkdir -p composetest
- cd composetest
2. 创建一个 pyton Web
## 创建 App.py
vi App.py
- ## 复制以下内容到文件内保存退出
- import time
- import Redis
- from flask import Flask
- App = Flask(__name__)
- cache = Redis.Redis(host='redis', port=6379)
- def get_hit_count():
- retries = 5
- while True:
- try:
- return cache.incr('hits')
- except Redis.exceptions.ConnectionError as exc:
- if retries == 0:
- raise exc
- retries -= 1
- time.sleep(0.5)
- @App.route('/')
- def hello():
- count = get_hit_count()
- return 'Hello World! I have been seen {} times.\n'.format(count)
- ## 创建一个文件用于保存命令
- vi requirements.txt
- ## 保存以下内容
- flask
- Redis
自定义镜像
- ## 创建新的 Dockerfile
- vi Dockerfile
- ## 加入以下内容
- FROM python:3.7-alpine
- WORKDIR /code
ENV FLASK_APP App.py
- ENV FLASK_RUN_HOST 0.0.0.0
- RUN apk add --no-cache gcc musl-dev Linux-headers
- COPY requirements.txt requirements.txt
- RUN pip install -r requirements.txt
- COPY . .
- CMD ["flask", "run"]
这告诉 Docker:
从 Python 3.7 映像开始构建映像.
将工作目录设置为./code
设置命令使用的环境变量. flask
安装 gcc, 以便诸如 MarkupSafe 和 SQLAlchemy 之类的 Python 包可以编译加速.
复制并安装 Python 依赖项. requirements.txt
将项目中的当前目录复制到映像中的工作目录...
将容器的默认命令设置为. flask run
撰写容器编排
- ## 创建 docker-compose.YAML 文件
- vi docker-compose.YAML
- ## 加入以下内容
- version: '3'
- services:
- Web:
- build: .
- ports:
- - "5000:5000"
- Redis:
- image: "redis:alpine"
网络服务
该 Web 服务使用从 Dockerfile 当前目录中构建的映像. 然后, 它将容器和主机绑定到暴露的端口 5000. 此示例服务使用 Flask Web 服务器的默认端口 5000.
Redis 服务
该 Redis 服务使用 从 Docker Hub 注册表中提取的公共 Redis 映像.
运行服务
- docker-compose up
- Starting composetest_redis_1 ... done
- Recreating composetest_web_1 ... done
- Attaching to composetest_redis_1, composetest_web_1
- redis_1 | 1:C 09 Feb 2020 02:14:31.462 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
- redis_1 | 1:C 09 Feb 2020 02:14:31.462 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=1, just started
- redis_1 | 1:C 09 Feb 2020 02:14:31.462 # Warning: no config file specified, using the default config. In order to specify a config file use Redis-server /path/to/Redis.conf
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 * Running mode=standalone, port=6379.
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys.NET/core/somaxconn is set to the lower value of 128.
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 # Server initialized
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never> /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 * DB loaded from disk: 0.000 seconds
- redis_1 | 1:M 09 Feb 2020 02:14:31.463 * Ready to accept connections
- web_1 | * Serving Flask App "app.py"
- web_1 | * Environment: production
- web_1 | WARNING: This is a development server. Do not use it in a production deployment.
- web_1 | Use a production WSGI server instead.
- web_1 | * Debug mode: off
- web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
服务成功的运行在 5000 端口下, 访问尝试, 而后刷新, 观察计数器的变化!
常用命令
docker-compose up -d 后台运行服务
- [root@mrclinux composetest]# docker-compose up -d
- Starting composetest_web_1 ... done
- Starting composetest_redis_1 ... done
docker-compose ps 查看当前的运行容器
- [root@mrclinux composetest]# docker-compose ps
- Name Command State Ports
- -------------------------------------------------------------------------------------
- composetest_redis_1 docker-entrypoint.sh Redis ... Up 6379/tcp
- composetest_web_1 flask run Up 0.0.0.0:8080->5000/tcp
docker-compose images 查看已存在的镜像信息
- [root@mrclinux composetest]# docker-compose images
- Container Repository Tag Image Id Size
- ------------------------------------------------------------------------
- composetest_redis_1 docker.io/Redis alpine b68707e68547 29.78 MB
- composetest_web_1 composetest_web latest 0a812986cca6 221.8 MB
docker-compose stop 用来停止容器, 不删除它, 可以再次使用 start 启用
- [root@mrclinux composetest]# docker-compose stop
- Stopping composetest_web_1 ... done
- Stopping composetest_redis_1 ... done
docker-compose start 使用 start 启动存在的容器服务
- [root@mrclinux composetest]# docker-compose start
- Starting Web ... done
- Starting Redis ... done
docker-compose down 停止服务并且移除容器
- [root@mrclinux composetest]# docker-compose down
- Stopping composetest_web_1 ... done
- Stopping composetest_redis_1 ... done
- Removing composetest_web_1 ... done
- Removing composetest_redis_1 ... done
- Removing network composetest_default
docker-compose up 创建以及启动服务
参考
- https://docs.docker.com/compose/
- https://www.cnblogs.com/ityouknow/p/8648467.html
来源: https://www.cnblogs.com/ChromeT/p/12286484.html