docker 客户端的命令十分简单,我们输入 docker就可以查看所有可用的命令:

ig:~ itguang$ docker

Usage:  docker COMMAND

A self-sufficient runtime for containers

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

Management Commands:
  checkpoint  Manage checkpoints
  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
  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
  deploy      Deploy a new stack or update an existing stack
  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.
ig:~ itguang$

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

例如我们要查看docker image命令的使用方法:可以使用 docker image --help命令查看它的用法

ig:~ itguang$ docker image --help

Usage:  docker image COMMAND

Manage images

Options:


Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

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

镜像篇

我们先使用 docker images --help 查看下有哪些命令可以使用

ig:webapp itguang$ docker image --help

Usage:  docker image COMMAND

Manage images

Options:


Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

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

下面我们来学习:

1、管理和使用本地 Docker 主机镜像
2、创建镜像

列出镜像列表

我们可以使用 docker images或者docker image ls 列出镜像列表

ig:webapp itguang$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        7 days ago          109MB
training/webapp     latest              6fae60ef3446        2 years ago         349MB
ig:webapp itguang$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ae513a47849c        7 days ago          109MB
training/webapp     latest              6fae60ef3446        2 years ago         349MB

各个选项说明:

REPOSITORY:表示镜像的仓库源

TAG:镜像的标签

IMAGE ID:镜像ID

CREATED:镜像创建时间

SIZE:镜像大小

同一个 REPOSITORY 可以有多个TAG,代表这个REPOSITORY的不同版本.如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:

$ docker run -t -i ubuntu:15.10 /bin/bash 
root@d77ccb2e5cca:/#

如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。

获取一个新镜像

当我们在本地主机上使用一个不存在的镜像时,docker就会从docker hub 自动下载镜像,如果我们想预先下载这个镜像,可以使用docker pull命令.

ig:webapp itguang$ docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
324d088ce065: Pull complete
2ab951b6c615: Pull complete
9b01635313e2: Pull complete
04510b914a6c: Pull complete
83ab617df7b4: Pull complete
Digest: sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Status: Downloaded newer image for ubuntu:14.04

下载完成后,我们就可以直接使用这个镜像来运行容器.

查找镜像

我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/

我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个httpd的镜像来作为我们的web服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。

