In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to expand the Python program in docker". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to expand the Python program in docker".
Extend the Python program, connect to the redis database, and do some operations on redis
Create a new App.py with the following contents:
From flask import Flaskfrom redis import Redisimport osimport socketapp = Flask (_ _ name__) redis = Redis (host=os.environ.get ('REDIS_HOST',' 127.0.0.1'), port=6379) @ app.route ('/') def hello (): redis.incr ('hits') return' Hello Container World! I have bean seen% s times and my hostname is% s.\ n'% (redis.get ('hits'), socket.gethostname ()) if _ _ name__ = = "_ _ main__": app.run (host= "0.0.0.0", port=5000, debug=True)
Create a new Dockerfile with the following contents:
FROM python:2.7LABEL maintainer= "vincent" COPY. / appWORKDIR / appRUN pip install flask redisEXPOSE 5000CMD ["python", "app.py"]
Create an image:
Docker build-t vincent/flask-redis.
Since we are going to use redis, but we are not installing redis in Dockerfile, we are just introducing a Python redis dependent library, how to use the redis service?
When we build a complex App, we need to split the components of the App into different containers to deploy. We deploy redis as a separate App, flask as a separate App, and flask to access redis. Let's first create a container for a redis.
Docker run-d-name redis redisdocker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1fb5745864cd redis "docker-entrypoint.s..." 23 seconds ago Up 22 seconds 6379/tcp redis
But the port of our current redis is 6379. However, when we created the container, we did not specify the-p 6379displacement 6379 parameter.
What are you asking? Because our redis here is not for external access, it is for our App internal access, so we do not need to expose 6379 to the outside (it is not safe).
In addition, our App.py does not know the IP address of redis, so the address is obtained as an environment variable (os.environ.get ('REDIS_HOST',' 127.0.0.1')).
We can access it by accessing the container name of redis using the link parameter.
Create a container for flask-redis:
Docker run-d-- link redis-- name flask-redis-e REDIS_HOST=redis vincent/flask-redis
-e REDIS_HOST means to set an environment variable REDIS_HOST=redis in the current container vincent/flask-redis
Let's go into the container and look at the env:
Docker exec-it flask-redis/ bin/bashroot@45977ae3cbed:/app# envREDIS_PORT_6379_TCP_PROTO=tcpREDIS_PORT=tcp://172.17.0.2:6379REDIS_NAME=/flask-redis/redisLANG=C.UTF-8HOSTNAME=45977ae3cbedREDIS_PORT_6379_TCP_ADDR=172.17.0.2REDIS_PORT_6379_TCP=tcp://172.17.0.2:6379GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FFPYTHONIOENCODING=UTF-8REDIS_ENV_REDIS_DOWNLOAD_URL= http://download.redis.io/releases/redis-5.0.5.tar.gzREDIS_HOST=redisPWD= / appHOME=/rootREDIS_PORT_6379_TCP_PORT=6379TERM=xtermREDIS_ENV_REDIS_DOWNLOAD_SHA=2139009799d21d8ff94fc40b7f36ac46699b9e1254086299f8d3b223ca54a375REDIS_ENV_GOSU_VERSION=1.10PYTHON_VERSION=2.7.16SHLVL=1PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binREDIS_ENV_REDIS_VERSION=5.0.5PYTHON_PIP_VERSION=19.1.1_=/usr/bin/env
You can see that there is an environment variable REDIS_HOST=redis
Ping through name:
Root@45977ae3cbed:/app# ping redisPING redis (172.17.0.2) 56 (84) bytes of data.64 bytes from redis (172.17.0.2): icmp_seq=1 ttl=64 time=1.16 ms64 bytes from redis (172.17.0.2): icmp_seq=2 ttl=64 time=0.105 ms64 bytes from redis (172.17.0.2): icmp_seq=3 ttl=64 time=0.215 ms
You can send a request within the container:
Root@e9361b832e36:/app# curl 127.0.0.1:5000Hello Container World! I have bean seen 3 times and my hostname is e9361b832e36.root@e9361b832e36:/app# curl 127.0.0.1:5000Hello Container World! I have bean seen 4 times and my hostname is e9361b832e36.root@e9361b832e36:/app# curl 127.0.0.1:5000Hello Container World! I have bean seen 5 times and my hostname is e9361b832e36.
But at this time, it is still inaccessible in the host.
We restart the flask-redis service and specify the port:
Docker run-d-p 5000 REDIS_HOST=redis vincent/flask-redisdocker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES835a7fb5a689 vincent/flask-redis 5000-- link redis-- name flask-redis-e REDIS_HOST=redis vincent/flask-redisdocker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES835a7fb5a689 vincent/flask-redis "python app.py" 2 minutes ago Up 4 seconds 0.0.0.05 000 -> 5000/tcp flask-redis1fb5745864cd redis "docker-entrypoint.s..." 15 hours ago Up 40 seconds 6379/tcp redis
Access from the host:
Curl 127.0.0.1:5000Hello Container World! I have bean seen 8 times and my hostname is 835a7fb5a689. Thank you for your reading, the above is the content of "how to expand the Python program in docker". After the study of this article, I believe you have a deeper understanding of how to expand the Python program in docker, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.