咱們以前講過部署一個簡單的Python程序。python
這一節咱們將這個Python程序進行一個擴展,鏈接redis數據庫,並對redis進行一些操做,redis
新建App.py,內容以下:docker
from flask import Flask from redis import Redis import os import socket app = 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)
新建Dockerfile,內容以下: 數據庫
FROM python:2.7 LABEL maintainer="vincent" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 5000 CMD [ "python", "app.py" ]
建立image:flask
docker build -t vincent/flask-redis .
咱們既然要使用redis,可是咱們並無在Dockerfile中安裝redis,只是在引入了一個Python redis依賴庫,如何使用redis服務?安全
當咱們build一個複雜的App的時候,咱們須要把App的組件分紅不一樣的容器去部署。咱們把redis做爲一個單獨的App進行部署,flask做爲一個單獨的App進行部署,flask要去訪問redis。咱們先建立一個redis的一個容器。bash
docker run -d --name redis redis
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fb5745864cd redis "docker-entrypoint.s…" 23 seconds ago Up 22 seconds 6379/tcp redis
可是咱們如今的redis的端口是6379。可是咱們在建立容器的時候,並無指定-p 6379:6379 參數。 服務器
問什麼呢?由於咱們這裏的redis不是提供給外面的人訪問的,他是供咱們App內部訪問的,所以咱們不必把6379暴露到外面去(這樣不安全)。架構
此外咱們的App.py並不知道redis的IP地址,所以把地址用環境變量的方式獲取(os.environ.get('REDIS_HOST', '127.0.0.1'))。app
咱們可使用link參數,經過訪問redis的容器名字來訪問。
建立flask-redis的container:
docker run -d --link redis --name flask-redis -e REDIS_HOST=redis vincent/flask-redis
-e REDIS_HOST 表示在當前容器vincent/flask-redis中設置一個環境變量REDIS_HOST=redis
咱們進入到容器中查看env:
docker exec -it flask-redis /bin/bash root@45977ae3cbed:/app# env REDIS_PORT_6379_TCP_PROTO=tcp REDIS_PORT=tcp://172.17.0.2:6379 REDIS_NAME=/flask-redis/redis LANG=C.UTF-8 HOSTNAME=45977ae3cbed REDIS_PORT_6379_TCP_ADDR=172.17.0.2 REDIS_PORT_6379_TCP=tcp://172.17.0.2:6379 GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF PYTHONIOENCODING=UTF-8 REDIS_ENV_REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.5.tar.gz REDIS_HOST=redis PWD=/app HOME=/root REDIS_PORT_6379_TCP_PORT=6379 TERM=xterm REDIS_ENV_REDIS_DOWNLOAD_SHA=2139009799d21d8ff94fc40b7f36ac46699b9e1254086299f8d3b223ca54a375 REDIS_ENV_GOSU_VERSION=1.10 PYTHON_VERSION=2.7.16 SHLVL=1 PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin REDIS_ENV_REDIS_VERSION=5.0.5 PYTHON_PIP_VERSION=19.1.1 _=/usr/bin/env
能夠看到裏面有一個環境變量REDIS_HOST=redis
經過name來ping通:
root@45977ae3cbed:/app# ping redis PING 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 ms 64 bytes from redis (172.17.0.2): icmp_seq=2 ttl=64 time=0.105 ms 64 bytes from redis (172.17.0.2): icmp_seq=3 ttl=64 time=0.215 ms
能夠在容器內發送請求:
root@e9361b832e36:/app# curl 127.0.0.1:5000 Hello Container World! I have bean seen 3 times and my hostname is e9361b832e36. root@e9361b832e36:/app# curl 127.0.0.1:5000 Hello Container World! I have bean seen 4 times and my hostname is e9361b832e36. root@e9361b832e36:/app# curl 127.0.0.1:5000 Hello Container World! I have bean seen 5 times and my hostname is e9361b832e36.
可是這時候在宿主機中仍是沒法訪問。
咱們從新啓動flask-redis服務,並指定端口:
docker run -d -p 5000:5000 --link redis --name flask-redis -e REDIS_HOST=redis vincent/flask-redis
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 835a7fb5a689 vincent/flask-redis "python app.py" 2 minutes ago Up 4 seconds 0.0.0.0:5000->5000/tcp flask-redis 1fb5745864cd redis "docker-entrypoint.s…" 15 hours ago Up 40 seconds 6379/tcp redis
在宿主機中訪問:
curl 127.0.0.1:5000 Hello Container World! I have bean seen 8 times and my hostname is 835a7fb5a689.
本節的架構圖以下所示:
目前咱們的兩個container是在同一臺機器上的,經過docker0能夠相互通訊。那麼若是有兩臺Linux服務器,分別部署一個docker,那麼如何通訊呢?
咱們將在後面介紹