在空目录创建Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
创建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)
构建app
docker build -t friendlyhello .
运行app
docker run -p 4000:80 friendlyhello
代理服务器
当应用隐藏在proxy server之后时,可以设置代理服务器的主机与端口
ENV http_proxy host:port
ENV https_proxy host:port
DNS配置
如果修改DNS配置,可以修改/etc/docker/daemon.json
{
"dns": ["your_dns_address", "8.8.8.8"]
}
共享镜像
登录cloud.docker.com
docker login
标记镜像
docker tag friendlyhello gordon/get-started:part2
推送镜像
docker push username/repository:tag
【参考】
1。docker-ce:快速入门:https://docs.docker.com/get-started/part2/\#conclusion-of-part-two