Foreplay
We start a nginx mysql and containers, nginx mysql containers often need to go read the data. ip between the two containers, they can communicate, we only need to configure it in a container address nginx mysql on it. When one day, we hung up mysql, mysql restart a container, inside the container to nginx configuration click on it. In fact, nginx and mysql are clustered deployment. When there is a mysql hung up after, we need one by one into the interior of the container nginx be modified. The docker also for us to consider this situation
Link between Docker containers based on one-way communication
What is one-way communication it? Take the example above, it is nginx can ping mysql, mysql but can not ping nginx
Start mysql container
docker run --name mydb -d -e MYSQL_ROOT_PASSWORD=zou123456 mysql:5.7
Start nginx application container and link to the mysql database:
docker run -itd --name mynginx --link mydb nginx
Wherein the link indicates --link mydb mysql database, and so can nginx mysql a one-way communication
Note: mydb This container must exist!
Nginx into the interior of the container to ping mydb
If you are pulling the nginx mirror, what you need to install plug-ping, inside the container nginx install oh.
apt-get update && apt-get install iputils-ping
Mysql into the container, is not ping mynginx image. But that can ping the ip
Docker bidirectional communication between the container bridge using brige
We use the link above to achieve a one-way communication, and one-way communication often can not meet all our needs, you can use two-way communication bridge
What network model has to use to view the next docker network ls
This is a docker three default network mode. Create a new bridge called my_bridge
docker network create -d bridge my_bridge
-d bridge my_bridge represents a bridge to create a bridge called my_bridge
Start two containers, mysql and nginx
docker run -d --name mydb mysql:5.7
docker run -d --name mynginx nginx
The two containers mydb mynginx added and the bridge my_bridge
docker network connect my_bridge mydb
docker network connect my_bridge mynginx
Into the two containers are verified, ping mynginx mydb and ping is the ping
Application deployment flask
Now we have a flask applications need to be deployed to the server, the following code flask
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 been 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)
The flask redis applications also need to use, so we need two containers, a container is redis, there is a python container.
First and redis mirrored pull the python3.7
docker pull redis
docker pull python:3.7
Start redis container
docker run -d --name myredis redis
redis only for their own internal access is not available to external access, there is no need to expose the port -p parameter
In creating the python dockerfile, reads as follows
FROM python:3.7 LABEL maintaner="zouzou" COPY app.py /app/ WORKDIR /app RUN pip install flask redis EXPOSE 5000 CMD ["python","app.py"]
So that our flask Dockerfile code and are ready to build python image
docker build -t flask-python:v1 .
Start python image
docker run -d --name myflask -p 8002:5000 --link myredis -e REDIS_HOST=myredis flask-python:v1
Wherein -e REDIS_HOST = myredis set the environment variable, REDIS_HOST variable value myredis, so that the code can be acquired flask ip address according to the myredis
Browser access port 8002, as follows