docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default
"C:\\Users\\admin\\.docker")
-c, --context string Name of the context to use to connect to the
daemon (overrides DOCKER_HOST env var and
default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal")
(default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"C:\\Users\\admin\\.docker\\ca.pem")
--tlscert string Path to TLS certificate file (default
"C:\\Users\\admin\\.docker\\cert.pem")
--tlskey string Path to TLS key file (default
"C:\\Users\\admin\\.docker\\key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Build with BuildKit (Docker Inc., v0.5.1-docker)
compose* Docker Compose (Docker Inc., 2.0.0-beta.1)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.6.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
Docker常用命令
运行镜像
$ docker run imagename echo "Hello World"
如果本地没有要要运行的镜像,Docker会到Docker Hub进行在线搜索,并下载。镜像下载后,Docker 会将它转成容器并运行。然后在容器中执行我们指定的命令——echo “Hello World”。
请求 Docker 提供一个容器中的 shell
$ docker run -i -t imagename /bin/bash
当中的 -i 和 -t参数表示我们想要一个附有 tty 的交互会话(interactive session),/bin/bash 参数表示你想获得一个 bash shell。当你退出 shell 时,容器就会停止——主进程运行多久,容器就运行多久。
获取关于容器的信息
$ docker inspect container_name
获取容器操作日志
$ docker logs container_name
查看当前容器有哪些文件改动过
$ docker diff container_name
删除指定容器
$ docker rm container_name
返回所有容器的id
$ docker ps -aq -f status=exited
清除所有已停止的容器
$ docker rm -v $(docker ps -aq -f status=exited)
为了避免已停止的容器的数量不断增加,可以在执行 docker run 的时候加上–rm 参数,它的作用是当容器退出时,容器和相关的文件系统会被一并删掉
通过Dockerfile创建镜像
假设我们希望基于另一个镜像来创建我们的容器的话,也必须从头开始。更重要的是,这样做的可重复性(repeatable)很差;创建镜像的步骤很难与他人分享,把步骤重做也不是件易事,而且容易出错。解决这些问题的方法就是利用 Dockerfile,使创建镜像的过程全部自动化。
$ mkdir cowsay
$ cd cowsay
$ ni Dockerfile
Dockerfile文件内容:
FROM debian
MAINTAINER John Smith <john@smith.com>
RUN apt-get update && apt-get install -y cowsay fortune
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
创建一个新文件 entrypoint.sh
#!/bin/bash
if [ $# -eq 0 ]; then
/usr/games/fortune | /usr/games/cowsay
else
/usr/games/cowsay "$@"
fi
$ docker build -t test/cowsay-dockerfile .
$ docker run test/cowsay-dockerfile Hello Moo
Docker 的镜像由多个不同的“层”(layer)组成,每一个层都是一个只读的文件系统。Dockerfile 里的每个指令都会创建一个新的层,而这个层将位于前一个层之上。当一个镜像被转化成一个容器时(譬如通过 docker run 或 docker create 命令),Docker 引擎会在镜像之上添加一个处于最上层的可读写文件系统(同时还会对一些配置进行初始化,如 IP 地址、名称、ID,以及资源使用限制等)。
由于不必要的层会使镜像变得臃肿(而且 AUFS 最多只能有 127 个层),你会发现很多 Dockerfile 都把多个 UNIX 命令放在同一个 RUN 指令中,以减少层的数量。
容器
容器可以处于以下几种状态之一:已创建(created)、重启中(restarting)、运行中(running)、已暂停(paused)和已退出(exited)。
“已创建”指容器已通过 dockercreate 命令初始化,但未曾启动。很多时候“已退出”也称为“已停止”,指容器中没有正在运行的进程(虽然“已创建”状态的容器也没有正在运行的进程,但“已退出”的容器至少启动过一次)。容器的主进程退出时,容器也会退出。“已退出”的容器可以用 docker start 命令重启。已停止的容器不等于一个镜像,因为前者还会保留对配置、元数据和文件系统的改动。“重启中”状态实际上很少遇见,当 Docker 引擎尝试重启一个启动失败的容器时,它才会出现。
使用寄存服务
我们运行的 Debian 镜像是从官方的 Docker 镜像寄存服务(registry),即 Docker Hub 下载的。同样,我们可以上传自己的镜像到 Docker Hub 供他人下载及使用。
镜像的储存以层级设计,并采用以下术语:
-
寄存服务(registry)
负责托管和发布镜像的服务,默认为 Docker Hub。
-
仓库(repository)
一组相关镜像(通常是一个应用或服务的不同版本)的集合。
-
标签(tag)
仓库中镜像的识别号,由英文和数字组成(如 14.04 或 stable)。
镜像的命名空间
服务器上的 Docker 镜像属于三种命名空间的其中之一,可以由镜像的名称判断它属于哪一种
-
镜像名称以字符串和 / 开头
如 amouat/revealjs,那么它属于“用户”命名空间(“user” namespace)。Docker Hub 上的这些镜像都上传自某个用户。
-
诸如 debian 或 ubuntu 的名称,不包含前缀或 /
属于“根”命名空间(“root” namespace)。它由 Docker 公司所控制,为一些常用的软件及发行版预留在 Docker Hub 上发布的官方镜像。
-
以主机名或 IP 开头的名称
代表该镜像来自第三方的寄存服务(并非 Docker Hub),包括公司或组织自己搭建的寄存服务