文章目录
一、基础镜像生成
1.1、编写supervisor
服务的DockerFile
# supervisord_python3:v1
# 使用centos7为基础镜像
FROM centos:7
# 后续需要的基础 更新epel第三方软件库 安装gcc
RUN yum install --nogpgcheck -y epel-release gcc which net-tools && yum clean all
# 安装supervisor
RUN yum install --nogpgcheck -y supervisor && yum clean all
RUN rm -rf /etc/supervisord.d/*.ini && mkdir -p /logs /etc/supervisord.d
# 重新编写supervisor.conf 拷贝到docker中
COPY supervisord.conf /etc/supervisord.conf
# 暴露supervisor端口
EXPOSE 9001
# 安装python3
RUN yum install --nogpgcheck -y python3 python3-devel && yum clean all
# 永久更改pip源
RUN pip3 install --no-cache-dir --upgrade pip -i https://mirrors.aliyun.com/pypi/simple
RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple
# 设置时区、字符集
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "export LANG=en_US.UTF-8" > /etc/profile.d/app_lang.sh
# 需要git刷代码,以及启动时的验证
#RUN yum install --nogpgcheck -y openssh-clients git && yum clean all
# Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app
# 基类检查文件拷贝
COPY start.sh /start.sh
# 增加执行权限
RUN chmod +x /start.sh
# Add demo app
COPY app /app
# 将工作台设置为app
WORKDIR /app
# Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Supervisor, which in turn will start Nginx and uWSGI
# 运行start.sh
CMD ["/start.sh"]
1.2、supervisor
相关镜像文件
supervisord.conf
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; (the path to the socket file)
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
; username=admin ; (default is no username (open server))
; password=admin ; (default is no password (open server))
[supervisord]
logfile=/logs/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files=/etc/supervisord.d/*.ini
prestart.sh
#! /usr/bin/env sh
echo "Running inside /app/prestart.sh, you could add migrations to this file, e.g.:"
echo "
#! /usr/bin/env sh
# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head
"
start.sh
#! /usr/bin/env sh
# 如果任何语句的执行结果不是true则应该退出
set -e
# If there's a prestart.sh script in the /app directory, run it before starting
PRE_START_PATH=/app/prestart.sh
echo "Checking for script in $PRE_START_PATH"
if [ -f $PRE_START_PATH ] ; then
echo "Running script $PRE_START_PATH"
# shellcheck disable=SC2039
# shellcheck disable=SC1090
source $PRE_START_PATH
else
echo "There is no script $PRE_START_PATH"
fi
# Start Supervisor, with Nginx and uWSGI
exec /usr/bin/supervisord
1.3、运行DockerFile文件
创建app空目录
### 将编写好的Dockerfile文件放到 /mydocker中
# 将supervisor.conf prestart.sh start.sh放入 /mydocker/super_py中
cd /
mkdir mydocker && cd /mydocker
mkdir super_py
## 运行生成镜像(准备好文件后执行)
docker build . -t supervisor_python:v1
二、代码部分上传和配置
2.1、docker-compose.yml
version: "3"
services:
xh_apis:
volumes:
# 配合环境变量里tmp目录的配置
- "/tmp/static:/static"
- "/configs:/app/configs"
- "/configs/prestart.sh:/app/prestart.sh"
- "/configs/apis_env:/app/env"
# xh_apis_001代码
- "/configs/run_uwsgi.sh:/app/xh_apis_001/run_uwsgi.sh"
- "/codes/xh_apis_001:/app/xh_apis_001/website"
image: "supervisor_python:v1"
logging:
driver: "json-file"
options:
max-size: "100m"
ports:
- "13105:9001"
- "13005:80"
environment:
- SERVICE_9001_NAME=xh_apis
- SERVICE_80_IGNORE=true
2.2、运行部署
cd //mydocker_blog/super_py/compose
docker-compose up -d
数据卷文件不方便公开,可加V: GuiltyHacker获取