Docker学习笔记12-----Docker Compose使用

本文详细介绍如何使用Docker Compose快速部署一个包含Flask应用和Redis服务的简单项目。从环境搭建到服务运行,逐步解析Docker Compose的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Docker Compose

https://docs.docker.com/compose/

1、安装
Linux环境下离线安装,在线安装速度太慢
1.1、下载
https://github.com/docker/compose/releases
1.2、配置
cp docker-compose-Linux-x86_64 /usr/local/sbin/docker-compose
1.3、授权
sudo chmod +x /usr/local/sbin/docker-compose
1.4、验证
[root@localhost sbin]# docker-compose version
docker-compose version 1.28.5, build c4eb3a1f
docker-py version: 4.4.4
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019
2、前期准备
2.1、创建composetest文件夹
[root@localhost ~]# mkdir composetest
[root@localhost ~]# cd composetest
2.2、创建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)
2.3、创建requirements.txt文件
flask
redis
3、创建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
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
4、在Componse文件docker-compose.yml中定义容器服务
version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"
5、通过Compose构建并运行应用
[root@localhost composetest]# docker-compose up -d
Creating network "composetest_default" with the default driver
Building web
Sending build context to Docker daemon  5.632kB

Step 1/10 : FROM python:3.7-alpine
 ---> 72e4ef8abf8e
Step 2/10 : WORKDIR /code
 ---> Using cache
 ---> f36d86481b1a
Step 3/10 : ENV FLASK_APP=app.py
 ---> Using cache
 ---> b6efb976b718
Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0
 ---> Using cache
 ---> b40d175dd5c5
Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers
 ---> Running in 8d619d3ed7cc
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libgcc (9.3.0-r2)
(2/13) Installing libstdc++ (9.3.0-r2)
(3/13) Installing binutils (2.34-r1)
(4/13) Installing gmp (6.2.0-r0)
(5/13) Installing isl (0.18-r0)
(6/13) Installing libgomp (9.3.0-r2)
(7/13) Installing libatomic (9.3.0-r2)
(8/13) Installing libgphobos (9.3.0-r2)
(9/13) Installing mpfr4 (4.0.2-r4)
(10/13) Installing mpc1 (1.1.0-r1)
(11/13) Installing gcc (9.3.0-r2)
(12/13) Installing linux-headers (5.4.5-r1)
(13/13) Installing musl-dev (1.1.24-r10)
Executing busybox-1.31.1-r19.trigger
OK: 153 MiB in 48 packages
Removing intermediate container 8d619d3ed7cc
 ---> a1ae320f6909
Step 6/10 : COPY requirements.txt requirements.txt
 ---> e55bd896e636
Step 7/10 : RUN pip install -r requirements.txt
 ---> Running in df3ef20e354b
Collecting flask
  Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting click>=5.1
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1.tar.gz (19 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting redis
  Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Building wheels for collected packages: MarkupSafe
  Building wheel for MarkupSafe (setup.py): started
  Building wheel for MarkupSafe (setup.py): finished with status 'done'
  Created wheel for MarkupSafe: filename=MarkupSafe-1.1.1-cp37-cp37m-linux_x86_64.whl size=16911 sha256=1d8e070872a5ec6c7c6b57c165ed2264759f4d55e1f592c78d3afb8334a5a462
  Stored in directory: /root/.cache/pip/wheels/b9/d9/ae/63bf9056b0a22b13ade9f6b9e08187c1bb71c47ef21a8c9924
Successfully built MarkupSafe
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, redis, flask
Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0 redis-3.5.3
WARNING: You are using pip version 20.3.3; however, version 21.0.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container df3ef20e354b
 ---> ba997612bc76
Step 8/10 : EXPOSE 5000
 ---> Running in 2f764b640db1
Removing intermediate container 2f764b640db1
 ---> 7bc3091ff536
Step 9/10 : COPY . .
 ---> ff0eaea963e7
Step 10/10 : CMD ["flask", "run"]
 ---> Running in 772cbb37bcb2
Removing intermediate container 772cbb37bcb2
 ---> d0cca2d14f87
Successfully built d0cca2d14f87
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
801bfaa63ef2: Already exists
9a8d0188e481: Pull complete
8a3f5c4e0176: Pull complete
3f7cb00af226: Pull complete
e421f2f8acb5: Pull complete
f41cc3c7c3e4: Pull complete
Digest: sha256:2cd821f730b90a197816252972c2472e3d1fad3c42f052580bc958d3ad641f96
Status: Downloaded newer image for redis:alpine
Creating composetest_web_1   ... done
Creating composetest_redis_1 ... done
6、查看启动成功的service
[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
7、验证程序

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BD4O6Zl0-1615354596117)(1E2080880D80403A9E9C1CD7B94991CF)]

8、常用命令
# 查看版本
docker-compose version

# 启动
docker-compose up -d

# 指定yml启动
docker-compose up -f docker-compose.yml

# 查看启动成功的service
docker-compose ps

# 查看images
docker-compose images

# 停止/启动service
docker-compose stop/start

# 删除service[同时会删除掉network和volume]
docker-compose down

# 进入到某个service
docker-compose exec redis sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itmrl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值