docker-compos

1.简介-安装

参考:https://docs.docker.com/compose/gettingstarted/
1. compose是docker应用程序工具,通过yml文件配置管理多个容器服务;
2. 步骤
2.1使用 Dockerfile 定义应用程序的环境。
2.2使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。
2.3执行 docker-compose up 命令来启动并运行整个应用程序。

3. 安装
curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

[root@localhost ~]# docker-compose --version
docker-compose version 1.24.1, build 4667896b

2. 测试项目

创建项目目录
mkdir composetest
1.添加文件
[root@localhost composetest]# ls
app.py  docker-compose.yml  Dockerfile  requirements.txt

# app.py  flask 项目
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)
    
# docker-compose.yml 定义了web 和 redis服务
version: "3.9"
services:
  web:
    build: .   # 构建当前目录下的Dockerfile文件
    ports:
      - "5000:5000"   # 映射端口
  redis:
    image: "redis:alpine"  # 使用redis公共镜像
    
# requirements.txt flask 项目依赖包
flask
redis

# Dockerfile docker镜像构建文件
FROM python:3.7-alpine  # 基础镜像
WORKDIR /code           # 工作目录
ENV FLASK_APP=app.py    # flask相关的环境变量
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers   # 安装系统依赖包
COPY requirements.txt requirements.txt  # 将当前宿主机目录中的文件拷贝至容器中的工作目录(/code)中
RUN pip install -r requirements.txt  # 安装python包
EXPOSE 5000  # 向图像添加元数据以描述容器正在侦听端口5000
COPY . .   # 当前项目中的内容复制到容器工作目录
CMD ["flask", "run"]  # 容器执行的默认命令

2.测试项目
[root@localhost composetest]# cd /qqc_data/composetest/
# 启动:
docker-compose up

# 查看启动的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED        STATUS          PORTS                    NAMES
98471f85bb25   redis:alpine      "docker-entrypoint.s…"   2 hours ago    Up 13 seconds   6379/tcp                 composetest_redis_1
4ee60f501995   composetest_web   "flask run"              2 hours ago    Up 12 seconds   0.0.0.0:5000->5000/tcp   composetest_web_1

# 访问
[root@localhost ~]# curl 127.0.0.1:5000
Hello World! I have been seen 8 times

3. 文件共享(无需重复docker-compose build web操作)

# docker-compose.yml
version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:    # 共享文件配置
      - .:/code
    environment: # 环境变量配置
      FLASK_ENV: development 
  redis:
    image: "redis:alpine"
    
在宿主机上更新项目:
1.新增文件README.md
[root@localhost composetest]# ls
app.py  docker-compose.yml  Dockerfile  __pycache__  README.md  requirements.txt
2.更改app.py
return 'lets go ,I have been seen {} times.\n'.format(count)

# 项目在前台运行时也检测到变更
web_1    |  * Serving Flask app "app.py" (lazy loading)
web_1    |  * Environment: development
web_1    |  * Debug mode: on
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 595-121-975
web_1    | 172.18.0.1 - - [14/Jan/2021 05:49:36] "GET / HTTP/1.1" 200 -
web_1    |  * Detected change in '/code/app.py', reloading
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 595-121-975

# 查看
[root@localhost ~]# curl 127.0.0.1:5000
lets go ,I have been seen 11 times.

# 进入composetest_web_1容器查看
[root@localhost ~]# docker exec -it composetest_web_1 /bin/sh
/code # ls
Dockerfile          README.md           __pycache__         app.py              docker-compose.yml  requirements.txt

4. compose 相关命令

1. 后台启动
[root@localhost composetest]# docker-compose up -d
Starting composetest_redis_1 ... done
Starting composetest_web_1   ... done

2. 查看进程
[root@localhost 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:5000->5000/tcp

3.关闭
[root@localhost composetest]# docker-compose stop
Stopping composetest_web_1   ... done
Stopping composetest_redis_1 ... done

4.重启
[root@localhost composetest]# docker-compose restart

5.run指定服务执行一次性命令
[root@localhost composetest]# docker-compose run web ls
Dockerfile          __pycache__         docker-compose.yml
README.md           app.py              requirements.txt
[root@localhost composetest]# docker-compose run redis env
HOSTNAME=b7c6971dceff
SHLVL=1
REDIS_DOWNLOAD_SHA=79bbb894f9dceb33ca699ee3ca4a4e1228be7fb5547aeb2f99d921e86c1285bd
HOME=/root
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.10.tar.gz
REDIS_VERSION=6.0.10
PWD=/data

6. 进入容器
[root@localhost composetest]# docker-compose exec web /bin/sh
/code # 

posted @ 2021-01-25 11:30  朝朝哥  阅读(203)  评论(0编辑  收藏  举报