Docker-compose

1.安装 Docker-compose

docker-compose 版本可以在官网中找到对应的版本下载

# 官方链接(不推荐,太慢了)
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 下面的官方链接会快一点
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

# 修改文件权限
chmod +x /usr/local/bin/docker-compose

# 查看 docker-compose 版本
docker-compose --version

在这里插入图片描述

2.使用体验

# 创建一个文件
mkdir composetest
cd composetest
# 创建一个 python 的测试文件 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)
# 创建一个引用的的文件 requirements.txt
flask
redis
# 创建一个 Dockerfile 文件
# syntax=docker/dockerfile:1
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
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
# 创建一个 docker-compose.yml 文件
version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"     # 这里注意,官网文档是 8000 端口,但是不对,因为前面 DockerFile 文件中暴露的是 5000
  redis:
    image: "redis:alpine"
# 在当前目录下执行 docker-compose up 命令
[root@dongxx composetest]# docker-compose up
[root@VM-16-14-centos composetest]# docker-compose up
Building web
Sending build context to Docker daemon  5.632kB
Step 1/10 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
40e059520d19: Pull complete 
4f950178bcec: Pull complete 
bb5415fc276b: Pull complete 
56303c2b76c4: Pull complete 
77c03d0e8ac9: Pull complete 
Digest: sha256:1a1727003ad874410fc94d7c72b16c9ac082f4e0fcfe1da13d314a06bf33422b
Status: Downloaded newer image for python:3.7-alpine
 ---> 710594a19ecc
Step 2/10 : WORKDIR /code
 ---> Running in cbd94042b13b
Removing intermediate container cbd94042b13b
 ---> 73125f188e41
Step 3/10 : ENV FLASK_APP=app.py
 ---> Running in c81fc773ff4a
Removing intermediate container c81fc773ff4a
 ---> 36951a02e3f4
Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0
 ---> Running in eecfadc85cbe
Removing intermediate container eecfadc85cbe
 ---> 8f677f3e75c1
Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers
 ---> Running in b72b71d85b3b
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libgcc (10.3.1_git20211027-r0)
(2/13) Installing libstdc++ (10.3.1_git20211027-r0)
(3/13) Installing binutils (2.37-r3)
(4/13) Installing libgomp (10.3.1_git20211027-r0)
(5/13) Installing libatomic (10.3.1_git20211027-r0)
(6/13) Installing libgphobos (10.3.1_git20211027-r0)
(7/13) Installing gmp (6.2.1-r1)
(8/13) Installing isl22 (0.22-r0)
(9/13) Installing mpfr4 (4.1.0-r0)
(10/13) Installing mpc1 (1.2.1-r0)
(11/13) Installing gcc (10.3.1_git20211027-r0)
(12/13) Installing linux-headers (5.10.41-r0)
(13/13) Installing musl-dev (1.2.2-r7)
Executing busybox-1.34.1-r4.trigger
OK: 143 MiB in 49 packages
Removing intermediate container b72b71d85b3b
 ---> 67f5a3856f69
Step 6/10 : COPY requirements.txt requirements.txt
 ---> 3756d392333e
Step 7/10 : RUN pip install -r requirements.txt
 ---> Running in 2fe0f88025a5
Collecting flask
  Downloading Flask-2.1.1-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.2/95.2 KB 119.8 kB/s eta 0:00:00
Collecting redis
  Downloading redis-4.2.1-py3-none-any.whl (225 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 225.6/225.6 KB 62.4 kB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.1.1-py3-none-any.whl (224 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 224.7/224.7 KB 41.0 kB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.1.1-py3-none-any.whl (132 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 132.6/132.6 KB 50.1 kB/s eta 0:00:00
Collecting click>=8.0
  Downloading click-8.1.2-py3-none-any.whl (96 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.6/96.6 KB 84.1 kB/s eta 0:00:00
Collecting importlib-metadata>=3.6.0
  Downloading importlib_metadata-4.11.3-py3-none-any.whl (18 kB)
Collecting typing-extensions
  Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting deprecated>=1.2.3
  Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting packaging>=20.4
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 KB 66.5 kB/s eta 0:00:00
Collecting async-timeout>=4.0.2
  Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)
Collecting wrapt<2,>=1.10
  Downloading wrapt-1.14.0-cp37-cp37m-musllinux_1_1_x86_64.whl (80 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 80.6/80.6 KB 36.0 kB/s eta 0:00:00
Collecting zipp>=0.5
  Downloading zipp-3.7.0-py3-none-any.whl (5.3 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp37-cp37m-musllinux_1_1_x86_64.whl (30 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
  Downloading pyparsing-3.0.7-py3-none-any.whl (98 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.0/98.0 KB 40.5 kB/s eta 0:00:00
Installing collected packages: zipp, wrapt, Werkzeug, typing-extensions, pyparsing, MarkupSafe, itsdangerous, packaging, Jinja2, importlib-metadata, deprecated, async-timeout, redis, click, flask
Successfully installed Jinja2-3.1.1 MarkupSafe-2.1.1 Werkzeug-2.1.1 async-timeout-4.0.2 click-8.1.2 deprecated-1.2.13 flask-2.1.1 importlib-metadata-4.11.3 itsdangerous-2.1.2 packaging-21.3 pyparsing-3.0.7 redis-4.2.1 typing-extensions-4.1.1 wrapt-1.14.0 zipp-3.7.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Removing intermediate container 2fe0f88025a5
 ---> bcc8008a625c
Step 8/10 : EXPOSE 5000
 ---> Running in 7679a46fd950
Removing intermediate container 7679a46fd950
 ---> 3cd29741fda4
Step 9/10 : COPY . .
 ---> 480d4978f3bd
Step 10/10 : CMD ["flask", "run"]
 ---> Running in bfc153ee00ea
Removing intermediate container bfc153ee00ea
 ---> 201511182744
Successfully built 201511182744
Successfully tagged composetest_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling redis (redis:alpine)...
alpine: Pulling from library/redis
40e059520d19: Already exists
9801e2773878: Pull complete
8a19c93db723: Pull complete
ffaef02f5447: Pull complete
1b609b4c7a67: Pull complete
ee96e1297c38: Pull complete
Digest: sha256:ca7b891a380358a5f197a84884d42c3f8b441bc77761178a9743c471a0ef7367
Status: Downloaded newer image for redis:alpine
Creating composetest_web_1   ... done
Creating composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1  | 1:C 03 Apr 2022 07:52:25.491 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 03 Apr 2022 07:52:25.491 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 03 Apr 2022 07:52:25.491 # 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 03 Apr 2022 07:52:25.492 * monotonic clock: POSIX clock_gettime
redis_1  | 1:M 03 Apr 2022 07:52:25.492 * Running mode=standalone, port=6379.
redis_1  | 1:M 03 Apr 2022 07:52:25.493 # 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 03 Apr 2022 07:52:25.493 # Server initialized
redis_1  | 1:M 03 Apr 2022 07:52:25.493 # 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 03 Apr 2022 07:52:25.493 * Ready to accept connections
web_1    |  * Serving Flask app 'app.py' (lazy loading)
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 all addresses (0.0.0.0)
web_1    |    WARNING: This is a development server. Do not use it in a production deployment.
web_1    |  * Running on http://127.0.0.1:5000
web_1    |  * Running on http://172.18.0.3:5000 (Press CTRL+C to quit)


在这里插入图片描述

3.Compose 编写规则

在这里插入图片描述

4.案例展示

 
 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值