1、下载
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2、授权
sudo chmod +x /usr/local/bin/docker-compose
多看官网……
官方文档:Get started with Docker Compose | Docker Documentation
为项目创建一个目录
mkdir composetest
cd composetest
1、应用 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、Dockerfifile 应用打包为镜像
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"]
3、Docker-compose yaml文件 (定义整个服务,需要的环境。 web、redis) 完整的上线服务!
version: "3.3"
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
4、创建在项目目录中调用的另一个文件 requirements.txt
flask
redis
5、ll 查看 composeset 目录下的文件(共四个)
[root@localhost composeset]# ll
总用量 16
-rw-r--r--. 1 root root 514 6月 22 09:30 app.py
-rw-r--r--. 1 root root 111 6月 22 09:32 docker-compose.yml
-rw-r--r--. 1 root root 252 6月 22 09:30 Dockerfile
-rw-r--r--. 1 root root 12 6月 22 09:30 requirements.txt
[root@localhost composeset]#
启动 compose 项目(docker-compose up)
# 第一次会卡在这里,执行 ctrl+c 退出
# 然后执行 docker-compose build
[root@localhost composeset]# docker-compose build
redis uses an image, skipping
Building web
Step 1/10 : FROM python:3.7-alpine
---> 04ffff590161
Step 2/10 : WORKDIR /code
---> Using cache
---> 4107e1d5ed13
Step 3/10 : ENV FLASK_APP=app.py
---> Using cache
---> d1bb629e7b3e
Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0
---> Using cache
---> 7e80aec14cb8
Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers
---> Using cache
---> ae486b762875
Step 6/10 : COPY requirements.txt requirements.txt
---> Using cache
---> 3fb49663d185
Step 7/10 : RUN pip install -r requirements.txt
---> Using cache
---> 7fe1f941980a
Step 8/10 : EXPOSE 5000
---> Using cache
---> f783e608dfbe
Step 9/10 : COPY . .
---> 3a9e40461254
Step 10/10 : CMD ["flask", "run"]
---> Running in 0170979e445a
Removing intermediate container 0170979e445a
---> fa9e4891a8a4
Successfully built fa9e4891a8a4
Successfully tagged composeset_web:latest
# 再次执行 docker-compose up
到这里则启动成功!
# 查看 docker images
# 查看 docker ps
[root@localhost composeset]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
composeset_web latest fa9e4891a8a4 20 minutes ago 182MB
045735153dcc 39 minutes ago 182MB
redis alpine f934e82c14d1 8 days ago 28.4MB
python 3.7-alpine 04ffff590161 2 weeks ago 45.5MB
[root@localhost composeset]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ee1892094c0 composeset_web "flask run" 20 minutes ago Up 20 minutes 0.0.0.0:8000->5000/tcp, :::8000->5000/tcp composeset_web_1
fe5a3e801682 redis:alpine "docker-entrypoint.s…" 39 minutes ago Up 20 minutes 6379/tcp composeset_redis_1
[root@localhost composeset]#
# 输入 ip地址:8000 则可以看到 hello world 页面
自动的默认规则?
docker images
[root@localhost composeset]# docker service ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
[root@localhost composeset]#
docker-compose
以前都是单个 docker run 启动容器
docker-compose。 通过 docker-compose 编写 yaml配置文件、可以通过 compose 一键启动所有服
务,停止。!
Docker小结:
1、Docker 镜像。 run => 容器
2、DockerFile 构建镜像(服务打包)
3、docker-compose 启动项目(编排、多个微服务/环境)
4、Docker 网络
(B站狂神说Docker 要PDF文件可私信我)