文章

《Docker实战(第2版)》读书笔记

《Docker实战(第2版)》(Docker in Action(2rd Edition))读书笔记

《Docker实战(第2版)》读书笔记

获取 Docker 帮助

1
2
docker help <command>
docker command --help

docker container

optionsappliction
attachAttach local standard input, output, and error streams to a running container
commitCreate a new image from a container’s changes
cpCopy files/folders between a container and the local filesystem
createCreate a new container
diffInspect changes to files or directories on a container’s filesystem
execExecute a command in a running container
exportExport a container’s filesystem as a tar archive
inspectDisplay detailed information on one or more containers
killKill one or more running containers
logsFetch the logs of a container
lsList containers
pausePause all processes within one or more containers
portList port mappings or a specific mapping for the container
pruneRemove all stopped containers
renameRename a container
restartRestart one or more containers
rmRemove one or more containers
runCreate and run a new container from an image
startStart one or more stopped containers
statsDisplay a live stream of container(s) resource usage statistics
stopStop one or more running containers
topDisplay the running processes of a container
unpauseUnpause all processes within one or more containers
updateUpdate configuration of one or more containers
waitBlock until one or more containers stop, then print their exit codes

docker create

optionsapplictionmemo or demo
--namecontainer 名称 
--detach -d后台启动 container 
--interactive -i交互式启动 containerCtrl+P + Ctrl+Q 分离
--tty创建虚拟终端可将 --interactive -- tty 写作 -it
--rm在容器停止后自动删除 
--read-only只读挂载外部文件系统或卷 
--tmpfs为容器提供常驻内存的临时文件系统--tmpfs /tmp \
--env -e注入环境变量-e web_root=/path/to/root
--restart容器的重启策略--restart always
--pid容器内的进程 pid 与宿主的关系--pid host 可使容器与宿主共享进程
--link  
--entrypoin入口点(P46) 
--cidfile --cidfile /path/to/cid/file

docker run

docker create 参数相同

docker logs

optionsapplictionmemo or demo
--follow -f显示日志并同时监视日志变化 
--tail -n显示日志的条目数 

docker exec

optionsapplictionmemo or demo
--detach -d后台启动 container 
--env -e注入环境变量-e web_root=/path/to/root
--interactive -i交互式启动 containerCtrl+P + Ctrl+Q 分离
--tty创建虚拟终端可将 --interactive -- tty 写作 -it
--user -u运行程序的用户名格式为<name\|uid>[:<group\|gid>]
--workdir -w工作目录 

docker ps

docker ps 可列出容器,默认只列出运行中的容器。

optionsappliction
--all -a显示所有容器(包括已停止)
--no-trunc -e显示完整 container id
--size -s显示容器大小

docker start

optionsappliction
--attach -a附着
--interactive -i交互式启动 container

docker restart

optionsappliction
--signal -s发送的信号
--timeout -t超时时间

docker stop

参数与 docker restart 相同

docker rename

docker rename <old_container_name> <new_container_name>

docker kill

docker kill <CONTAINER> 强制删除容器

docker rm

optionsappliction
--force -f强制删除
--volume -v删除卷

docker image

optionsappliction
buildBuild an image from a Dockerfile
historyShow the history of an image
importImport the contents from a tarball to create a filesystem image
inspectDisplay detailed information on one or more images
loadLoad an image from a tar archive or STDIN
lsList images
pruneRemove unused images
pullDownload an image from a registry
pushUpload an image to a registry
rmRemove one or more images
saveSave one or more images to a tar archive (streamed to STDOUT by default)
tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE

docker pull

1
docker pull quay.io/dockerinaction/ch3_hello_registry:latest

docker rmi

docker rmi <image_name> 删除镜像

docker volume

optionsappliction
createCreate a volume
inspectDisplay detailed information on one or more volumes
lsList volumes
pruneRemove unused local volumes
rmRemove one or more volumes

docker network

optionsappliction
connectConnect a container to a network
createCreate a network
disconnectDisconnect a container from a network
inspectDisplay detailed information on one or more networks
lsList networks
pruneRemove all unused networks
rmRemove one or more networks

探索桥接网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 建立桥接网络
docker network create \
  --driver bridge \
  --label projection=dockerinaction \
  --label chapter=5 \
  --attachable \
  --scope local \
  --subnet 10.0.42.0/24 \
  --ip-range 10.0.42.128/25 \
  user-network

# 运行加入该网络的容器
docker run --it --name tom \
  --network user-network \
  --alpine:latest sh

# 容器内运行,查看网络地址
ip -f inet -4 -o addr

Ctrl+PCtrl+Q 分离终端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 建立第二个桥接网络
docker network create \
  --driver bridge \
  --label projection=dockerinaction \
  --label chapter=5 \
  --attachable \
  --scope local \
  --subnet 10.0.43.0/24 \
  --ip-range 10.0.43.128/25 \
  user-network2

# 将容器连接到网络
docker network connect user-network2 tom

# 将终端再次挂接到容器
docker attach tom

使用 nmap 查看网络

1
2
apk update && apk add nmap
nmap -sn 10.0.42.* -sn 10.0.43.* -oG /dev/stdout | grep Status

host 网络

1
docker run --rm --network host alpine:latest ip -o addr

none 网络

1
docker run --rm --network none alpine:latest ip -o addr

存储和卷

绑定挂载

1
--mount type=bind,src=$(SRC),dst=${dst},readonly=true

常驻内存

1
--mount type=tmpfs,dst=${dst},tmpfs-size=16k,tmpfs-mode=1770

1
2
docker volume create --driver local --label example=location location-example
docker volume inspect --format "" location-example

Dockfile

从 container 建立 image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 列出 container 的修改情况
docker container diff <CONTAINER>

# 提交镜像
docker container commit -a '@author' \
  -m 'memo info' \
  --entry-point <APP> \
  --env ENV1=VAL1 \
  <CONTAINER> <IMAGE>

# 为镜像重新打标签
docker image tag <OLD> <NEW>

# 列出 image 的历史
docker image history <IMAGE>

dockerfile 语法

1
2
3
4
5
6
7
8
FROM alpine:latest
LABEL maintainer="admin@google.com"
ENV APPROOT="~" VERSION="0.1"
LABEL base.name="dockerfile demo" base.version="${VERSION}"
WORKDIR $APPROOT
RUN apk update && apk add git
ENTRYPOINT ["git"]
EXPOSE 4321
1
docker image build -t kode4fun/alpine-git:0.1 -f demo.df .

文件系统指令

commandmemo
COPY [FILE1,FILE2,...,TARGET] 
VOLUME [VOL1,VOL2] 
CMD [ARG1,ARG2]ENTRYPOINT 的参数

dockerignore 文件

本文由作者按照 CC BY 4.0 进行授权