with docker-compose
Dockerfile:
FROM ubuntu:18.04
MAINTAINER chudongyu <chudongyu#126.com>
ENV TZ=Asia/Shanghai
# host也是Ubuntu1804,可以直接配置镜像源为host的源
COPY sources.list /etc/apt/sources.list
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone &&
apt-get update -y &&
apt-get install tzdata -y &&
dpkg-reconfigure --frontend noninteractive tzdata
RUN apt upgrade -y &&
apt-get install -y vim &&
apt-get install -y software-properties-common
#add-apt-repository ppa:jonathonf/python-3.6
RUN apt-get update &&
apt upgrade -y &&
apt install -y python3.6 python3-pip
RUN apt-get install -y python3.6-dev python3.6-venv
# 并更新pip
RUN python3.6 -m pip install pip --upgrade -i https://pypi.douban.com/simple &&
python3.6 -m pip install wheel -i https://pypi.douban.com/simple
# 设置pip源
RUN pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple/ &&
pip3 config set global.trusted-host mirrors.aliyun.com
RUN apt-get update -y &&
apt-get install -y locales &&
locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN python3.6 -m pip install --no-cache-dir numpy pycrypto gevent
RUN pip install --no-cache-dir webrtcvad python-levenshtein pymongo pypinyin zhon
RUN pip install --no-cache-dir redis 'kombu==4.6.3' 'celery[redis]==4.3.0'
'tornado==5.1.1' flower celery-flower
RUN pip install --no-cache-dir requests
RUN pip install --no-cache-dir 'coverage==5.0.4' 'eventlet==0.25.1' 'Flask-APScheduler==1.11.0' &&
pip install --no-cache-dir flask Flask-Logger Flask-login flask-mongoengine &&
pip install --no-cache-dir Flask-Script Flask-Session gevent importlib-metadata python-Levenshtein
RUN pip install --no-cache-dir uwsgi uwsgitop
RUN pip install --no-cache-dir python-jenkins
RUN apt install -y supervisor
RUN pip install --no-cache-dir jieba nose pypinyin rednose 'smart-open==1.10.0' synonyms webrtcvad zhon
EXPOSE 5001
EXPOSE 5555
WORKDIR /expression-flask
#ENTRYPOINT ["celery", "worker", "-A", "celery_tasks.app"]
ENTRYPOINT ["uwsgi", "--ini", "uwsgi.ini"]
docker-compose:
version: '2.1'
services:
web:
image: exp-flask
build: ./build-scripts
container_name: exp-web
ports:
- 5001:5001
volumes:
- .:/expression-flask
extra_hosts:
- "redis-server.expression.hosts:192.168.0.4"
- "mongo-server.expression.hosts:192.168.0.4"
- "flask-server.expression.hosts:192.168.0.4"
sysctls:
- net.ipv4.tcp_keepalive_time=1800
- net.ipv4.tcp_keepalive_probes=3
- net.ipv4.tcp_keepalive_intvl=15
- net.ipv4.tcp_max_tw_buckets=25000
- net.ipv4.tcp_tw_reuse=1
- net.ipv4.tcp_fin_timeout=10
- net.ipv4.tcp_syncookies=0
- net.core.somaxconn=65535
flower:
image: exp-flask
container_name: exp-flower
ports:
- 50082:5555
volumes:
- .:/expression-flask
extra_hosts:
- "redis-server.expression.hosts:192.168.0.4"
- "mongo-server.expression.hosts:192.168.0.4"
- "flask-server.expression.hosts:192.168.0.4"
entrypoint: celery flower -A app.async_tasks.celery_config.app --address=0.0.0.0 --port=5555
nginx:
image: nginx:stable
container_name: exp-nginx
restart: always
network_mode: "host"
volumes:
- ./build-scripts/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./build-scripts/nginx/conf.d:/etc/nginx/conf.d
# 证书不要放在代码仓库中
- /root/expression/nginx/cert:/etc/nginx/cert
# 挂载日志路径
- ./log/nginx:/var/log/nginx
# 挂入静态文件
- /root/expression/expression-vue/dist:/var/www/expression-vue/dist
- /root/expression/expression-wx-static:/var/www/expression-wx-static
jenkins:
#!/bin/bash
base_dir='/root/expression/'
source_dir='/root/expression/expression-flask'
git_repo='git@git.mooctest.net:Expressiveness/expression-flask.git'
service_name_web='web'
container_name_web='exp-web'
service_name_flower='flower'
container_name_flower='exp-flower'
function get_latest_source_code(){
# cd /somedir/doesnot/exisit
echo "=============Start get latest source code============="
if [ ! -d $source_dir ];then
mkdir -p $base_dir &&
cd $base_dir &&
git clone --recursive $git_repo
else
cd $source_dir &&
git checkout . &&
git pull &&
echo "update submodule..." &&
git submodule init &&
git submodule update
fi &&
echo "=============End get latest source code============="
}
function restart_project(){
echo "======== services runnig:" &&
docker-compose ps &&
echo . &&
echo "restarting services..." &&
lns=`docker-compose ps|grep $service_name_web|grep Up|wc -l`
if [[ $lns -gt 0 ]]; then
docker-compose restart $service_name_web $service_name_flower
else
docker-compose up -d $service_name_web $service_name_flower
fi &&
echo "====Wait 20s for uwsgi to respawn before re-checking processes===="
sleep 20s &&
docker-compose ps &&
echo "========================"
}
function tail_log(){
echo -e "last 23 lines of the log file:
" &&
docker-compose logs --tail 23 -t $service_name_web
echo -e "
========================"
lns=`docker-compose ps|grep $container_name_web|grep Up|wc -l`
if [[ $lns -gt 0 ]]; then
exit 0
else
exit 1
fi
}
get_latest_source_code &&
cd $source_dir &&
restart_project &&
tail_log