Docker Compose安装和使用
程序员文章站
2022-05-14 20:40:46
...
一、安装
运行以下命令下载Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
对二进制文件应用可执行权限
[[email protected] composetest]# chmod +x /usr/local/bin/docker-compose
查看docker-compose版本
[[email protected] composetest]# docker-compose -v
docker-compose version 1.29.2, build 5becea4c
二、使用
1.为项目创建一个目录
[[email protected] ~]# mkdir /composetest
[[email protected] composetest]# cd /composetest
2.在项目目录下创建一个名为app.py的文件
[[email protected] composetest]# cat 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)
3.在项目目录下创建一个requirements.txt的文件
[[email protected] composetest]# cat requirements.txt
flask
redis
4.在项目目录下创建一个Dockerfile
[[email protected] composetest]# cat 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"]
5.在项目目录中创建一个docker-compose.yml文件
[[email protected] composetest]# cat docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
ports:
- "6379:6379"
6.使用compose启动应用程序
[[email protected] composetest]# docker-compose up -d
7.测试启动的应用程序
[[email protected] ~]# curl 192.168.10.230:5000
Hello World! I have been seen 5 times.
下一篇: python读取文件时的相对路径