概述:通过Docker构建、运行和发布镜像
(一)新的开发环境:
在过去的应用开发方式中,我们要同时配置开发环境和生产环境。而使用Docker,我们只需要获得一个运行环境的镜像,应用的代码、依赖、运行环境可以同时迁移。这种便携式的镜像定义在Dockerfile中。
(二)通过Dockerfile定义容器:
概述:
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"]
APP其他部分:
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)
我们不需要Python或者其他任何在requirement.txt中所需要的东西——不论是构建还是运行,我们并没有真正准备一个包含python、Flask的运行环境,但是我们确实“拥有”它
(三)构建应用:
docker build -t friendlyhello .
docker image ls
(四)运行应用:
docker run -p 4000:80 friendlyhello
如果使用Docker ToolBox,请使用Docker Machine IP去替代localhost,例如:http://192.168.99.100:4000/
docker-machine ip
退出:CTRL+C;Windows中无法这样退出,需要:
docker container ls
docker container stop <Container NAME or ID>
后台运行应用:-d
docker run -d -p 4000:80 friendlyhello
(五)分享应用:
为了证明我们应用的便携性,让我们上传镜像,并且在其他地方运行。
“注册”(registry)是仓库的集合,“仓库”(repository)是镜像的集合,类似于GitHub中的repository,不过代码是已经构建完成的。一个注册的账号可以创建很多仓库,docker命令行工具(CLI)默认使用Docker的公开注册(public registry)。
1)登录Docker账号(public registry)
docker login
2)标记镜像:
用来联系本地镜像和注册中的标记是 username/repository:tag,可选,但是建议使用。registry用来给dorker镜像一个版本号,建议给仓库和标记相对于内容语义化的命名
docker tag image username/repository:tag
docker tag friendlyhello john/get-started:part2
3)发布镜像:
docker push username/repository:tag
4)从远程仓库拉取和运行镜像
docker run -p 4000:80 username/repository:tag
无论在哪里执行run命令,代码都与一个干净的小package一同传递,没必要为了使Docker运行它而安装任何东西
(六)备忘单:
docker build -t friendlyhello . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry