DockerLession02 使用 image 和 container

写在前面

今天是 2016 年第一天, 新年新气象, 三天小长假写一套 Docker 入门, 纪念过去的 2015.

Docker 的管理命令

执行 docker –help 我们可以看到所有的命令

$ docker --help
Usage: docker [OPTIONS] COMMAND [arg...]
       docker daemon [ --help | ... ]
       docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:

  -b, --bridge=                                                        Attach containers to a virtual switch
  --config=%USERPROFILE%\.docker                                       Location of client config files
  -D, --debug=false                                                    Enable debug mode
  --disable-legacy-registry=false                                      Do not contact legacy registries
  -H, --host=[]                                                        Daemon socket(s) to connect to
  -h, --help=false                                                     Print usage
  -l, --log-level=info                                                 Set the logging level
  --tls=false                                                          Use TLS; implied by --tlsverify
  --tlscacert=%USERPROFILE%\.docker\machine\machines\default\ca.pem    Trust certs signed only by this CA
  --tlscert=%USERPROFILE%\.docker\machine\machines\default\cert.pem    Path to TLS certificate file
  --tlskey=%USERPROFILE%\.docker\machine\machines\default\key.pem      Path to TLS key file
  --tlsverify=true                                                     Use TLS and verify the remote
  -v, --version=false                                                  Print version information and quit

Commands:
    attach    Attach 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 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 a container or image
   +kill      Kill a running container
    load      Load an image from a tar archive or STDIN
    login     Register or log in to a Docker registry
    logout    Log out from a Docker registry
   +logs      Fetch the logs of a container
    network   Manage Docker networks
   +pause     Pause all processes within a container
    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 a container
   +rm        Remove one or more containers
   -rmi       Remove one or more images
   +run       Run a command in a new container
    save      Save an image(s) to a tar archive
    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 a running container
    tag       Tag an image into a repository
   +top       Display the running processes of a container
   +unpause   Unpause all processes within a container
   ~version   Show the Docker version information
    volume    Manage Docker volumes
    wait      Block until a container stops, then print its exit code

Run 'docker COMMAND --help' for more information on a command.

上面标注 ~表示是系统级别的语句, +是 container 级别的语句, -是 images 级别的语句.

我们可以看到里面包含了上一节我们使用过的 docker run hello-world, 意思是启动 hello-world 这个 container 容器.

docker version 查看当前 docker 版本

$ docker version

 Version:      1.9.1
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   a34a1d5
 Built:        Fri Nov 20 17:56:04 UTC 2015
 OS/Arch:      windows/amd64

Server:
 Version:      1.9.1
 API version:  1.21
 Go version:   go1.4.3
 Git commit:   a34a1d5
 Built:        Fri Nov 20 17:56:04 UTC 2015
 OS/Arch:      linux/amd64

docker pull 下载一个镜像

docker pull centos

docker images 查看当前已有镜像

docker images

REPOSITORY  TAG     IMAGE ID       CREATED      VIRTUAL SIZE
redis       latest  ff3dae0ffe3b   2 days ago   151.3 MB
centos      latest  60e65a8e4030   7 days ago   196.6 MB
php         latest  6559f4d19365   10 days ago  484.8 MB

docker info 查看当前 docker 整体情况

docker info

Images: 176
Server Version: 1.9.1
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 187
 Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.1.13-boot2docker
Operating System: Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20 19:33:59 UTC 2015
CPUs: 1
Total Memory: 996.2 MiB
Name: default
ID: OFEO:NBBK:3XTH:I5DH:3Q6N:R6LS:SLLL:I7NY:JCBT:F6FO:ZT7E:Q5L6
Debug mode (server): true
 File Descriptors: 17
 Goroutines: 29
 System Time: 2016-01-01T12:29:12.818305979Z
 EventsListeners: 0
 Init SHA1:
 Init Path: /usr/local/bin/docker
 Docker Root Dir: /mnt/sda1/var/lib/docker
