1 介绍

记录使用docker 构建包含 supervior 的镜像,

supervisor: 是一个管理和监控进程的程序,可以方便的通过配置文件来管理我们的任务脚本

将supervisor构建到系统镜像中,启动时默认启动 supervisor进程,容器可以正常运行,然后通过 ,supervisor 去启动,停止,重启我们的 任务脚本,例如, flask, fastapi 等服务启动脚本

1.1 背景

构建一个 fastapi web 服务镜像,方便业务上线.基础需求:

  1. 容器中没有 fastapi web 服务也可以正常启动运行
  2. web 服务通过配置文件可插拔加载和分离

2 具体流程

2.1 准备supervisor默认启动的配置文件

使用默认的配置文件可以 默认开启 supervisor 的 http 服务器管理访问功能,即配置块 inet_http_server

对应的配置如下:

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700) [supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface # [supervisorctl]
# serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket ; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves. [include]
files = /etc/supervisor/conf.d/*.conf [inet_http_server] ; inet (TCP) server disabled by default
port=*:9001 ; (ip_address:port specifier, *:port for all iface)
username=admin ; (default is no username (open server))
password=admin.123 ; (default is no password (open server))

2.2 web服务的pip包安装配置文件

准备 requirements.txt, 使用时根据自己实际情况.

fastapi==0.115.0

2.3 Dockerfile构建配置

这里我使用 python:3.10-slim 作为基础镜像构建, supervisor的启动命令作为入口 CMD

FROM python:3.10-slim 

MAINTAINER faron
WORKDIR /usr/src/app RUN mkdir -p /var/log/supervisor \
&& apt-get update && apt-get install -y cron autoconf automake libtool vim procps supervisor \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* COPY requirements.txt ./
COPY supervisord.conf /etc/supervisor/supervisord.conf
RUN pip install -i https://mirrors.aliyun.com/pypi/simple --no-cache-dir -r requirements.txt \
&& rm -rf requirements.txt CMD ["/usr/bin/supervisord","-c","/etc/supervisor/supervisord.conf" ]

构建命令:

docker build -t web:1.0 .

2.4 启动测试

使用 docker-compose 进行测试

services:
web: # web服务
image: web:1.0
container_name: web_test
ports:
- 9001:9001
volumes:
- /etc/localtime:/etc/localtime
restart: always

启动命令:

docker-compose up -d
NAME       IMAGE     COMMAND                  SERVICE   CREATED         STATUS         PORTS
web_test web:1.0 "/usr/bin/supervisor…" web 8 seconds ago Up 7 seconds 0.0.0.0:9001->9001/tcp

查看效果: 访问 IP:9001 ,账户名密码: admin admin.123

账户名密码在 supervisord.conf 文件的 inet_http_server 下;

3 使用supervisor配置启动服务

这里以一个 实时查看文本文件的需求服务为示例,介绍如何通过 supervisor 管理服务;

3.1 创建 服务配置文件

实时查看某个文件的服务管理脚本: test.conf

[program:test]
command=tail -n 20 -f /var/log/supervisor/supervisord.log
autostart=true
startsecs=3
autorestart=true

调整docker-compose.yml文件,将启动配置文件映射进去

services:
web: # web服务
image: web:1.0
container_name: web_test
ports:
- 9001:9001
volumes:
- /etc/localtime:/etc/localtime
- ./test.conf:/etc/supervisor/conf.d/test.conf # 映射启动配置文件
restart: always

启动新的容器服务,注意历史的关闭

docker-compose down
docker-compose up -d

访问 IP:9001

4 参考文档

docker构建supervisor镜像的更多相关文章

  1. 使用docker构建supervisor全步骤

    1.使用docker build 命令基于Dockerfile文件进行构建supervisor镜像,命令:docker build -t supervisor镜像名 Dockerfile文件放置的位置 ...

  2. docker构建自定义镜像

    docker构建自定义镜像 要构建一个镜像,第一步准备所需要的文件,第二步编写Dockerfile文件,比如我现在构建一个java web镜像 第一步:准备java web工程的war包文件(这里假设 ...

  3. Docker构建YApi镜像, Docker安装YApi, Docker部署YApi

    概述 YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布.维护 API,YApi 还为用户提供了优秀的交互体验,开发 ...

  4. 用Docker构建Tomcat镜像

    构建tomcat镜像 创建工作目录 [root@elk-node2 tomcat]# mkdir tomcat [root@elk-node2 tomcat]# cd tomcat [root@elk ...

  5. 用Docker构建MySQL镜像

    构建MySQL镜像 本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过D ...

  6. 用Docker构建Nginx镜像

    1构建Nginx镜像 1建立工作目录 [root@localhost ]# mkdir 1nginx [root@localhost 1nginx]# cd 1nginx/ [root@localho ...

  7. Docker构建FastDFS镜像

    https://blog.csdn.net/qq_26440803/article/details/83066132 Dockerfile 所需依赖: fastdfs    libfastcommon ...

  8. Docker 构建私有镜像仓库

    在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便,另外有时候只是希望在内部用户之间进行分享,不希望暴露出去.这种情况下,就有必要搭建一个本地 ...

  9. docker构建tomcat镜像

    下载centos镜像 # docker pull daocloud.io/centos:7 [root@localhost ~]# docker pull daocloud.io/centos: : ...

  10. Docker构建ssh镜像

    FROM ubuntu MAINTAINER ggzone xxx@live.com ENV REFRESHED_AT 2015-10-21 RUN apt-get -qqy update & ...

随机推荐

  1. 使用Hexo主题搭建个人博客(markdown)

    依赖环境 安装node.js:node.js下载可以从其官方界面开始https://nodejs.org/zh-cn/ 安装git:git下载则可以从其官方界面开始https://git-scm.co ...

  2. 小tips:vue结合百度UEditor富文本编辑器实现vue-ueditor-wrap

    1.下载vue-ueditor-wrap cnpm i vue-ueditor-wrap -S 下载最新的 UEditor 资源文件放入你项目的静态资源目录中(比如 static 或者 public, ...

  3. SpringCloud入门(二)服务间调用和案例

    一.微服务拆分注意事项微服务拆分注意事项:1.单一职责:不同微服务,不要重复开发相同业务2.数据独立:不要访问其它微服务的数据库3.面向服务:将自己的业务暴露为接口,供其它微服务调用 1.微服务需要根 ...

  4. placement new --特殊的内存分配

    placement new 是 C++ 中的一种特殊的内存分配技术,用来在指定的内存地址上直接构造对象.与普通的 new 运算符不同,placement new 并不分配新的内存,而是在已经分配好的内 ...

  5. USB总线-Linux内核USB设备驱动之UAC2驱动分析(十)

    1.概述 UVC(USB Audio Class)定义了使用USB协议播放或采集音频数据的设备应当遵循的规范.目前,UAC协议有UAC1.0和UAC2.0. UAC2.0协议相比UAC1.0协议,提供 ...

  6. Linux-USB驱动笔记--设备控制器(UDC)驱动

    1.前言 在Linux-USB驱动笔记(四)–USB整体框架中有説到设备侧的设备控制器(UDC),下面我们来具体看一下. 2.设备控制器(UDC) USB设备控制器(UDC)驱动指的是作为其他USB主 ...

  7. style 标签写在body 前后的区别?

    知识储备:了解浏览器渲染页面的流程 a)首先 , 解析(parse)html 标签 , 获取DOM 树 b)解析html 的同时 , 解析css  , 获得样式规则 (style rules) CSS ...

  8. 后台管理系统的setting.js

    // 修改了此处要重新启动 module.exports = { // 网页的标题 title: "人力资源系统", /** * @type {boolean} true | fa ...

  9. Android复习(二)应用资源 --> 颜色状态列表

    转自: https://developer.android.google.cn/guide/topics/resources/color-list-resource 颜色状态列表资源 ColorSta ...

  10. 在 openEuler 22.03 上安装 KubeSphere 实战教程

    作者:老 Z,中电信数智科技有限公司山东分公司运维架构师,云原生爱好者,目前专注于云原生运维,云原生领域技术栈涉及 Kubernetes.KubeSphere.DevOps.OpenStack.Ans ...