DockerCompose安装卸载以及执行官网例子

x33g5p2x  于2021-10-21 转载在 Docker  
字(4.2k)|赞(0)|评价(0)|浏览(660)

1.背景

我们之前自己制作Docker镜像,都是自己通过命令build run 制作,而且是制作单个镜像。
1.
那么,如果我想制作多个镜像呢?刚刚提到的方式是不是效率太低了,所以这就需要用到DockerCompose解决
1.
DockerCompose 的作用就是批量容器编排
1.
DockerCompose 是Docker官方的开源项目,需要安装

Compose 概念

  • 服务service ,容器。应用。(mysql,redis ,es)
  • 项目project。一组关联的容器

2.安装卸载Compose

其实按照官网一步一步来即可。
官网地址:
https://docs.docker.com/compose/

安装Compose

  1. 下载包,不过官网的这个慢
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

可以用daocloud下载

sudo 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

2.对/usr/local/bin/docker-compose 加执行权限

chmod +x /usr/local/bin/docker-compose

3.测试安装成功

[root@l4 ~]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

卸载Compose

  1. 删除Compose文件
rm /usr/local/bin/docker-compose
  1. 卸载使用pip
pip uninstall docker-compose

官网例子

  1. 创建一个工程文件
mkdir 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)
  1. 创建requirements.txt文件
flask
redis
  1. 创建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"]
  1. 创建docker-compose.yml文件
version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"
  1. 开始运行
docker-compose up

运行结果失败

WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main: temporary error (try again later)
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/community: temporary error (try again later)
ERROR: unable to select packages:

解决:

[root@l4 composetest]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@l4 composetest]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
[root@l4 composetest]# sysctl -p /etc/sysctl.conf
[root@l4 composetest]# grubby --update-kernel=ALL --args="systemd.unified_cgroup_hierarchy=0"
  1. 查看启动容器
[root@l4 composetest]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                       NAMES
941cd4cdbbf6   composetest_web   "flask run"              42 seconds ago   Up 41 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   composetest_web_1
4a384903d90e   redis:alpine      "docker-entrypoint.s…"   42 seconds ago   Up 41 seconds   6379/tcp                                    composetest_redis_1
[root@l4 composetest]#
  1. 查看镜像
[root@l4 composetest]# docker images
REPOSITORY        TAG          IMAGE ID       CREATED         SIZE
composetest_web   latest       f74fb203b756   9 minutes ago   184MB
tomcat            latest       4ce9babdd885   7 days ago      680MB
redis             alpine       e24d2b9deaec   2 weeks ago     32.3MB
python            3.7-alpine   206aee52d71d   2 weeks ago     41.9MB
[root@l4 composetest]#
  1. 测试成功

  1. 停止服务
ctrl +c 也可
[root@l4 composetest]# docker-compose down
Stopping composetest_web_1   ... done
Stopping composetest_redis_1 ... done
Removing composetest_web_1   ... done
Removing composetest_redis_1 ... done
Removing network composetest_default
[root@l4 composetest]#
  1. 查看网络
[root@l4 composetest]# docker network ls
NETWORK ID     NAME                  DRIVER    SCOPE
a3a9b221801d   bridge                bridge    local
def4faae18b6   composetest_default   bridge    local
0c1e234da975   host                  host      local
91e8927f2bd5   none                  null      local
  1. 检查发现redis和web服务在同一网络下,可以通过域名访问
[root@l4 composetest]# docker inspect composetest_default

        "Containers": {
            "3396710c15fe9fb7d81526ae6122d7b4af4cf17fc0f3efb6f8f3c9bf648abb84": {
                "Name": "composetest_web_1",
                "EndpointID": "daadf0308cec9764b855eef2b38ea04af1bdbac88833cac5b6e03a5a810ced4c",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "eda1e4016bbd033b28d897ef7938a8ee9bc56a479cb149d7cc3d0f21e4fdbd3e": {
                "Name": "composetest_redis_1",
                "EndpointID": "6aba87cbdfc1e392bc9610b3d81a0df0a9ca3f285232dca0d98cac8636cd1775",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },

步骤流程分析

composetest_redis_1;composetest_web_1
默认的服务名 文件名_服务名_num num是副本数量

Docker网络

相关文章