Docker 客户端

转载链接

docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。

lusifer@UbuntuBase:~$ docker

Usage:    docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/home/lusifer/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -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/lusifer/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/lusifer/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/lusifer/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  config      Manage Docker configs
  container   Manage containers
  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 (experimental)
  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

可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法。

例如我们要查看 docker stats 指令的具体使用方法:

lusifer@UbuntuBase:~$ 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
      --help            Print usage
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

运行 WEB 容器


前面我们运行的容器并没有一些什么特别的用处。

接下来让我们尝试使用 docker 构建一个 web 应用程序。

我们将在 docker 容器中运行一个 Python Flask 应用来运行一个web应用。

lusifer@UbuntuBase:~$ docker run -d -P training/webapp python app.py
d89a785788469da23529eebf70f1764611bb28818a8de7fa1ea85154327b807a

参数说明:

  • -d:让容器在后台运行
  • -P:将容器内部使用的网络端口映射到我们使用的主机上

查看 WEB 容器


使用 docker ps 来查看我们正在运行的容器

lusifer@UbuntuBase:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
d89a78578846        training/webapp     "python app.py"     2 minutes ago       Up 2 minutes        0.0.0.0:32771->5000/tcp   laughing_cori

这里多了端口信息

PORTS
0.0.0.0:32771->5000/tcp

Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32771 上。

这时我们可以通过浏览器访问WEB应用

我们也可以指定 -p 标识来绑定指定端口

lusifer@UbuntuBase:~$ docker run -d -p 5000:5000 training/webapp python app.py
9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb
lusifer@UbuntuBase:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
9490b017fdf6        training/webapp     "python app.py"     3 seconds ago       Up 2 seconds        0.0.0.0:5000->5000/tcp   amazing_archimedes


查看 WEB 应用日志


docker logs [ID或者名字] 可以查看容器内部的标准输出:

lusifer@UbuntuBase:~$ docker logs -f amazing_archimedes 
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.75.1 - - [03/Nov/2017 06:14:05] "GET / HTTP/1.1" 200 -
192.168.75.1 - - [03/Nov/2017 06:14:05] "GET /favicon.ico HTTP/1.1" 404 -
192.168.75.1 - - [03/Nov/2017 06:16:57] "GET / HTTP/1.1" 200 -
192.168.75.1 - - [03/Nov/2017 06:16:57] "GET /favicon.ico HTTP/1.1" 404 -

参数说明:

  • -f:让 dokcer logs 像使用 tail -f 一样来输出容器内部的标准输出

从上面,我们可以看到应用程序使用的是 5000 端口并且能够查看到应用程序的访问日志。

查看 WEB 应用容器的进程


我们还可以使用 docker top 来查看容器内部运行的进程

lusifer@UbuntuBase:~$ docker top amazing_archimedes 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                71917               71897               0                   14:13               ?                   00:00:00            python app.py

检查 WEB 应用程序


使用 docker inspect 来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息:

lusifer@UbuntuBase:~$ docker inspect amazing_archimedes 
[
    {
        "Id": "9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb",
        "Created": "2017-11-03T06:13:13.053498761Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 71917,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-11-03T06:13:13.374235386Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
        "ResolvConfPath": "/var/lib/docker/containers/9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb/hostname",
        "HostsPath": "/var/lib/docker/containers/9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb/hosts",
        "LogPath": "/var/lib/docker/containers/9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb/9490b017fdf6b8c6415d5d01b925413c6ed2dd782566d251ad2bdad90263c3cb-json.log",
        "Name": "/amazing_archimedes",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5000"
                    }
                ]
            },
...

重启 WEB 应用容器


停止WEB应用容器

docker stop amazing_archimedes

已经停止的容器,我们可以使用命令 docker start 来启动

docker start amazing_archimedes

重启WEB应用容器

docker restart amazing_archimedes

查询全部容器

docker ps -a

查询最后一次创建的容器

docker ps -l



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