🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 docker 常见操作
  • 1.1 容器
  • 1.2 镜像
  • 2 Butterfly 镜像
  • 2.1 build butteffly 镜像
  • 2.2 镜像导出和导入
  • 2.3 使用 butteffly 镜像
  • 3 容器细节
  • 3.1 网桥模式
  • 3.2 映射容器端口到主机端口,并使用随机主机端口
  • 4 docker API 例子
  • 4.1 创建容器
  1. 五 部署运维

单机容器化部署

如下使用时,可根据自己需求,修改版本 tag

1 docker 常见操作

1.1 容器

docker run -d -it centos:centos7 /bin/bash   #启动
docker exec -it xxxx /bin/bash               #连接 

1.2 镜像

比如 nginx

// image 列表
$ docker images

// image 拉取
$ docker pull nginx

// image 详情
$ docker inspect <image_id>

2 Butterfly 镜像

2.1 build butteffly 镜像

$ git clone https://github.com/meetbill/butterfly.git
$ cd butterfly

// x86 机器上操作
$ docker build -t butterfly:1.1.21 .

// MAC M1 版本操作
$ docker buildx build --platform=linux/amd64 -t butterfly:1.1.21 .

2.2 镜像导出和导入

导出时带上 tag:1.1.21 ,在导入后,也会带有 tag


// 本机导出 image
$ docker save -o butterfly.tar butterfly:1.1.21

// 目标机器加载 image
$ docker load < butterfly.tar

// 测试(bridge)
$ docker run -d -p 8585:8585 --name butterfly_app butterfly:1.1.21
$ curl "http://127.0.0.1:8585/demo_api/ping"

// volume 映射
-v host_path:container_path

2.3 使用 butteffly 镜像

如下是接口认证组件 app 的 demo 的 Dockerfile

FROM butterfly:1.1.21

MAINTAINER meetbill <meetbill@163.com>
ENV REFRESHED_AT 2019-12-30

COPY ./butterfly_auth/conf /opt/butterfly/conf
COPY ./butterfly_auth/handlers/auth /opt/butterfly/handlers/auth

EXPOSE 8585

ENTRYPOINT ["sh","/opt/butterfly/run.sh","docker_start"]

3 容器细节

3.1 网桥模式

$ docker run -d -p 8888:8585 --name butterfly_app butterfly:1.1.21

docker inspect <container_id>

"HostConfig": {
    "NetworkMode": "default",                               // 指定的 network 配置
    "PortBindings": {
        "8585/tcp": [
            {
                "HostIp": "",
                "HostPort": "8888"                          // 映射的端口,如果是 host 模式,则此端口为 8585
            }
        ]
    },
}
"Config": {
    "Hostname": "714d1d2cb4c5",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
        "8585/tcp": {}
    },
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
        "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        "LANG=C.UTF-8",
        "PYTHONIOENCODING=UTF-8",
        "GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF",
        "PYTHON_VERSION=2.7.18",
        "PYTHON_PIP_VERSION=20.0.2",
        "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/d59197a3c169cef378a22428a3fa99d33e080a5d/get-pip.py",
        "PYTHON_GET_PIP_SHA256=421ac1d44c0cf9730a088e337867d974b91bdce4ea2636099275071878cc189e"
    ],
    "Cmd": null,
    "Image": "butterfly:1.1.21",
    "Volumes": null,
    "WorkingDir": "",
    "Entrypoint": [
        "sh",
        "/opt/butterfly/run.sh",
        "docker_start"
    ],
    "OnBuild": null,
    "Labels": {}
},
"NetworkSettings": {
    "Bridge": "",
    "SandboxID": "16a5df7d1ba55a2f3fa2e9a8cfee8fde10d9735407351c5cb6a7555f73e3f314",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {
        "8585/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "8888"
            },
            {
                "HostIp": "::",
                "HostPort": "8888"
            }
        ]
    },
    "SandboxKey": "/var/run/docker/netns/16a5df7d1ba5",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "f2b1f9a9f5f27258e9e0af8d4c9f721aab4a16df89c0f8f4e3e04fd56ed4a892",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:02",
    "Networks": {
        "bridge": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": null,
            "NetworkID": "3989e21b180fbb25a769528a57da22c6a1a1094274855ad04a81a3e4a0f80ffb",
            "EndpointID": "f2b1f9a9f5f27258e9e0af8d4c9f721aab4a16df89c0f8f4e3e04fd56ed4a892",
            "Gateway": "172.17.0.1",
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:11:00:02",
            "DriverOpts": null
        }
    }
}

3.2 映射容器端口到主机端口,并使用随机主机端口

$ docker run -d -p :8585 --name butterfly_app1 butterfly:1.1.21

$ docker inspect <container_id>
{
    "HostConfig": {
        "PortBindings": {
            "8585/tcp": [
                {
                    "HostIp": "",
                    "HostPort": ""
                }
            ]
        }
    }
}

4 docker API 例子

4.1 创建容器

POST /containers/create HTTP/1.1  
Host: localhost:2375  
Content-Type: application/json  
  
{  
  "Image": "my-image",  
  "ExposedPorts": {  
    "80/tcp": {}  
  },  
  "HostConfig": {  
    "PortBindings": {  
      "80/tcp": [  
        {  
          "HostPort": "8080"  
        }  
      ]  
    }  
  }  
}

例子中使用镜像(my-image),并且暴露了容器的 80 端口(ExposedPorts)。然后,在HostConfig字段中,我们指定了将容器的 80 端口映射到主机的 8080 端口(PortBindings)。

Previous五 部署运维Next监控

Last updated 1 month ago