最底层是container
,第二层是service
,最高层是stack
。
前提
- dockers version:1.13或者更高(17.03.1-ce)
用Dockerfile
定制container
dockerfile
定义了container
的环境里面的内容。在container
里面的资源(如网络接口和磁盘驱动)都是虚拟的,和物理机分离的,所以必须映射端口、指定要copy
的文件。
创建一个空目录,创建一个Dockerfile
文件。
# Use an official python runtime as a base 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 -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"]
需要在container中运行的App
在Dockerfile
的同级目录中添加两个文件:requirements.txt
和app.py
。
Dockerfile
中的ADD
命令将这两个文件存放在image
中,EXPOSE
命令可以让我们通过HTTP访问。
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)
在一个
container
中获取hostname
得到的是container ID
。
构建这个app
docker build -t friendlyhello .
运行app
docker run -p 4000:80 friendlyhello
docker run -d -p 4000:80 friendlyhello
docker ps
docker stop 1fa4ab2cf395
分享image
A registry is a collection of repositories, and a repository is a collection of images.
登陆docker id
docker login
给image
打标签
本地image和一个registry
中的一个repository
建立联系的标记为username/repository:tag
。
docker tag friendlyhello john/get-started:part1
发布image
docker push username/repository:tag
从远程仓库获取image
docker run -p 4000:80 username/repository:tag
在构建和运行image的时候,如果没有指定
:tag
,将会使用:latest
。运行的时候如果没有tag则运行:latest
的版本(不见得是最新的版本)。
docker build -t friendlyname . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyname # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyname # Same thing, but in detached mode
docker ps # See a list of all running containers
docker stop <hash> # Gracefully stop the specified container
docker ps -a # See a list of all containers, even the ones not running
docker kill <hash> # Force shutdown of the specified container
docker rm <hash> # Remove the specified container from this machine
docker rm $(docker ps -a -q) # Remove all containers from this machine
docker images -a # Show all images on this machine
docker rmi <imagename> # Remove the specified image from this machine
docker rmi $(docker images -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
网友评论