实战 -- 通过docker-compose来实现git中的app项目一键部署

发现在git上写的flask app项目,部署起来还是蛮花时间的。于是想到了用docker 来部署这个项目,并通过docker-compose把这些如flask app/mysql/elasticsearch 相互依赖的docker container连接起来,实现整个flask app项目环境的一键部署。这样,不但方便了自己的使用,而且对使用这个项目的用户来说,可以实现用户环境的快速部署。用户可以非常方便的把这个app应用环境跑起来进行测试。

我的flask app项目

  • 项目git地址
  • docker-container之间的关系:
    1. microblog -- flask app
    2. elasticsearch -- elasticsearch for search
    3. mysqldb -- mysql database
    

创建microblog的Dockerfile

下面在my_microblog的git项目根目录中增加一个Dockerfile ,用来创建microblog的docker image.这个Dockerfile 会pull python:3.6-alpine 官方python3.6 image,创建一个microblog用户,copy本项目中的所有相关运行文件,安装pipenv并根据项目中的Pipfile文件安装所有项目依赖包,最后用./boot.sh启动app,并对外EXPOSE TCP 5000端口,以接收用户访问连接。
Dockerfile文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FROM python:3.6-alpine

RUN adduser -D microblog
WORKDIR /home/microblog

COPY Pipfile Pipfile
COPY app app
COPY migrations migrations
COPY microblog.py boot.sh ./

ENV FLASK_APP microblog.py

RUN chown -R microblog:microblog ./ \
&& chmod +x boot.sh \
&& dos2unix boot.sh \
&& pip install pipenv

USER microblog
RUN pipenv install
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]

增加一个boot.sh文件,这个文件用来启动flask app:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh
# this script is used to boot a Docker container
cd /home/microblog
source `pipenv --venv`/bin/activate
# waiting mysqldb start && create microblog db tables
while true; do
flask db upgrade
if [[ "$?" == "0" ]]; then
break
fi
echo Deploy command failed, retrying in 5 secs...
sleep 5
done
#flask translate compile
# run this app through gunicorn
exec gunicorn -b :5000 --access-logfile - --error-logfile - microblog:app

使用docker-compose.yml 文件来组合整个应用环境

上面,已经把microblog app的python docker image准备好了,但这个应用还有search功能 ,需要elasticsearch及mysql 数据库。如何整合这些应用呢?可以使用docker-compose.yml文件,把所有这三个docker image整合在一起,作为三个service 运行启来。下面是docker-compose.yml文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
version: '3'

services:
# start microblog app
# build a microblog image at . dir
microblog:
build: .
# image: microblog
container_name: microblog
ports:
- "5000:5000"
env_file:
- .env_online
links:
- mysqldb
- elasticsearch

# start elasticsearch service
# need elasticsearch version 6 above
elasticsearch:
#image: docker.elastic.co/elasticsearch/elasticsearch:6.4.0
image: docker.elastic.co/elasticsearch/elasticsearch:6.0.1
container_name: elasticsearch
env_file:
- .env_online
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata:/usr/share/elasticsearch/data
# start mysql service
# map a 33060 port for Navicat connect
# set mysql root password and host allow microblog service can connect from 172.0.0.0/8
# https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-more-topics.html#docker-persisting-data-configuration
mysqldb:
image: mysql/mysql-server:5.7
container_name: mysqldb
# Chinese support
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
# export 33060 port for manage outside
ports:
- "33060:3306"
env_file:
- .env_online
volumes:
- mysqldata:/var/lib/mysql

volumes:
esdata:
driver: local
mysqldata:
driver: local

所有这三个image运行所需要的环境变量的设置,我都放在了example.env.online文件中,在配置完成后,把这个文件更名为.env.online就可以了,在运行docker-compose up命令时,文件中的变量会传递到这三个container中,下面是example.env.online文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
### 1.  config your setting in example.env_online
### 2. rename this file from example.env_online to env_online
### 3. run "docker-compose up" command to start your microblog app
### 4. Access URL: http://127.0.0.1:5000

### MICROBLOG SETTING
# FLASK_APP NAME
FLASK_APP=microblog.py
# SECRET_KEY
SECRET_KEY=YourSecretKeyllkasjdlkfjkdkjfjiety;alskdjflsadhfgsDFGWSD
# Email setting
MAIL_DEBUG=1
MAIL_SERVER=smtp.qq.com
MAIL_PORT=465
MAIL_USE_SSL=True
MAIL_USE_TSL=False
MAIL_USERNAME=yourqq@qq.com
MAIL_PASSWORD=yourqqpassword
MAIL_SENDER=yourqq@qq.com
# YOUDAO translation setting
YOUDAO_APPKEY=YOUR_YOUDAO_APPKEY
YOUDAO_SECRET_KEY=YOUR_YOUDAO_SECRET_KEY
# elasticsearch url setting
ELASTICSEARCH_URL=http://elasticsearch:9200
# SQLALCHEMY URI
DATABASE_URL=mysql+cymysql://microblog:your_mysql_user_password@mysqldb:3306/microblog

### ElisticSearch_SETTING
discovery.type=single-node
bootstrap.memory_lock=true
"ES_JAVA_OPTS=-Xms512m -Xmx512m"

### MYSQL_SETTING
MYSQL_ROOT_PASSWORD=your_root_passowrd
MYSQL_ROOT_HOST=172.%.%.%_or_your_root_host
MYSQL_DATABASE=microblog
MYSQL_USER=microblog
MYSQL_PASSWORD=your_mysql_user_password

用户只要在安装好docker及docker-compose后,运行 docker-compose up ,就可以把这个应用环境给启动起来了。

在README.txt中,写上完整的运行方法

最后,在README.txt中,写上完整的运行方法及依赖的运行环境,用户就可以很快的把我的flask app运行启来了。README.txt文件内容如下:

1
2
3
4
5
6
7
8
9
## Welcome to my microblog
This is my flask practice according to [The Flask Mega-Tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world).
## How to start this app use docker-compose(need a docker-ce & docker-compose 3 enviroment on your system)
### 1. git clone my_microblog, run "git clone https://github.com/w8833531/my_microblog && cd my_microblog" command.
### 2. config your setting in example.env_online file
### 3. rename this file from example.env_online to .env_online run "mv example.env_online .env_online" command
### 4. run "docker-compose up" command to start your microblog app
### 5. Access URL: http://127.0.0.1:5000
### 6. Stop & remove run "docker-compose stop" to stop all docker containers, run "docker-compose rm" to rm all docker containers, run ", run "docker volume rm microblog_mysqldata" to remove mysqldata volume"

坚持原创技术分享,您的支持将鼓励我继续创作!