Labels:
 provider=virtualbox

docker rmi 删除镜像

docker rmi ff3da (写镜像ID前几位就可以, 删除了redis镜像)

docker run 启动 container

启动一个 container, 执行一个命令

docker run centos /bin/echo "hello docker"

docker run 启动一个 container, 用于 ssh 交互

docker run -i -t cnetos /bin/bash

-i 表示可以交互,
-t 表示 tty

可以在新开创的 container 中执行一些 shell 命令

ls -la && cd / && ls -la
exit

docker run 启动一个后台 container, 用于服务

docker run -d centos /bin/sh -c "while true; do echo hello docker; sleep 5; done"

-d 表示后台运行,
此时打印返回一个很长的字符串, 就退出了.

docker ps 查看当前活动的 container

CONTAINER ID    IMAGE    COMMAND                  CREATED         STATUS        PORTS    NAMES
52b51839a47c    centos   "/bin/sh -c 'while tr"   2 minutes ago   Up 4 minutes           sad_noyce

1.CONTAINER ID, 刚刚 docker run -d 返回的字符串的前 12 位.
1.IMAGE, 表示当前 container 是基于哪一个镜像创建的
1.COMMAND, 表示创建时执行的语句
1.CREATED, 表示创建时长
1.STATUS, 表示 container 状态
1.PORTS, 表示 container 内部与外部端口的映射
1.NAMES, 是 container 的名字

docker logs 查看日志

docker logs 52b51839a47c

docker stop 停止服务

docker stop 52b51839a47c (-t 10)

docker kill 停止服务

docker kill 52b51839a47c

docker kill 与 docker stop 区别

docker stop 会向容器发送一个 SIGTERM 信号,然后过一段时间再发送 SIGKILL 信号。
我们知道,处理良好的程序可以捕捉 SIGTERM 信号,并进行清理动作然后退出。但是也可能可以忽略该信号。
但是经过-t(默认 10S)时间后,会再发送 SIGKILL 信号触发进程的最终的退出。

docker kill 会向容器发送一个信号(SIGKILL 或者其他信号)。

相比来说,docker stop 可以更优雅的关闭容器,容器里的进程可以很好的退出。
docker kill 相当于快速地强制关闭容器。但是关闭容器并非是 docker kill 的唯一功能,向容器发送信号也是很有用的功能。

docker start 开启停止的 container

docker start 52b51839a47c

docker restart 重启 container (start 状态的重启, stop 状态的启动)

docker restart 52b51839a47c

docker stats 查看 container 状态

docker stats 52b51839a47c

CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O
52b51839a47c        0.11%               397.3 kB / 1.045 GB   0.04%               648 B / 648 B       0 B / 0 B

docker rm 删除 container

docker rm 52b51839a47c

docker exec 在 container 中执行命令

docker exec 52b51839a47c /bin/sh -c "echo hello yo!"
docker exec -i -t 52b51839a47c /bin/bash

docker inspect 查看 container 或 images 的底层

docker inspect ImageID
docker inspect ContainerID

docker top 查看运行的 container 中的进程

docker top 52b51839a47c
        PID    PPID   C   STIME   TTY   TIME       CMD
root    8031   1172   0   12:11   ?     00:00:00   /bin/sh -c while true; do echo hello docker; sleep 1; done
root    9903   8031   0   12:35   ?     00:00:00   sleep 1

docker pause 暂停 container

docker pause 52b51839a47c

此时再使用 container 执行命令(run), 获取 container 服务(redis 等内置服务)均报错

不同于 stop 的是 docker logs, top 命令照常

docker unpause 重启启用暂停的 container

docker unpause 52b51839a47c

一切恢复正常

docker rename 重命名一个 container

docker rename 52b51839a47c ono

本文涉及到的知识点

docker

我们用的命令工具

container

容器, 相当于启动一个系统的虚拟机

image

镜像, 相当于ghost系统的ghost镜像

application

在启动的container中, 执行的沙盒程序
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.