案例2:创建一个python的web应用,使用Flask,将访问次数记在redis中,通过web首页显示访问次数。
第一步:创建一个工程目录:
mkdir pythondir
cd pythondir
第二步:创建一个web应用:
[root@node1 pythondir]# cat app.py
from flask import Flask
from redis import Redisapp = Flask(__name__)
redis = Redis(host='redis', port=6379)@app.route('/')
def hello():redis.incr('hits')return 'Hello world! I have been see %s times.' % redis.get('hits')if __name__ == "__main__":app.run(host="0.0.0.0", debug=True)
[root@node1 pythondir]# cat requirements.txt
flask
redis
在容器镜像中安装flask和redis应用,需求软件的安装列表。软件需求列表。
在一些应用软件中都有requirements.txt文件。
如果没有安装flask和redis,我们是不能import导入模块。
flask和redis是需要安装到容器镜像中的。
第三步:创建一个Dockerfile文件,创建容器镜像:
[root@node1 pythondir]# cat Dockerfile
FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD python app.py
说明:这个不需要基础镜像,直接python环境就可以。
第四步:创建docker-compose.yaml文件:
[root@node1 pythondir]# cat docker-compose.yaml
version: '2'
services:web:build: .ports:- "5000:5000"volumes:- .:/codedepends_on:- redisredis:image: redis:latest
说明:
1)version2版本支持空格,version3版本支持tab键。tab键四个空格。
2)web依赖redis,也就是depends_on,先启动redis,然后再启动web。
第五步:运行:
docker-compose up
[root@node1 pythondir]# docker-compose up
Creating network "pythondir_default" with the default driver
Creating pythondir_redis_1 ... done
Creating pythondir_web_1 ... done
Attaching to pythondir_redis_1, pythondir_web_1
redis_1 | 1:C 06 Jul 2023 10:56:02.498 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1 | 1:C 06 Jul 2023 10:56:02.498 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1 | 1:C 06 Jul 2023 10:56:02.498 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | 1:M 06 Jul 2023 10:56:02.499 * monotonic clock: POSIX clock_gettime
redis_1 | 1:M 06 Jul 2023 10:56:02.500 * Running mode=standalone, port=6379.
redis_1 | 1:M 06 Jul 2023 10:56:02.500 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 06 Jul 2023 10:56:02.500 # Server initialized
redis_1 | 1:M 06 Jul 2023 10:56:02.500 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | 1:M 06 Jul 2023 10:56:02.500 * Ready to accept connections
web_1 | * Serving Flask app "app" (lazy loading)
web_1 | * Environment: production
web_1 | WARNING: This is a development server. Do not use it in a production deployment.
web_1 | Use a production WSGI server instead.
web_1 | * Debug mode: on
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 982-279-484
web_1 | 192.168.17.1 - - [06/Jul/2023 10:56:08] "GET / HTTP/1.1" 200 -
web_1 | 192.168.17.1 - - [06/Jul/2023 10:56:08] "GET /favicon.ico HTTP/1.1" 404 -
web_1 | 192.168.17.1 - - [06/Jul/2023 10:56:11] "GET / HTTP/1.1" 200 -
web_1 | 192.168.17.1 - - [06/Jul/2023 10:56:12] "GET / HTTP/1.1" 200 -
web_1 | 192.168.17.1 - - [06/Jul/2023 10:56:13] "GET / HTTP/1.1" 200 -
从输出信息中,我们可以看到我通过浏览器进行了页面访问。
[root@node1 pythondir]# docker-compose up -d
Creating network "pythondir_default" with the default driver
Creating pythondir_redis_1 ... done
Creating pythondir_web_1 ... done
[root@node1 pythondir]#
第六步:访问:
在笔记本电脑上的浏览器进行访问:
可以看到访问次数发生了变化。