用python制作网页镜像文件
第一步:初始化操作,用可用镜像源:
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://hub.docker-alhk.dkdun.com/"]
}
EOF
systemctl daemon-reload
systemctl restart docker
第二步:制作镜像前操作
mkdir /mydocker
cd /mydocker
Dockerfile:指令解释详情看前一篇
FROM python:2.7-slim WORKDIR /app ADD . /app RUN pip install --trusted-host pypi.python.org -r requirements.txt EXPOSE 80 ENV NAME World ENV AUTHOR psl CMD ["python","app.py"]
app.py:
from flask import Flask from redis import Redis, RedisError import os import socket # Connect to Redis redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>cannot connect to Redis, counter disabled</i>" html = "<h3>Hello {name}!</h3>" \ "<b>Hostname:</b> {hostname}<br/>" \ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) if __name__ == "__main__": app.run(host='0.0.0.0', port=80)
requirements.txt:
第三步:制作镜像并基于该镜像启动容器
docker build -t myapp:1.0 制作镜像取名myapp:1.0
docker run -d -p 6379:6379 --name redis-1 redis 容器启用redis数据库(需要有redis镜像文件,因为初始化操作已经设置了可用源,拉取镜像直接docker pull redis就可以)
docker run -d -p 5081:80 --name myapp-2 --link redis-1:redis myapp:1.0(指定容器占用5081端口,连接redis-1容器内的redis数据库,给容器取名myapp-2)
去浏览器输入docker1主机ip地址:5081查看网页效果