1. Docker 客户端
docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
dd@ubuntu04:~$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/home/dd/.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 "/home/dd/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/home/dd/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/dd/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
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 command --help 更深入的了解指定的 Docker 命令使用方法。
例如我们要查看 docker stats 指令的具体使用方法:
dd@ubuntu04:~$ docker stats --help
Usage: docker stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output
2. 容器使用
2.1 获取镜像
如果我们本地没有 ubuntu 镜像,我们可以使用 docker pull 命令来载入 ubuntu 镜像:
dd@ubuntu04:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
2.2 启动容器
以下命令使用 ubuntu 镜像启动一个容器,参数为以命令行模式进入该容器:
dd@ubuntu04:~$ docker run -it ubuntu /bin/bash
root@27f6750675e1:/#
参数说明:
- -i: 交互式操作。
- -t: 终端。
- ubuntu: ubuntu 镜像。
- /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
要退出终端,直接输入 exit:
root@27f6750675e1:/# exit
exit
dd@ubuntu04:~$
2.3 启动已停止运行的容器
查看所有的容器命令如下:
dd@ubuntu04:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27f6750675e1 ubuntu "/bin/bash" About a minute ago Exited (0) 42 seconds ago priceless_sinoussi
59ea255506bd ubuntu:18.04 "/bin/sh -c 'while t…" 13 hours ago Exited (137) 13 hours ago pensive_meninsky5
4ee4b474dd97 ubuntu:18.04 "/bin/bash" 13 hours ago Exited (0) 13 hours ago funny_jepsen
1255d4baa690 ubuntu:18.04 "/bin/echo 'Hello wo…" 13 hours ago Exited (0) 13 hours ago strange_euler
109636351a72 hello-world "/hello" 14 hours ago Exited (0) 14 hours ago modest_turing
831320a55d0c hello-world "/hello" 14 hours ago Exited (0) 14 hours ago epic_joliot
39ed0eb25cf5 hello-world "/hello" 14 hours ago Exited (0) 14 hours ago wizardly_sammet
使用 docker start 启动一个已停止的容器:
dd@ubuntu04:~$ docker start 27f6750675e1
27f6750675e1
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27f6750675e1 ubuntu "/bin/bash" 3 minutes ago Up 28 seconds priceless_sinoussi
2.4 后台运行
在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以过 -d 指定容器的运行模式。
dd@ubuntu04:~$ docker run -itd --name ubuntu-test ubuntu /bin/bash
12a20b95e6b3a44ec71286ee00fa1860246202c4439c0a00bb8e7d000307b646
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12a20b95e6b3 ubuntu "/bin/bash" 31 seconds ago Up 29 seconds ubuntu-test
27f6750675e1 ubuntu "/bin/bash" 4 minutes ago Up About a minute priceless_sinoussi
TIPS
加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec
2.5 停止一个容器
停止容器的命令如下:
docker stop <容器 ID>
dd@ubuntu04:~$ docker stop 12a20b95e6b3
12a20b95e6b3
dd@ubuntu04:~$ docker stop priceless_sinoussi
priceless_sinoussi
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2.6 重启容器
停止的容器可以通过 docker restart 重启:
docker restart <容器 ID>|<容器 names>
dd@ubuntu04:~$ docker restart priceless_sinoussi
priceless_sinoussi
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27f6750675e1 ubuntu "/bin/bash" 8 minutes ago Up 6 seconds priceless_sinoussi
2.7 进入容器
在使用 -d 参数时,容器启动后会进入后台。此时想要进入容器,可以通过以下指令进入:
docker attach
docker exec:推荐大家使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止。
2.7.1 attach 命令
下面演示了使用 docker attach 命令。
dd@ubuntu04:~$ docker attach 27f6750675e1
root@27f6750675e1:/# exit
exit
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
TIPS
注意: 如果从这个容器退出,会导致容器的停止。
2.7.2 exec 命令
下面演示了使用 docker exec 命令。
dd@ubuntu04:~$ docker exec -it 27f6750675e1 /bin/bash
root@27f6750675e1:/# exit
exit
dd@ubuntu04:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27f6750675e1 ubuntu "/bin/bash" 13 minutes ago Up 21 seconds priceless_sinoussi
dd@ubuntu04:~$
TIPS
注意: 如果从这个容器退出,不会导致容器的停止,这就是为什么推荐大家使用 docker exec 的原因。
更多参数说明请使用 docker exec --help 命令查看。
2.8 导出和导入容器
2.8.1 导出容器
如果要导出本地某个容器,可以使用 docker export 命令。
dd@ubuntu04:~$ docker export 27f6750675e1 > dd-ubuntu.tar
dd@ubuntu04:~$ ll
total 74504
drwxr-xr-x 5 dd dd 4096 May 7 00:50 ./
drwxr-xr-x 3 root root 4096 May 6 02:18 ../
-rw------- 1 dd dd 1181 May 6 09:49 .bash_history
-rw-r--r-- 1 dd dd 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 dd dd 3771 Apr 4 2018 .bashrc
drwx------ 2 dd dd 4096 May 6 02:23 .cache/
-rw-rw-r-- 1 dd docker 76237312 May 7 00:50 dd-ubuntu.tar
drwx------ 3 dd dd 4096 May 6 02:23 .gnupg/
-rw-r--r-- 1 dd dd 807 Apr 4 2018 .profile
drwx------ 2 dd dd 4096 May 6 09:52 .ssh/
-rw-r--r-- 1 dd dd 0 May 6 02:42 .sudo_as_admin_successful
-rw------- 1 root root 8996 May 6 09:44 .viminfo
-rw------- 1 dd dd 108 May 6 09:49 .Xauthority
这样将导出容器快照到本地文件。
TIPS
导出的时候容器是否在运行状态没有影响
2.8.2 导入容器快照
可以使用 docker import 从容器快照文件中再导入为镜像,以下实例将快照文件 dd-ubuntu.tar 导入到镜像 test/ubuntu:v1
dd@ubuntu04:~$ cat dd-ubuntu.tar | docker import - dd/ubuntu:v1
sha256:63cf4e1dfe435578d0bc4f6953072d0adf38715ab93f120bd0b7a7e979d7b602
dd@ubuntu04:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dd/ubuntu v1 63cf4e1dfe43 14 seconds ago 73.9MB
ubuntu latest 1d622ef86b13 13 days ago 73.9MB
ubuntu 18.04 c3c304cb4f22 13 days ago 64.2MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
此外,也可以通过指定 URL 或者某个目录来导入
docker import <url>
2.9 删除容器
删除容器使用 docker rm 命令:
dd@ubuntu04:~$ docker rm -f 12a20b95e6b3
dd@ubuntu04:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12a20b95e6b3 ubuntu "/bin/bash" 25 minutes ago Exited (0) 22 minutes ago ubuntu-test
27f6750675e1 ubuntu "/bin/bash" 29 minutes ago Exited (0) 10 minutes ago priceless_sinoussi
59ea255506bd ubuntu:18.04 "/bin/sh -c 'while t…" 13 hours ago Exited (137) 13 hours ago pensive_meninsky5
4ee4b474dd97 ubuntu:18.04 "/bin/bash" 14 hours ago Exited (0) 14 hours ago funny_jepsen
1255d4baa690 ubuntu:18.04 "/bin/echo 'Hello wo…" 14 hours ago Exited (0) 14 hours ago strange_euler
109636351a72 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago modest_turing
831320a55d0c hello-world "/hello" 15 hours ago Exited (0) 15 hours ago epic_joliot
39ed0eb25cf5 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago wizardly_sammet
dd@ubuntu04:~$ docker rm -f 12a20b95e6b3
12a20b95e6b3
dd@ubuntu04:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27f6750675e1 ubuntu "/bin/bash" 29 minutes ago Exited (0) 10 minutes ago priceless_sinoussi
59ea255506bd ubuntu:18.04 "/bin/sh -c 'while t…" 13 hours ago Exited (137) 13 hours ago pensive_meninsky5
4ee4b474dd97 ubuntu:18.04 "/bin/bash" 14 hours ago Exited (0) 14 hours ago funny_jepsen
1255d4baa690 ubuntu:18.04 "/bin/echo 'Hello wo…" 14 hours ago Exited (0) 14 hours ago strange_euler
109636351a72 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago modest_turing
831320a55d0c hello-world "/hello" 15 hours ago Exited (0) 15 hours ago epic_joliot
39ed0eb25cf5 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago wizardly_sammet
dd@ubuntu04:~$
3. 参考文献
[1] Docker 容器使用