Docker multi-container, deployment of complex applications!

Start two containers this time

One container is used to run redis. Another flask web application running python. 
In this web app, visit redis.

 
 

[miller@docker4 py-flask-redis]$ ls
app.py Dockerfile

[miller@docker4 py-flask-redis]$ cat app.py
from flask import Flask
import redis
import os

host = os.environ.get("REDIS_HOST", "redis")

redis_conn = redis.Redis(host=host, port="6379")

app = Flask(__name__)

@ app.route ("/")
def web_test ():
    res = redis_conn.incr ("range_key")
    return "This is my% s call, incr method."% res

 
 

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)


[miller@docker4 py-flask-redis]$ cat Dockerfile
FROM python:3.7.7
LABEL maintainer="[email protected]"
COPY app.py /app/
RUN pip install flask redis
WORKDIR /app
EXPOSE 5000  
CMD ["python3", "app.py"]

Then do docker build 

[miller@docker4 py-flask-redis]$  docker build -t caijiwandocker/flask-redis

[miller@docker4 py-flask-redis]$ docker images
 REPOSITORY                  TAG         IMAGE ID       CREATED         SIZE
 caijiwandocker/flask-redis  latest      557535394ca7   8 minutes ago   929MB
 python                      3.7.7       859d8ec7db6a   10 days ago     919MB
 redis                       latest      4cdbec704e47   11 days ago     98.2MB

After the build is complete, execute docker run

Start the redis container first, 
[miller @ docker4 py-flask-redis] $ docker run -d --name = redis redis
Then start the flask application just built. 
[miller @ docker4 py-flask-redis] $ docker run -d --link = redis -p 5000: 5000 -e REDOS_HOST = reids caijiwandocker / flask-redis

explain:

--link = redis link to redis. This redis is just a name, used instead of the ip address of the redis container. (Because the IP address is changeable. But as long as the name remains unchanged, you can access it)
-p 5000: 5000 Map the 5000 port of the host to this flask container, and throw it on the 5000 port.
-e REDOS_HOST = reids add an environment variable for this flask container. (os.environ.get ("REDOS_HOST") in app.py) is actually the environment variable obtained.

In this way, if one day, the redis container needs to be renamed to redis-v1, then it can be done when running the flask container:

[miller@docker4 py-flask-redis]$ docker run -d --link=redis-v1 -p 5000:5000 -e REDOS_HOST=reids-v1 caijiwandocker/flask-redis

You don't need to pay attention to it, what is the ip address. It is very convenient.

 

##################################################################################################

In this example the redis server. Only internal apps are accessed. And because of the existence of docker bridge, containers and containers can communicate with each other to access.
So redis server, there is no need to do mapping. This will allow redis to be accessed from the outside. Not safe.

Just need to be a flask container. Make a mapping so that the flask can be accessed from the outside.

 

Guess you like

Origin www.cnblogs.com/chengege/p/12681273.html