docker compose初体验(3)

docker compose初体验

链接:https://docs.docker.com/compose/gettingstarted/

python应用,计数器 、redis

1、应用app.py
2、Dockerfile应用打包为镜像
3、Docker-compose yaml文件(定义整个服务,需要的环境。web、redis)完整的上线服务
4、启动compose 项目(docker-compose up)
流程:
1、创建网络
2、执行Docker-compose yaml
3、启动服务。
Docker-compose yaml
Creating composetest_web_1 ...done
Creating composetest_redis_1 ... done

version: "3.3"
services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"
mkdir composetest
cd composetest
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)
vi requirements.txt
输入:
flask
redis
# 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"]
version: "3.8"
services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"

image.png

docker-compose up

image.png

如果等待时间比较长,可以对其中两个文件进行修改Dockerfile和app.py

FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["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)

if __name__ == "__main__":
  app.run(host="0.0.0.0",debug=True)

运行成功:
image.png
会发现多了三个镜像
image.png

# docker service ls
[root@kuangshen ~]# docker service ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarmjoin" to connect this node to swarm and try again.

默认的服务名文件名_服务名_num
多个服务器。集群。A B_num副本数量
服务redis服务=>4个副本。
集群状态。服务都不可能只有一个运行实例。弹性、10 HA高并发。
kubectl service负载均衡。

网络规则

[root@localhost composetest]# docker network  ls
NETWORK ID     NAME                  DRIVER    SCOPE
0093797706ae   composetest_default   bridge    local

网络细节,构建了一个网络,在同一个网络下通过局域网进行访问
image.png

停止:

docker-compose down
ctrl+c

docker-compose
以前都是单个docker run启动容器。
docker-compose。通过docker-compose编写yaml配置文件、可以通过compose一键启动所有服务,停止。

Docker小结:
1、Docker镜像。 run=>容器
2、DockerFile构建镜像(服务打包)
3、docker-compose启动项目(编排、多个微服务/环坏境)
4、Docker网络

posted @ 2022-11-22 14:59  yutoujun  阅读(27)  评论(0编辑  收藏  举报