docker部署web应用实践分享

前言

没接触docker之前,觉得这个是很悬乎的东西,实践了一把后发现其实还蛮简单的,下面来用实例给大家介绍一下docker是如何部署一个WEB应用的。


ps:关于docker的介绍和安装不在本次分享里哦~同学们可自行补充基础知识。

docker部署前

首先是得有个web应用,我实践的项目是python+flask,目录结构如下:

在部署前,先编写docker file文件,例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 基础镜像
FROM python:3.7.2
# 维护者信息
MAINTAINER xxx@qq.com
# 挂载任务服务路径
VOLUME ["/usr/fin/MyFlaskTest", "/usr/fin/MyFlaskTest"]
# 指定工作目录,保持跟挂载路径一致
WORKDIR /usr/fin/MyFlaskTest
# Flask环境参数变量,可自定义变量
ENV FLASK_ENV Dev
# ENV FLASK_DB_KEY 2019011418570114
EXPOSE 8888
# 将依赖包信息加入镜像
COPY requirements.txt ./
# 安装依赖包
RUN pip install -r requirements.txt
# 执行命令启动gunicorn
CMD gunicorn app:app -b 0.0.0.0:8888 -w 2 --preload

创建镜像

1
2
命令:
sudo docker build -t myflask:mock -f dockerfile .

进入到服务器web应用目录下,执行创建镜像命令。实例:

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
[lcpubsudo@sz2-201-5-centos124 MyFlaskTest]$ sudo docker build -t myflask:mock -f dockerfile .
Sending build context to Docker daemon 58.88kB
Step 1/9 : FROM python:3.7.2
---> 2053ca75899e
Step 2/9 : MAINTAINER xxx@qq.com
---> Running in e89ae5ecf2c2
Removing intermediate container e89ae5ecf2c2
---> cfde4d21fb2e
Step 3/9 : VOLUME ["/usr/fin/MyFlaskTest", "/usr/fin/MyFlaskTest"]
---> Running in b44bb0683f25
Removing intermediate container b44bb0683f25
---> 516a5e994884
Step 4/9 : WORKDIR /usr/fin/MyFlaskTest
Removing intermediate container cbcb02b683f2
---> 400d37be29f2
Step 5/9 : ENV FLASK_ENV Dev
---> Running in 48419ca2d56e
Removing intermediate container 48419ca2d56e
---> 66b7ef6d3a91
Step 6/9 : EXPOSE 8888
---> Running in cc536af2d0b1
Removing intermediate container cc536af2d0b1
---> 4164ebe880ee
Step 7/9 : COPY requirements.txt ./
---> fecb9ec8aa56
Step 8/9 : RUN pip install -r requirements.txt
---> Running in 67a28a5f3b70
Collecting Flask==1.1.1 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Installing collected packages: click, itsdangerous, Werkzeug, MarkupSafe, Jinja2, Flask, SQLAlchemy, Flask-SQLAlchemy, idna, urllib3, certifi, chardet, requests, gunicorn, python-dotenv, marshmallow, environs, pymysql
Successfully installed Flask-1.1.1 Flask-SQLAlchemy-2.3.2 Jinja2-2.11.2 MarkupSafe-1.1.1 SQLAlchemy-1.3.16 Werkzeug-1.0.1 certifi-2020.4.5.1 chardet-3.0.4 click-7.1.2 environs-4.1.0 gunicorn-19.9.0 idna-2.6 itsdangerous-1.1.0 marshmallow-3.5.2 pymysql-0.9.3 python-dotenv-0.13.0 requests-2.18.4 urllib3-1.22
You are using pip version 19.0.3, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 67a28a5f3b70
---> 12c0eb2b3cdd
Step 9/9 : CMD gunicorn app:app -b 0.0.0.0:8888 -w 2 --preload
---> Running in 0123732d1732
Removing intermediate container 0123732d1732
---> f48397a0f1ff
Successfully built f48397a0f1ff
Successfully tagged myflask:mock

查看镜像

1
2
命令:
sudo docker images
1
2
3
[lcpubsudo@sz2-201-5-centos124 MyFlaskTest]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myflask mock f48397a0f1ff 3 minutes ago 953MB

创建和启动容器

1
2
命令:
sudo docker run -v /usr/fin/MyFlaskTest:/usr/fin/MyFlaskTest -d -p 8888:8888 myflask:mock

-d 为后台运行

-p 为指定宿主机端口,不是随机端口

-P 随机端口

-v 挂载目录,宿主机目录:容器目录,路径会自动创建

1
2
[lcpubsudo@sz2-201-5-centos124 MyFlaskTest]$ sudo docker run -v /usr/fin/MyFlaskTest:/usr/fin/MyFlaskTest -d -p 8888:8888 myflask:mock
2c801515b3bf686e3a1d4f7224576d27834140dd632ff679b2974dfbec16dfc3

查看容器运行状态

1
2
命令:
sudo docker ps -a
1
2
3
[lcpubsudo@sz2-201-5-centos124 MyFlaskTest]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c801515b3bf myflask:mock "/bin/sh -c 'gunicor…" 58 seconds ago Up 57 seconds 0.0.0.0:8888->8888/tcp stoic_curie

查看容器日志

1
2
命令:
sudo docker logs -f 容器id
1
2
3
4
5
6
[lcpubsudo@sz2-201-5-centos124 MyFlaskTest]$ sudo docker logs -f 2c801515b3bf
[2020-05-06 09:53:19 +0000] [6] [INFO] Starting gunicorn 19.9.0
[2020-05-06 09:53:19 +0000] [6] [INFO] Listening at: http://0.0.0.0:8888 (6)
[2020-05-06 09:53:19 +0000] [6] [INFO] Using worker: sync
[2020-05-06 09:53:19 +0000] [9] [INFO] Booting worker with pid: 9
[2020-05-06 09:53:19 +0000] [10] [INFO] Booting worker with pid: 10

其他docker命令

  • 停止容器
1
sudo docker stop 容器id
  • 启动容器
1
sudo docker start 容器id
  • 更新容器内第三方库
1
2
3
4
5
6
7
8
9
10
11
第一步:
sudo docker exec -it 容器id /bin/bash

第二步:
pip install 第三方库

第三步:
exit

第四步:
sudo docker commit -m '更新描述' 容器id vpaasthree
  • 删除容器
1
2
先停止容器,再删除
sudo docker rm 容器id
  • 删除镜像
1
docker rmi 镜像id
  • 重启容器
1
docker restart 容器id
  • 查看容器属性工作目录等
1
docker inspec 容器id

结束

是不是很简单呀,快去实践一下吧~!

0%