由于现在禁止访问docker官方源,我们可以添加国内公共的镜像仓库
mkdir -p /etc/docker/daemon.json
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://hub.docker-alhk.dkdun.com/"]
}
重新加载daemon,并重启docker服务
systemctl daemon-reload
systemctl restart docker
准备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 cali
CMD ["python","app.py"]
编辑requirements.txt文件
vim requirements.txt
Flask
Redis
编辑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)
制作镜像
docker build -t myapp:1.0 .
启动redis容器
docker run -d -p 6379:6379 --name redis-1 redis
启动myapp镜像的容器,链接到redis容器
docker run -d --name myapp-2 -p 5081:80 --link redis-1:redis myapp:1.0
访问容器的web服务