云原生之使用Docker部署Python应用
- 一、检查系统版本
- 1.检查系统 版本
- 2.检查系统内核
- 二、检查docker状态
- 三、编辑python文件
- 1.创建目录
- 2.编辑test.py文件
- 四、构建镜像
- 1.编辑dockerfile文件
- 2.使用dockerfile构建镜像
- 五、运行镜像容器
- 1.运行python_app容器
- 2.查看容器状态
- 六、访问Python应用
一、检查系统版本
1.检查系统 版本
[root@node ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@node ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
2.检查系统内核
[root@node ~]# uname -r
3.10.0-957.el7.x86_64
二、检查docker状态
[root@node ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-10-31 03:28:51 CST; 2 days ago
Docs: https://docs.docker.com
Main PID: 11884 (dockerd)
Tasks: 20
Memory: 2.6G
CGroup: /system.slice/docker.service
└─11884 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.196245118+08:00" level=info msg="Container failed to exit within 10s of sig...cc518a551
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.206220804+08:00" level=info msg="Container failed to exit within 10s of sig...161b81627
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.233055677+08:00" level=info msg="Container failed to exit within 10s of sig...fa1863c3d
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.243086387+08:00" level=info msg="Container failed to exit within 10s of sig...3efcbb91f
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.255572629+08:00" level=info msg="ignoring event" container=d535649eed1277d3...skDelete"
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.261318979+08:00" level=info msg="Container failed to exit within 10s of sig...c621d2f74
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.289790332+08:00" level=info msg="ignoring event" container=df6c0a3d1190d674...skDelete"
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.444356587+08:00" level=info msg="ignoring event" container=0924fcec04ff7f24...skDelete"
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.474083648+08:00" level=info msg="ignoring event" container=8408fdf0dc109387...skDelete"
Nov 03 00:55:51 node dockerd[11884]: time="2022-11-03T00:55:51.542109102+08:00" level=info msg="ignoring event" container=c7e17f3b7f825163...skDelete"
Hint: Some lines were ellipsized, use -l to show in full.
三、编辑python文件
1.创建目录
[root@node ~]# mkdir /data/python
[root@node ~]# cd /data/python/
[root@node python]# ls
2.编辑test.py文件
[root@node python]# cat test.py
from flask import Flask
# 安装依赖
# pip3 install -U flask
app = Flask(__name__)
@app.route('/')
def index():
return "The world is beautiful because of you!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8100)
# 浏览器访问测试
# http://127.0.0.1:8100/
四、构建镜像
1.编辑dockerfile文件
[root@node python]# cat dockerfile
FROM centos:7.8.2003
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo;
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo;
RUN sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo;
RUN yum makecache fast;
RUN yum install python3-devel python3-pip -y
RUN pip3 install -i https://pypi.douban.com/simple flask
COPY test.py /opt
WORKDIR /opt
EXPOSE 8100
CMD ["python3","test.py"]
2.使用dockerfile构建镜像
docker build --no-cache -t “python_app” .
Collecting click>=7.1.2 (from flask)
Downloading https://pypi.doubanio.com/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl (97kB)
Collecting itsdangerous>=2.0 (from flask)
Downloading https://pypi.doubanio.com/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl
Collecting Jinja2>=3.0 (from flask)
Downloading https://pypi.doubanio.com/packages/20/9a/e5d9ec41927401e41aea8af6d16e78b5e612bca4699d417f646a9610a076/Jinja2-3.0.3-py3-none-any.whl (133kB)
Collecting Werkzeug>=2.0 (from flask)
Downloading https://pypi.doubanio.com/packages/f4/f3/22afbdb20cc4654b10c98043414a14057cd27fdba9d4ae61cea596000ba2/Werkzeug-2.0.3-py3-none-any.whl (289kB)
Collecting importlib-metadata; python_version < "3.8" (from click>=7.1.2->flask)
Downloading https://pypi.doubanio.com/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl
Collecting MarkupSafe>=2.0 (from Jinja2>=3.0->flask)
Downloading https://pypi.doubanio.com/packages/fc/d6/57f9a97e56447a1e340f8574836d3b636e2c14de304943836bd645fa9c7e/MarkupSafe-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting dataclasses; python_version < "3.7" (from Werkzeug>=2.0->flask)
Downloading https://pypi.doubanio.com/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata; python_version < "3.8"->click>=7.1.2->flask)
Downloading https://pypi.doubanio.com/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata; python_version < "3.8"->click>=7.1.2->flask)
Downloading https://pypi.doubanio.com/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Installing collected packages: zipp, typing-extensions, importlib-metadata, click, itsdangerous, MarkupSafe, Jinja2, dataclasses, Werkzeug, flask
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.3 click-8.0.4 dataclasses-0.8 flask-2.0.3 importlib-metadata-4.8.3 itsdangerous-2.0.1 typing-extensions-4.1.1 zipp-3.6.0
Removing intermediate container 4df40876d4fd
---> a0a24e6b22e6
Step 8/11 : COPY test.py /opt
---> b0d8e6ddf6df
Step 9/11 : WORKDIR /opt
---> Running in efe38fb11eb0
Removing intermediate container efe38fb11eb0
---> 1b4f3d085ecc
Step 10/11 : EXPOSE 8100
---> Running in 8f09099feac2
Removing intermediate container 8f09099feac2
---> 77a9dfddfe2e
Step 11/11 : CMD ["python3","test.py"]
---> Running in 19aa9e47ec35
Removing intermediate container 19aa9e47ec35
---> cadbb7e67c48
Successfully built cadbb7e67c48
Successfully tagged python_app:latest
五、运行镜像容器
1.运行python_app容器
[root@node python]# docker run -d --name web_flask --restart always -p 8100:8100 python_app:latest
5c23f432b05efc9833a0826b3570a4c672e7ee0accb173871109febba43d9564
2.查看容器状态
[root@node python]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c23f432b05e python_app:latest "python3 test.py" 10 seconds ago Up 9 seconds 0.0.0.0:8100->8100/tcp, :::8100->8100/tcp web_flask