前言
因为之前根据一些网上的教程一个个部分实践过整个部署流程,但都是根据现有的程序/ymal文件等进行创建部署,未能根据自己特定的项目进行部署。因此,这篇博文,打算完整部署一个自己编写的python+flask+html项目。
构建镜像的过程中有很多出现的问题,笔者把过程也都列出来了,或许有些朋友会遇到一些类似的问题,可供参考。若是觉得累赘,也可直接跳到关键步骤进行查看(都标出来了)
此帖主要为记录自己部署的流程!
第一步:构建镜像
0 第一次构建
root@master:/home/hqc/docker_learning
root@master:/home/hqc/docker_learning/plus-of-arrays
root@master:/home/hqc/docker_learning/plus-of-arrays
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>plus of array</title>
</head>
<body>
list1 is :{{list1}}
<br>
list2 is :{{list2}}
<br>
<br>
list1 plus list2 is : {{list}}
</body>
</html>
root@master:/home/hqc/docker_learning/plus-of-arrays
plus.html templates
root@master:/home/hqc/docker_learning/plus-of-arrays
root@master:/home/hqc/docker_learning/plus-of-arrays
templates
root@master:/home/hqc/docker_learning/plus-of-arrays
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/index')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return render_template('plus.html',list = list,list1 = list1,list2 = list2)
if __name__ == '__main__':
app.run()
root@master:/home/hqc/docker_learning/plus-of-arrays
array-plus.py templates
root@master:/home/hqc/docker_learning/plus-of-arrays
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [10/Dec/2021 11:36:18] "GET /index HTTP/1.1" 200 -
127.0.0.1 - - [10/Dec/2021 11:36:19] "GET /index HTTP/1.1" 200 -
root@master:/home/hqc/docker_learning/plus-of-arrays
FROM python
RUN mkdir -p /array-plus \
COPY ./templates/* /array-plus/
COPY array-plus.py /array-plus/
EXPOSE 80
RUN /bin/bash -c 'echo init ok'
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 4.608kB
Step 1/6 : FROM python
---> f48ea80eae5a
Step 2/6 : RUN mkdir -p /array-plus
---> Using cache
---> 2e1bfa6c2c76
Step 3/6 : COPY ./templates/* /array-plus/
---> 385c39be4fba
Step 4/6 : COPY array-plus.py /array-plus/
---> 5523bef7ba17
Step 5/6 : EXPOSE 80
---> Running in 10de283ba678
Removing intermediate container 10de283ba678
---> 922b030ff0a1
Step 6/6 : RUN /bin/bash -c 'echo init ok'
---> Running in 32e175765578
init ok
Removing intermediate container 32e175765578
---> 0ed6350571d5
Successfully built 0ed6350571d5
Successfully tagged array-plus:v1.0
root@master:/home/hqc/docker_learning/plus-of-arrays
1 尝试解决1
分析原因:结合上次部署html项目时需要加入一个ui界面用于显示的经验来说,可能是少了ui文件
root@master:/home/hqc/docker_learning/plus-of-arrays
FROM python
RUN mkdir -p /array-plus \
&& rm /etc/nginx/conf.d/default.conf
COPY ./templates/* /array-plus/
COPY array-plus.py /array-plus/
COPY plus.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
RUN /bin/bash -c 'echo init ok'
server {
listen 80;
location / {
root /array-plus/;
index lovestory.html;
}
}
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM nginx
---> ea335eea17ab
Step 2/7 : RUN mkdir -p /array-plus && rm /etc/nginx/conf.d/default.conf
---> Running in 73364b010397
Removing intermediate container 73364b010397
---> efcd0c60672d
Step 3/7 : COPY ./templates/* /array-plus/
---> 67fbcbf54aed
Step 4/7 : COPY array-plus.py /array-plus/
---> 2613cca37078
Step 5/7 : COPY plus.conf /etc/nginx/conf.d/default.conf
---> 9b49e46e5093
Step 6/7 : EXPOSE 80
---> Running in 83c146f1ac45
Removing intermediate container 83c146f1ac45
---> b7778c67197c
Step 7/7 : RUN /bin/bash -c 'echo init ok'
---> Running in 4585a0bb8fd7
init ok
Removing intermediate container 4585a0bb8fd7
---> e3cddd224cc5
Successfully built e3cddd224cc5
Successfully tagged array-plus:v1.1
root@master:/home/hqc/docker_learning/plus-of-arrays
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
2021/12/10 03:54:16 [notice] 1
...
root@master:/home/hqc/docker_learning/plus-of-arrays
55f463aa181dd84edf3fb83232fdc4d4fb0cbad6076b170d54f93aef4e5f00ee
但是,ip+端口号访问失败
2 尝试解决2
分析原因:怀疑可能是py程序后面要跟的‘/index’的问题
root@master:/home/hqc/docker_learning/plus-of-arrays
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return render_template('plus.html',list = list,list1 = list1,list2 = list2)
if __name__ == '__main__':
app.run()
root@master:/home/hqc/docker_learning/plus-of-arrays
array-plus.py templates
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM nginx
---> ea335eea17ab
Step 2/7 : RUN mkdir -p /array-plus && rm /etc/nginx/conf.d/default.conf
---> Using cache
---> efcd0c60672d
Step 3/7 : COPY ./templates/* /array-plus/
---> Using cache
---> 67fbcbf54aed
Step 4/7 : COPY array-plus.py /array-plus/
---> d1856a23fa81
Step 5/7 : COPY plus.conf /etc/nginx/conf.d/default.conf
---> d26866bbdc37
Step 6/7 : EXPOSE 80
---> Running in 906672daa310
Removing intermediate container 906672daa310
---> 2c51759a4bec
Step 7/7 : RUN /bin/bash -c 'echo init ok'
---> Running in 8b87e01ac112
init ok
Removing intermediate container 8b87e01ac112
---> b48902c70940
Successfully built b48902c70940
Successfully tagged array-plus:v1.2
root@master:/home/hqc/docker_learning/plus-of-arrays
12b61389ec321693aec05db88a5d7b0df585e17af3cf418e635aeaf97bf584b7
仍然无法访问:
3 尝试解决3
分析原因:可能是py程序默认端口是5000的问题,而不应该是80
root@master:/home/hqc/docker_learning/plus-of-arrays
EXPOSE 5000
root@master:/home/hqc/docker_learning/plus-of-arrays
listen 5000
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM nginx
---> ea335eea17ab
Step 2/7 : RUN mkdir -p /array-plus && rm /etc/nginx/conf.d/default.conf
---> Using cache
---> efcd0c60672d
Step 3/7 : COPY ./templates/* /array-plus/
---> Using cache
---> 67fbcbf54aed
Step 4/7 : COPY array-plus.py /array-plus/
---> Using cache
---> d1856a23fa81
Step 5/7 : COPY plus.conf /etc/nginx/conf.d/default.conf
---> e2f7f1d236e7
Step 6/7 : EXPOSE 5000
---> Running in 2bd9f2d36bdc
Removing intermediate container 2bd9f2d36bdc
---> 5cc24d47cdeb
Step 7/7 : RUN /bin/bash -c 'echo init ok'
---> Running in 3e7532707bcd
init ok
Removing intermediate container 3e7532707bcd
---> 080098572156
Successfully built 080098572156
Successfully tagged array-plus:v1.3
root@master:/home/hqc/docker_learning/plus-of-arrays
4786aa5b12f6d7aa114cbf1b4dbf2ea4a399c0fb25b9036eb76bee843213688e
仍然失败!!!qswl
4 尝试解决4
root@master:/home/hqc/docker_learning/plus-of-arrays
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4786aa5b12f6 array-plus:v1.3 "/docker-entrypoint.…" 4 hours ago Up 4 hours 80/tcp, 0.0.0.0:1113->5000/tcp, :::1113->5000/tcp elastic_swirles
12b61389ec32 array-plus:v1.2 "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:1112->80/tcp, :::1112->80/tcp relaxed_merkle
55f463aa181d array-plus:v1.1 "/docker-entrypoint.…" 4 hours ago Up 4 hours 0.0.0.0:1111->80/tcp, :::1111->80/tcp hungry_wright
root@master:/home/hqc/docker_learning/plus-of-arrays
root@12b61389ec32:/
array-plus bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@12b61389ec32:/
root@12b61389ec32:/array-plus
array-plus.py plus.html
root@master:/home/hqc/docker_learning/plus-of-arrays
FROM nginx
RUN mkdir -p /array-plus \
&& mkdir -p /array-plus/templates \
&& rm /etc/nginx/conf.d/default.conf
COPY ./templates/* /array-plus/templates
COPY array-plus.py /array-plus/
COPY plus.conf /etc/nginx/conf.d/default.conf
EXPOSE 5000
RUN /bin/bash -c 'echo init ok'
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/7 : FROM nginx
---> ea335eea17ab
Step 2/7 : RUN mkdir -p /array-plus && mkdir -p /array-plus/templates && rm /etc/nginx/conf.d/default.conf
---> Using cache
---> 52cdfa067df3
Step 3/7 : COPY ./templates/* /array-plus/templates
---> f0b746b473ba
Step 4/7 : COPY array-plus.py /array-plus/
---> 4bf1d3f2b49d
Step 5/7 : COPY plus.conf /etc/nginx/conf.d/default.conf
---> 257f3c1d8d37
Step 6/7 : EXPOSE 5000
---> Running in ecebc719a0ed
Removing intermediate container ecebc719a0ed
---> 1ee4941610ef
Step 7/7 : RUN /bin/bash -c 'echo init ok'
---> Running in baf0c12e99a2
init ok
Removing intermediate container baf0c12e99a2
---> 07d60b8f1224
Successfully built 07d60b8f1224
Successfully tagged array-plus:v1.5
root@master:/home/hqc/docker_learning/plus-of-arrays
55e31feeaed7ddc9217b0ef9ed4058e0a6afd175798beb01810b06ef9232bbf1
仍然无法正常访问!
root@master:/home/hqc/docker_learning/plus-of-arrays
root@55e31feeaed7:/
array-plus bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@55e31feeaed7:/
root@55e31feeaed7:/array-plus
array-plus.py templates
root@55e31feeaed7:/array-plus
root@55e31feeaed7:/array-plus/templates
plus.html
root@55e31feeaed7:/array-plus/templates
root@55e31feeaed7:/array-plus
bash: python: command not found
5 尝试解决5(关键步骤)
分析原因:不能基于nginx制作镜像,因为里边没有配置python和flask环境,自然无法运行该项目;另外,可能并不需要制作ui界面
root@master:/home/hqc/docker_learning/plus-of-arrays
FROM python
RUN mkdir -p /array-plus \
&& mkdir -p /array-plus/templates \
&& pip install flask
COPY ./templates/plus.html /array-plus/templates
COPY array-plus.py /array-plus/
WORKDIR /array-plus
EXPOSE 5000
RUN /bin/bash -c 'echo init ok'
CMD ["python", "array-plus.py"]
WORKDIR /array-plus 语句的作用是:进入该容器时会默认进入工作目录- 添加了
CMD ["python", "array-plus.py"] 语句,用来执行py程序 - 最重要的是换了基础镜像
python ,以及添加了pip install flask 语句用来配置flask环境 - 注意拷贝本机文件到容器中的方式:
COPY ./templates/plus.html /array-plus/templates
root@master:/home/hqc/docker_learning/plus-of-arrays
访问失败???? 但失败界面和之前403报错的不同了,也不知道是为啥。。
root@master:/home/hqc/docker_learning/plus-of-arrays
root@9c1312d06381:/array-plus
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "/array-plus/array-plus.py", line 19, in <module>
app.run()
File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 920, in run
run_simple(t.cast(str, host), port, self, **options)
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 1010, in run_simple
inner()
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 950, in inner
srv = make_server(
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 782, in make_server
return ThreadedWSGIServer(
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 688, in __init__
super().__init__(server_address, handler)
File "/usr/local/lib/python3.10/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/local/lib/python3.10/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.10/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
分析原因:可能是之前运行的容器暴露的都是5000,且并未停止 解决:停掉之前的所有容器
docker stop 55e31feeaed7
但仍然未解决???
6 尝试解决6
因为不清楚为啥停掉了之前的所有运行中的容器,仍然显示端口已被占用,因此,决定暴露一个新的端口5001。
root@master:/home/hqc/docker_learning/plus-of-arrays
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return render_template('plus.html',list = list,list1 = list1,list2 = list2)
if __name__ == '__main__':
app.run(port = '5001')
还是没解决??? 突然发现端口好像不需要打上引号。。 修改为app.run(port = 5001) 之后运行,还是显示端口被占用???哎~累了累了
7 尝试解决7
一直被占用adress所困扰,干脆把默认ip换成本机IP,端口也换到6000 可以成功构建,也可以映射端口2222后运行可以,但docker ps 查看容器时没有,并且发现运行容器后就立马退出了???咱也不知道是啥问题呀
只好先改回默认adress和port
root@master:/home/hqc/docker_learning/plus-of-arrays
root@master:/home/hqc/docker_learning/plus-of-arrays
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/8 : FROM python
---> f48ea80eae5a
Step 2/8 : RUN mkdir -p /array-plus && mkdir -p /array-plus/templates && pip install flask
---> Using cache
---> 1348fc1e170d
Step 3/8 : COPY ./templates/plus.html /array-plus/templates
---> Using cache
---> 63a3d5c65ade
Step 4/8 : COPY array-plus.py /array-plus/
---> Using cache
---> 01a1e208d021
Step 5/8 : WORKDIR /array-plus
---> Using cache
---> 47a2a2cf5902
Step 6/8 : EXPOSE 5000
---> Using cache
---> 2c373fb07eae
Step 7/8 : RUN /bin/bash -c 'echo init ok'
---> Using cache
---> f1bae3ef5919
Step 8/8 : CMD ["python", "array-plus.py"]
---> Using cache
---> c93ff28fea14
Successfully built c93ff28fea14
Successfully tagged array-plus:v2.4
root@master:/home/hqc/docker_learning/plus-of-arrays
ce08f056a47127f455195fd835b79b1238ccebd010ee19247a3a33acc3be4d0f
root@master:/home/hqc/docker_learning/plus-of-arrays
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce08f056a471 array-plus:v2.4 "python array-plus.py" 3 seconds ago Up 3 seconds 0.0.0.0:1121->5000/tcp, :::1121->5000/tcp goofy_ganguly
root@master:/home/hqc/docker_learning/plus-of-arrays
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
但还是无法访问
root@master:/home/hqc/docker_learning/plus-of-arrays
root@ce08f056a471:/array-plus
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "/array-plus/array-plus.py", line 19, in <module>
app.run()
File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 920, in run
run_simple(t.cast(str, host), port, self, **options)
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 1010, in run_simple
inner()
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 950, in inner
srv = make_server(
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 782, in make_server
return ThreadedWSGIServer(
File "/usr/local/lib/python3.10/site-packages/werkzeug/serving.py", line 688, in __init__
super().__init__(server_address, handler)
File "/usr/local/lib/python3.10/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/local/lib/python3.10/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.10/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
分析原因:可能是dockerfile文件中已经执行了,再在容器中运行就会报该错误,因此,后续可先不用管此错误。
root@master:/home/hqc/docker_learning/plus-of-arrays
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [10/Dec/2021 18:22:59] "GET / HTTP/1.1" 200 -
提出疑问:为啥docker运行成功仍然无法访问呢??????qswl
8 尝试解决8
telnet [本机IP] [端口号] 查看端口是否连通
root@master:/home/hqc/docker_learning/plus-of-arrays
Trying 172.30.117.60...
telnet: Unable to connect to remote host: Connection refused
而
root@master:/home/hqc/docker_learning/plus-of-arrays
root@da5a26fc0ace:/array-plus
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>plus of array</title>
</head>
<body>
list1 is :[1, 2, 3, 4, 5, 6]
<br>
list2 is :[2, 3, 4, 5, 6, 7]
<br>
<br>
list1 plus list2 is : [3, 5, 7, 9, 11, 13]
</body>
</html>
root@master:/home/hqc/docker_learning/plus-of-arrays
20443d540f107da71d115d1ab40eeebd7edd487869fd05ee569d0b369a6ec700
root@master:/home/hqc/docker_learning/plus-of-arrays
curl: (7) Failed to connect to 172.30.117.60 port 1133: 拒绝连接
另一种:
root@master:/home/hqc/docker_learning/plus-of-arrays
* Serving Flask app 'array-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Dec/2021 09:17:59] "GET / HTTP/1.1" 200 -
但要注意:须新开一个终端进行curl,之前的不能关闭,关闭的话该容器会立即停止运行。
因此,我有充分理由怀疑:只是端口映射网络通信的问题,程序是一切正常可以运行的。
00 第二次构建
总感觉是多了一个html文件而出的问题,还是简单点吧。。。
root@master:/home/hqc/docker_learning/plus-of-arrays
root@master:/home/hqc/docker_learning
root@master:/home/hqc/docker_learning
root@master:/home/hqc/docker_learning/json_plus
from flask import Flask,render_template,jsonify
app = Flask(__name__)
@app.route('/')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return jsonify(list)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
root@master:/home/hqc/docker_learning/json_plus
root@master:/home/hqc/docker_learning/json_plus
Dockerfile json-plus.py
root@master:/home/hqc/docker_learning/json_plus
* Serving Flask app 'json-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Dec/2021 20:28:57] "GET / HTTP/1.1" 200 -
本地运行访问成功:
root@master:/home/hqc/docker_learning/json_plus
Sending build context to Docker daemon 3.584kB
Step 1/7 : FROM python
---> f48ea80eae5a
Step 2/7 : RUN mkdir -p /json-plus && pip install flask
---> Running in 6a082693653b
Collecting flask
Downloading Flask-2.0.2-py3-none-any.whl (95 kB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
Downloading click-8.0.3-py3-none-any.whl (97 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, flask
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 flask-2.0.2 itsdangerous-2.0.1
WARNING: You are using pip version 21.2.4; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 6a082693653b
---> 732d2f5d4c0d
Step 3/7 : COPY json-plus.py /json-plus/
---> 82cb8d60ea90
Step 4/7 : WORKDIR /json-plus
---> Running in 39a187aa7828
Removing intermediate container 39a187aa7828
---> ee39664fcead
Step 5/7 : EXPOSE 5000
---> Running in ba016287ec11
Removing intermediate container ba016287ec11
---> 7e821ccd74ef
Step 6/7 : RUN /bin/bash -c 'echo init ok'
---> Running in 787bc3e0b328
init ok
Removing intermediate container 787bc3e0b328
---> 17f891c4ed0b
Step 7/7 : CMD ["python", "json-plus.py"]
---> Running in a6db9776c7ca
Removing intermediate container a6db9776c7ca
---> 7dc9d402b001
Successfully built 7dc9d402b001
Successfully tagged json-plus:v1.0
root@master:/home/hqc/docker_learning/json_plus
* Serving Flask app 'json-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Dec/2021 12:34:17] "GET / HTTP/1.1" 200 -
docker运行浏览器中还是不能访问??? 但进入容器中还是可以访问的。
root@master:/home/hqc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1dd28d8c2a61 json-plus:v1.0 "python json-plus.py" About a minute ago Up About a minute 5000/tcp optimistic_elgamal
root@master:/home/hqc
root@1dd28d8c2a61:/json-plus
[3,5,7,9,11,13]
出现和第一次构建一毛一样的问题qswl 到底是哪出了问题呢?
01 解决(核心步骤)
对比之前的flask-hello-world的代码,发现有些不同!!!
root@master:/home/hqc/docker_learning/json_plus
from flask import Flask,render_template,jsonify
app = Flask(__name__)
@app.route('/')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return jsonify(list)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
root@master:/home/hqc/docker_learning/json_plus
root@master:/home/hqc/docker_learning/json_plus
Sending build context to Docker daemon 3.584kB
Step 1/7 : FROM python
---> f48ea80eae5a
Step 2/7 : RUN mkdir -p /json-plus && pip install flask
---> Using cache
---> 732d2f5d4c0d
Step 3/7 : COPY json-plus.py /json-plus/
---> 0a25ade532eb
Step 4/7 : WORKDIR /json-plus
---> Running in 3417773df444
Removing intermediate container 3417773df444
---> b288adfd98d2
Step 5/7 : EXPOSE 5000
---> Running in c547cd285afe
Removing intermediate container c547cd285afe
---> 9f7fd3c34ec6
Step 6/7 : RUN /bin/bash -c 'echo init ok'
---> Running in 0281b502a454
init ok
Removing intermediate container 0281b502a454
---> f4dd1c67120a
Step 7/7 : CMD ["python", "json-plus.py"]
---> Running in 2260960099db
Removing intermediate container 2260960099db
---> fab5150c7c5e
Successfully built fab5150c7c5e
Successfully tagged json-plus:v1.2
root@master:/home/hqc/docker_learning/json_plus
* Serving Flask app 'json-plus' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 501-077-407
172.17.0.1 - - [11/Dec/2021 12:53:45] "GET / HTTP/1.1" 200 -
^Croot@master:/home/hqc/docker_learning/json_plus
29a2a2bedd9a79ffdc9b0175d25afb2730f6b4ee9dad0a98d72e0c8d6c0d4e8a
成功!!!! 以此类推,前面的应该也是这个问题。后面再测试,回家。
02 顺便解决第一次构建的问题
也是同样的问题,以下是步骤:
root@master:/home/hqc/docker_learning/plus-of-arrays
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def index():
list1 = [1,2,3,4,5,6]
list2 = [2,3,4,5,6,7]
list = [ list1[i]+list2[i] for i in range(min(len(list1),len(list2))) ]
return render_template('plus.html',list = list,list1 = list1,list2 = list2)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
root@master:/home/hqc/docker_learning/plus-of-arrays
Sending build context to Docker daemon 5.632kB
Step 1/8 : FROM python
---> f48ea80eae5a
Step 2/8 : RUN mkdir -p /array-plus && mkdir -p /array-plus/templates && pip install flask
---> Using cache
---> 1348fc1e170d
Step 3/8 : COPY ./templates/plus.html /array-plus/templates
---> Using cache
---> 63a3d5c65ade
Step 4/8 : COPY array-plus.py /array-plus/
---> 40370d94dc7f
Step 5/8 : WORKDIR /array-plus
---> Running in 651c9e493a42
Removing intermediate container 651c9e493a42
---> 63e0109a8cb4
Step 6/8 : EXPOSE 5000
---> Running in 312f598ff4ca
Removing intermediate container 312f598ff4ca
---> fcdde5856054
Step 7/8 : RUN /bin/bash -c 'echo init ok'
---> Running in ae89c41bdda4
init ok
Removing intermediate container ae89c41bdda4
---> 34d480284f2d
Step 8/8 : CMD ["python", "array-plus.py"]
---> Running in d6e1cfb713ea
Removing intermediate container d6e1cfb713ea
---> 7f4f56bbf3c8
Successfully built 7f4f56bbf3c8
Successfully tagged array-plus:v2.5
root@master:/home/hqc/docker_learning/plus-of-arrays
891a8dcfe4f9aaca002b93733d0fe04dce338dbe2272c1406d8d916baf914c16
访问成功:
03 分析原因
通过寻找发现是host='0.0.0.0' 的原因,和debug=True 无关。 必须设置host='0.0.0.0' ,否则只能本地运行,无法通过外网访问。 参考的博客 讲的还比较清楚
04 一个疑问
疑问:master上运行了容器,在本机上可以正常访问,但在手机上却没法访问。
一时蒙蔽,忘了必须在同一个局域网下(同一网段)才可以访问。
第一步总结
整体上是对的,访问失败的原因主要是没有设置为可供外网访问,而不是端口映射的原因(因此,前面很多次尝试解决都是找错了方向),但那些过程也不是一无是处,正是在这些失败的过程中,对这玩意儿更加理解,一步步找到问题所在。
最关键的是01 解决(核心步骤) 这步,真正解决了问题。
第二步:上传镜像到阿里云私有仓库
0 登录阿里云私有镜像仓库
root@master:/home/hqc/docker_learning/plus-of-arrays
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/
Login Succeeded
1 给镜像打一个新的标签
关键不可少,否则会push不成功
root@master:/home/hqc/docker_learning/plus-of-arrays
注意 :也可通过直接在图形界面中创建
2 push到镜像仓库
root@master:/home/hqc/docker_learning/plus-of-arrays
The push refers to repository [registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus]
f06f6fb37ea8: Pushed
7395b20bb823: Pushed
bf080306b1d4: Pushed
42ce61e841fa: Mounted from hqc-k8s/ali-flask-hello-world
a9b125f164c3: Mounted from hqc-k8s/ali-flask-hello-world
e24045f8c247: Mounted from hqc-k8s/ali-flask-hello-world
b7b662b31e70: Mounted from hqc-k8s/ali-flask-hello-world
6f5234c0aacd: Mounted from hqc-k8s/ali-flask-hello-world
8a5844586fdb: Mounted from hqc-k8s/ali-flask-hello-world
a4aba4e59b40: Mounted from hqc-k8s/ali-flask-hello-world
5499f2905579: Mounted from hqc-k8s/ali-flask-hello-world
a36ba9e322f7: Mounted from hqc-k8s/ali-flask-hello-world
v2.5: digest: sha256:1dd85ae174eeedbaa5d370e625c6db439c4188634e58d648a6b5d30e28b9aa86 size: 2843
3 查看
push成功!
第三步:部署到k8s
0 将集群启动
root@master:/home/hqc/k8s_test/array-plus
防火墙在系统启动时自动禁用
root@master:/home/hqc/k8s_test/array-plus
root@master:/home/hqc/k8s_test/array-plus
NAME STATUS ROLES AGE VERSION
master Ready master 25d v1.18.0
node01 Ready <none> 20d v1.18.0
1 yaml文件创建deployment
尝试自动创建:报错
root@master:/home/hqc/k8s_test/array-plus
W1213 20:50:23.489941 1289 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
error: unable to match a printer suitable for the output format "yml", allowed formats are: go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,yaml
直接创建:
root@master:/home/hqc/k8s_test/array-plus
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: array-plus-deployment
name: array-plus-deployment
spec:
replicas: 2
selector:
matchLabels:
app: array-plus-deployment
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: array-plus-deployment
spec:
containers:
- image: registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
name: ali-array-plus
resources: {}
ports:
- containerPort: 5000
imagePullPolicy: IfNotPresent
status: {}
root@master:/home/hqc/k8s_test/array-plus
deployment.apps/array-plus-deployment created
2 yaml文件创建service
root@master:/home/hqc/k8s_test/array-plus
apiVersion: v1
kind: Service
metadata:
name: array-plus-deployment
labels:
app: array-plus-deployment
spec:
ports:
- port: 80
targetPort: 5000
nodePort: 30000
protocol: TCP
selector:
app: array-plus-deployment
type: NodePort
root@master:/home/hqc/k8s_test/array-plus
service/array-plus-deployment created
3 查看
root@master:/home/hqc/k8s_test/array-plus
NAME READY STATUS RESTARTS AGE
pod/array-plus-deployment-55657b7c55-wfjtx 0/1 ErrImagePull 0 6m33s
pod/array-plus-deployment-55657b7c55-z8kx2 0/1 ErrImagePull 0 6m33s
pod/helloworld-deployment-79cbf4dcbb-5x6b2 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-n5sps 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-sr8xf 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-wfh5w 1/1 Terminating 0 12d
pod/nginx-f89759699-ksgcb 1/1 Running 2 13d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/array-plus-deployment NodePort 10.102.249.251 <none> 80:30000/TCP 93s
service/helloworld-deployment NodePort 10.106.97.144 <none> 80:30002/TCP 13d
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/nginx NodePort 10.109.178.122 <none> 80:30650/TCP 13d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/array-plus-deployment 0/2 2 0 6m33s
deployment.apps/helloworld-deployment 3/3 3 3 12d
deployment.apps/nginx 1/1 1 1 13d
NAME DESIRED CURRENT READY AGE
replicaset.apps/array-plus-deployment-55657b7c55 2 2 0 6m33s
replicaset.apps/helloworld-deployment-79cbf4dcbb 3 3 3 12d
replicaset.apps/nginx-f89759699 1 1 1 13d
4 问题解决
结合之前的实践经验,问题应该出在节点上没法实时进行拉取,需要在各节点上事先拉取好才行。
root@nodroot@node01:/hroot@nrootrootrorororoot@node01:/home/user
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/
Login Succeeded
root@node01:/home/user
v2.5: Pulling from hqc-k8s/ali-array-plus
647acf3d48c2: Already exists
b02967ef0034: Already exists
e1ad2231829e: Already exists
5576ce26bf1d: Already exists
a66b7f31b095: Already exists
05189b5b2762: Already exists
af08e8fda0d6: Already exists
287d56f7527b: Already exists
dc0580965fb6: Already exists
1cb8e8d308bd: Pull complete
c68499244bc4: Pull complete
1dafd85198a4: Pull complete
Digest: sha256:1dd85ae174eeedbaa5d370e625c6db439c4188634e58d648a6b5d30e28b9aa86
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus:v2.5
root@node01:/home/user
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-beijing.aliyuncs.com/hqc-k8s/ali-array-plus v2.5 7f4f56bbf3c8 2 hours ago 928MB
master中再次查看:发现包括service全部正常
root@master:/home/hqc/k8s_test/array-plus
NAME READY STATUS RESTARTS AGE
pod/array-plus-deployment-55657b7c55-wfjtx 1/1 Running 0 10m
pod/array-plus-deployment-55657b7c55-z8kx2 1/1 Running 0 10m
pod/helloworld-deployment-79cbf4dcbb-5x6b2 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-n5sps 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-sr8xf 1/1 Running 1 12d
pod/helloworld-deployment-79cbf4dcbb-wfh5w 1/1 Terminating 0 12d
pod/nginx-f89759699-ksgcb 1/1 Running 2 13d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/array-plus-deployment NodePort 10.102.249.251 <none> 80:30000/TCP 5m59s
service/helloworld-deployment NodePort 10.106.97.144 <none> 80:30002/TCP 13d
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25d
service/nginx NodePort 10.109.178.122 <none> 80:30650/TCP 13d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/array-plus-deployment 2/2 2 2 10m
deployment.apps/helloworld-deployment 3/3 3 3 12d
deployment.apps/nginx 1/1 1 1 13d
NAME DESIRED CURRENT READY AGE
replicaset.apps/array-plus-deployment-55657b7c55 2 2 2 10m
replicaset.apps/helloworld-deployment-79cbf4dcbb 3 3 3 12d
replicaset.apps/nginx-f89759699 1 1 1 13d
注意 :无需将deployment和service删除重建,直接查看即可
5 运行验证
- master IP + port访问成功
- node IP + port访问成功
- dashboard中查看running成功
到此为止,完全成功!!!
|