ig:webapp itguang$ docker search httpd
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                   The Apache HTTP Server Project                  1679                [OK]
hypriot/rpi-busybox-httpd               Raspberry Pi compatible Docker Image with a …   40
centos/httpd                                                                            18                                      [OK]
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   12
armhf/httpd                             The Apache HTTP Server Project                  8
macadmins/netboot-httpd                 use in combination with bruienne/bsdpy          5                                       [OK]
lolhens/httpd                           Apache httpd 2 Server                           2                                       [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                                       [OK]
rgielen/httpd-image-simple              Docker image for simple Apache httpd based o…   1                                       [OK]
fboaventura/dckr-httpd                  Small footprint http server to use with othe…   1                                       [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                                       [OK]
tplatform/aws-linux-httpd24-php71-fpm   aws-linux-httpd24-php71-fpm                     1                                       [OK]
epflidevelop/os-wp-httpd                WP httpd                                        1                                       [OK]
dockerpinata/httpd                                                                      0
trollin/httpd                                                                           0
tplatform/aws-linux-httpd24-php71       aws-linux-httpd24-php71                         0                                       [OK]
ppc64le/httpd                           The Apache HTTP Server Project                  0
manasip/httpd                                                                           0
publici/httpd                           httpd:latest                                    0                                       [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   0                                       [OK]
interlutions/httpd                      httpd docker image with debian-based config …   0                                       [OK]
tplatform/aws-linux-httpd24-php70       aws-linux-httpd24-php70                         0                                       [OK]
fintaffy/fintaffy-httpd                                                                 0
cilium/demo-httpd                                                                       0
buzzardev/httpd                         Based on the official httpd image               0                                       [OK]

参数解释:

NAME:镜像仓库源的名称

DESCRIPTION:镜像的描述

OFFICIAL:是否docker官方发布

创建镜像

当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。

1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像

更新镜像

更新镜像之前,我们需要使用镜像来创建一个容器。

runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/# 

在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit命令来退出这个容器。

此时ID为e218edb10161的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit来提交容器副本。

runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8

各个参数说明:

-m:提交的描述信息

-a:指定镜像作者

e218edb10161:容器ID

runoob/ubuntu:v2:指定要创建的目标镜像名

构建镜像

我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。

runoob@runoob:~$ cat Dockerfile 
FROM    centos:6.7
MAINTAINER      Fisher "fisher@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。

第一条FROM,指定使用哪个镜像源

RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。

然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。

runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
 ---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "fisher@sudops.com"
 ---> Using cache
 ---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
 ---> Using cache
 ---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......

参数说明:

-t :指定要创建的目标镜像名

. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径

设置镜像标签

我们可以使用 docker tag 命令,为镜像添加一个新的标签。

runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev

docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。

容器篇

我们先使用 docker container --help 查看容器有哪些命令可用:

ig:webapp itguang$ docker container --help

Usage:  docker container COMMAND

Manage containers

Options:


Commands:
  attach      Attach local standard input, output, and error streams to a running container
  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
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  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
  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
  wait        Block until one or more containers stop, then print their exit codes

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

下面我们通过一个实例来了解下docker 容器命令的使用方式:

实例:运行一个web应用程序

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

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

拉取image文件

ig:webapp itguang$ docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest

运行

ig:webapp itguang$ docker run -d -p 8001:5000 training/webapp python app.py
6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab
ig:webapp itguang$

参数说明:

-d: 让容器在后台运行

-p 8001:5000 把容器的5000端口映射到本机的8001端口.

查看web应用程序

使用docker ps 查看所有正在运行的容器

ig:webapp itguang$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6ea91d88fc04        training/webapp     "python app.py"          4 minutes ago       Up 4 minutes        0.0.0.0:8001->5000/tcp   awesome_yalow

通过docker ps 命令可以查看到容器的端口映射,docker还提供了另一个快捷方式:docker port,使用docker port [containerId/containerName]可以查看指定(名字或者id) 的容器映射的端口信息.

ig:webapp itguang$ docker port 6ea91d88fc04
5000/tcp -> 0.0.0.0:8001

查看容器日志

docker logs [containerId/containerName] 查看容器内部的标准输出

ig:webapp itguang$ docker logs -f 6ea91d88fc04
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [08/May/2018 01:57:40] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [08/May/2018 01:57:51] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [08/May/2018 01:57:52] "GET /favicon.ico HTTP/1.1" 404 -
172.17.0.1 - - [08/May/2018 01:57:53] "GET / HTTP/1.1" 200 -

参数解释:

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

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

查看容器进程

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

ig:webapp itguang$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6ea91d88fc04        training/webapp     "python app.py"          14 minutes ago      Up 14 minutes       0.0.0.0:8001->5000/tcp   awesome_yalow
e8c8fae59eaf        nginx               "nginx -g 'daemon of…"   11 hours ago        Up 11 hours         0.0.0.0:8000->80/tcp     mynginx
ig:webapp itguang$ docker top 6ea91d88fc04
PID                 USER                TIME                COMMAND
3268                root                0:00                python app.py

检查容器底层信息

使用 docker inspect [containerId/containerName] 检查容器的底层信息,它会返回一个json文件记录着docker容器的配置和基本信息

ig:webapp itguang$ docker inspect 6ea91d88fc04
[
    {
        "Id": "6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab",
        "Created": "2018-05-08T01:57:16.057824746Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3268,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-05-08T01:57:16.549183232Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
        "ResolvConfPath": "/var/lib/docker/containers/6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab/hostname",
        "HostsPath": "/var/lib/docker/containers/6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab/hosts",
        "LogPath": "/var/lib/docker/containers/6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab/6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab-json.log",
        "Name": "/awesome_yalow",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "8001"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/29b26bf2f3c2ea2ac555314bc2d24d22d0df32e9fdda9bd7857ace38b655a872-init/diff:/var/lib/docker/overlay2/28ec29d1be1cae13b7a0f179797a7fe9db8573300a00ea153265f0d5a6e02632/diff:/var/lib/docker/overlay2/9580cf40c13e51da606a31199980f656909ff305f681fdbaa5dd6e5682931d64/diff:/var/lib/docker/overlay2/4acc65dad48dd34a5ac90ee6a179978b394849d5b5f34fa62fbb09370df56229/diff:/var/lib/docker/overlay2/1aaa1241c9cc7ab75e8a38ae9b700402d7f1fd9c9c026ea90f3a92b21b1e3ca0/diff:/var/lib/docker/overlay2/a46254f67ca32101abe271c6d513b767d75d9a2ad7fd60161e61bf4f4573b168/diff:/var/lib/docker/overlay2/3c0617c989d4162b2520576ed8c93ce94d0f03f4702c4437145f8b45505949ed/diff:/var/lib/docker/overlay2/737006702cbbf49cb6ecf85e1e19bf1583cb122c7c3555d969f1a7414799c783/diff:/var/lib/docker/overlay2/0f34c1cc4439c5e4231afeccd1f80e7a9752a2e1a0e5c061ed2dce970c0d0eca/diff:/var/lib/docker/overlay2/cf60a468248590a9535d7103c11a6b6fc9b617391eb9e5ceb43978188eac3857/diff:/var/lib/docker/overlay2/02376b91aed01fc9ab846da641a34f723400df9ad0492643865ab7ca75b85765/diff:/var/lib/docker/overlay2/3c2229290a692c67a11ba7bdb027bf289353e4878dd225b49959f797df3ea470/diff:/var/lib/docker/overlay2/172cad00615443a6a4f2c2030bffa7735012f68f4a42686573e2781c6d4c21a3/diff:/var/lib/docker/overlay2/32a8e8ed68f72cd6c4438d07a8ca55d04d91d954ebd397ae46e75d3fb017b8bf/diff",
                "MergedDir": "/var/lib/docker/overlay2/29b26bf2f3c2ea2ac555314bc2d24d22d0df32e9fdda9bd7857ace38b655a872/merged",
                "UpperDir": "/var/lib/docker/overlay2/29b26bf2f3c2ea2ac555314bc2d24d22d0df32e9fdda9bd7857ace38b655a872/diff",
                "WorkDir": "/var/lib/docker/overlay2/29b26bf2f3c2ea2ac555314bc2d24d22d0df32e9fdda9bd7857ace38b655a872/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "6ea91d88fc04",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "python",
                "app.py"
            ],
            "Image": "training/webapp",
            "Volumes": null,
            "WorkingDir": "/opt/webapp",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "26aa29b5a74b1b875a6df5d0fed8d446bb5dbc13d6f8c95ffcbe82d50ca40b22",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8001"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/26aa29b5a74b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "0e5f2c40113e3487dc248182aca66ba6c10ec15b860190da5c61bc5b9ad947cd",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "c9b902b74db7effc428c8765e3bb06fd874e42cc7b3c6910ada26d5cc065d16f",
                    "EndpointID": "0e5f2c40113e3487dc248182aca66ba6c10ec15b860190da5c61bc5b9ad947cd",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
ig:webapp itguang$

停止容器

ig:webapp itguang$ docker stop 6ea91d88fc04
6ea91d88fc04
ig:webapp itguang$

使用 docker ps -l 命令查看最后一次创建的容器:

ig:webapp itguang$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
6ea91d88fc04        training/webapp     "python app.py"     22 minutes ago      Exited (137) 40 seconds ago                       awesome_yalow
ig:webapp itguang$

重启容器

对于已经停止的容器我们可以使用docker start [containerId/containerName]来启动容器,对于正在运行的容器,我们可以使用docker restart [containerId/containerName] 来重新启动容器

ig:webapp itguang$ docker start 6ea91d88fc04
6ea91d88fc04
ig:webapp itguang$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
6ea91d88fc04        training/webapp     "python app.py"     24 minutes ago      Up 9 seconds        0.0.0.0:8001->5000/tcp   awesome_yalow
ig:webapp itguang$ docker restart 6ea91d88fc04
6ea91d88fc04
ig:webapp itguang$

移除容器

我们可以使用docker rm [containeID/containerName]命令移除已经停止的容器

ig:webapp itguang$ docker rm 6ea91d88fc04
Error response from daemon: You cannot remove a running container 6ea91d88fc04ae2e1a27bb0c6d188f3b14390c13f6e60e3d943445e34d376eab. Stop the container before attempting removal or force remove
ig:webapp itguang$ docker stop 6ea91d88fc04
6ea91d88fc04
ig:webapp itguang$ docker rm 6ea91d88fc04
6ea91d88fc04
ig:webapp itguang$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
e8c8fae59eaf        nginx               "nginx -g 'daemon of…"   11 hours ago        Up 11 hours         0.0.0.0:8000->80/tcp   mynginx
ig:webapp itguang$
Logo

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

更多推荐