Docker使用一 : 镜像管理&容器管理

2. docker镜像管理

导图:

2.1 镜像的搜索/获取/查看

  • 镜像搜索

docker search 镜像名称

例: 搜索测试镜像hello-world为例

likedeMacBook-Pro:~ like$ docker search hello-world
NAME                                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
hello-world                                Hello World! (an example of minimal Dockeriz…   747                 [OK]                
kitematic/hello-world-nginx                A light-weight nginx container that demonstr…   112                                     
tutum/hello-world                          Image to test docker deployments. Has Apache…   58                                      [OK]
dockercloud/hello-world                    Hello World!                                    14                                      [OK]
hypriot/armhf-hello-world                  Hello World! (an example of minimal Dockeriz…   6                                       
armhf/hello-world                          Hello World! (an example of minimal Dockeriz…   5                                       
crccheck/hello-world                       Hello World web server in under 2.5 MB          5                                       [OK]
marcells/aspnet-hello-world                ASP.NET vNext - Hello World                     5                                       [OK]
bonomat/nodejs-hello-world                 a simple nodejs hello world container           3                                       [OK]
kornkitti/express-hello-world              Node.js Express Hello World : https://github…   2                                       
ppc64le/hello-world                        Hello World! (an example of minimal Dockeriz…   2                                       
hello-seattle                              Hello from DockerCon 2016 (Seattle)!            2                   [OK]                
arm32v7/hello-world                        Hello World! (an example of minimal Dockeriz…   1                                       
carinamarina/hello-world-app               This is a sample Python web application, run…   1                                       [OK]
microsoft/mcr-hello-world                  Hello World! (an example of minimal Dockeriz…   0                                       
ansibleplaybookbundle/hello-world-apb      An APB which deploys a sample Hello World! a…   0                                       [OK]
gscrivano/hello-world                      hello world example system container            0                                       [OK]
s390x/hello-world                          Hello World! (an example of minimal Dockeriz…   0                                       
ansibleplaybookbundle/hello-world-db-apb   An APB which deploys a sample Hello World! a…   0                                       [OK]
markmnei/hello-world-java                  Automated build of Hello World Java             0                                       [OK]
burdz/hello-world-k8s                      To provide a simple webserver that can have …   0                                       [OK]
kevindockercompany/hello-world                                                             0                                       
infrastructureascode/hello-world           A tiny "Hello World" web server with a healt…   0                                       [OK]
uniplaces/hello-world                                                                      0                                       
lkungs/docker-hello-world                  Simple Hello World Example                      0                                       [OK]
likedeMacBook-Pro:~ like$ 

  • 获取镜像:

docker pull 镜像名称. (下载远程仓库(如Docker Hub)中的镜像)
镜像存储目录(linux), Mac 不详
/var/lib/docker/image

例:

likedeMacBook-Pro:~ like$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:523e382ab1801f2a616239b1052bb7ee5a7cce6a06cfed27ccb93680eacad6ef
Status: Downloaded newer image for hello-world:latest
  • 查看镜像:

查看所有的镜像
方法一:docker images
方法二:docker image ls
查看某个镜像
docker images 镜像的名字
docker image ls 镜像的名字

例:

likedeMacBook-Pro:~ like$ docker images ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
likedeMacBook-Pro:~ like$ docker image ls hello-world
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB

2.2 镜像重命名/删除

  • 镜像重命名:

docker tag 源镜像名[:tag] 新的名字[:tag]
[:tag] 可以省略
注:重命名并不会重新复制一个镜像

例:

likedeMacBook-Pro:~ like$ docker tag hello-world  mytest 
likedeMacBook-Pro:~ like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB
mytest              latest              4ab4c602aa5e        2 months ago        1.84kB
  • 镜像删除:

docker rmi 镜像名/镜像ID
或者: docker image rm 镜像名/镜像ID

注:
1.如果镜像被重命名了, 通过镜像ID是不能直接删除的
2.如果镜像被重命名了2次, 需要删除两次镜像才会被删除

例:

likedeMacBook-Pro:~ like$ docker rmi hello-world  
Untagged: hello-world:latest
Untagged: hello-world@sha256:523e382ab1801f2a616239b1052bb7ee5a7cce6a06cfed27ccb93680eacad6ef
likedeMacBook-Pro:~ like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mytest              latest              4ab4c602aa5e        2 months ago        1.84kB

2.3 镜像的导入导出

  • 镜像导入:

docker load -i/--input 要导入的镜像文件的名字
或:docker load < 要导入的镜像文件的名字

例: 将本地的redis .tar 导入本地仓库中

likedeMacBook-Pro:images like$  docker load -i redis.tar 
ef68f6734aa4: Loading layer  58.44MB/58.44MB
2c1e04fae1f2: Loading layer  338.4kB/338.4kB
94bdf74df564: Loading layer  3.033MB/3.033MB
3fa9bea13981: Loading layer  36.37MB/36.37MB
eaa9c0598e6d: Loading layer  1.536kB/1.536kB
33bc8c18cc05: Loading layer  3.584kB/3.584kB
Loaded image: redis:latest
likedeMacBook-Pro:images like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              c188f257942c        2 weeks ago         94.9MB
mytest              latest              4ab4c602aa5e        2 months ago        1.84kB

  • 镜像导出:

docker save -o/--output 导出之后的镜像名(自己起名) 要导出的镜像的镜像名/镜像ID
注:会导出到当前终端所在路径

likedeMacBook-Pro:~ like$ docker save -o 111 mytest
likedeMacBook-Pro:~ like$ ls
111             HydraLog            go
Applications            Library             hydra.txt
Applications (Parallels)    Movies              ipfsTest
ChainTest           Music               ipmsg.db
Desktop             Parallels           reactProject
Documents           Pictures            shj-git
Downloads           Public              truffleTest

2.4 镜像的历史和详细信息

  • 查看镜像的历史信息:

查看制作镜像的时候的操作步骤
docker history 镜像名/镜像ID

例:

likedeMacBook-Pro:images like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              c188f257942c        2 weeks ago         94.9MB
mytest              latest              4ab4c602aa5e        2 months ago        1.84kB
likedeMacBook-Pro:images like$ docker history mytest
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4ab4c602aa5e        2 months ago        /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
<missing>           2 months ago        /bin/sh -c #(nop) COPY file:9824c33ef192ac94…   1.84kB              
  • 查看镜像的历史信息:

镜像的属性信息, 以json格式输出的
docker inspect 镜像名/镜像ID
查看指定json格式输出的key 对应得值
docker inspect -f {{.}} 镜像名/镜像ID

例:

likedeMacBook-Pro:~ like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              c188f257942c        2 weeks ago         94.9MB
mytest              latest              4ab4c602aa5e        2 months ago        1.84kB
likedeMacBook-Pro:~ like$ docker inspect mytest
[
    {
        "Id": "sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f",
        "RepoTags": [
            "mytest:latest"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2018-09-07T19:25:39.809797627Z",
        "Container": "15c5544a385127276a51553acb81ed24a9429f9f61d6844db1fa34f46348e420",
        "ContainerConfig": {
            "Hostname": "15c5544a3851",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9a5813f1116c2426ead0a44bbec252bfc5c3d445402cc1442ce9194fc1397027",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:9a5813f1116c2426ead0a44bbec252bfc5c3d445402cc1442ce9194fc1397027",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1840,
        "VirtualSize": 1840,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/5cdd66e6bbc849c43158d0b51af61c8444f2d1b12265b6a6bd7827a8590b0169/merged",
                "UpperDir": "/var/lib/docker/overlay2/5cdd66e6bbc849c43158d0b51af61c8444f2d1b12265b6a6bd7827a8590b0169/diff",
                "WorkDir": "/var/lib/docker/overlay2/5cdd66e6bbc849c43158d0b51af61c8444f2d1b12265b6a6bd7827a8590b0169/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:428c97da766c4c13b19088a471de6b622b038f3ae8efa10ec5a37d6d31a2df0b"
            ]
        },
        "Metadata": {
            "LastTagTime": "2018-12-03T13:35:07.686031597Z"
        }
    }
]

// 查看id
likedeMacBook-Pro:~ like$ docker inspect -f {{.Id}} mytest
sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f

3. docker容器管理

导图:

image.png

3.1 创建/启动

  • 容器创建:

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
注: 容器被创建, 但是还不能使用, 需要启动

  • OPTIONS: 创建容器的一些参数:
    -i, --interactive: 容器是否和标准输入进行关联
    -t, --tty: 创建容器的时候是不是给容器关联虚拟终端
    --rm: 容器停止之后是否会自动销毁
    --name: 给创建的容器指定一个名字, 如果不指定, 名字是随机生成的
  • IMAGE:
    镜像名/镜像ID
  • [COMMAND] [ARG...]
    启动容器之后, 在容器中执行一个命令
    不知道指定什么命令, 指定bash

例:
创建镜像redis 的容器, 关联标准输入和虚拟终端, 并给容器命名为test。
返回的是对应容器的id

likedeMacBook-Pro:~ like$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              c188f257942c        2 weeks ago         94.9MB
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB
likedeMacBook-Pro:~ like$ docker create
  • 容器启动:

docker start 参数 容器的名字

  • 参数:
  1. a: 关联标准输出/标准错误
  2. i: 关联标准输入

例: 启动之前创建的test容器, 直接进入root @9525ce400a20(test容器)的更目录下的data文件夹中
(输入 exit 可以退出容器)

likedeMacBook-Pro:~ like$ docker start -ai test
root@9525ce400a20:/data# 
  • 创建新容器并启动: creat+start

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

  • OPTIONS:
  1. -i, --interactive: 容器是否和标准输入进行关联
  2. -t, --tty: 创建容器的时候是不是给容器管理虚拟终端
  3. --rm: 容器停止之后是否会自动销毁
  4. --name: 给创建的容器指定一个名字, 如果不指定, 名字是随机生成的
  5. -d, --detach: 容器启动之后是守护进程, 容器启动之后, 不能直接进入到容器内部

例: 以守护进程启动redis容器

likedeMacBook-Pro:~ like$ docker run -itd --name test redis bash
3f367d1e9e6afbbb57e7ee5250feaa63962279236d44d8e67380e2de46493d73

3.2 容器查看

  • 查看运行状态的容器:

docker ps

  • 查看查看所有状态的容器:

docker ps -a

例:查看正在运行的容器: redis

likedeMacBook-Pro:~ like$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp            test

3.3 容器的暂停/重启

  • 暂停:

docker pause 容器名/容器ID

例: 暂停之前启动的redis的容器(test)

likedeMacBook-Pro:~ like$ docker pause test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes (Paused)   6379/tcp            test

  • 取消暂停:

docker unpause 容器名/容器ID
例: 取消暂停test 容器

likedeMacBook-Pro:~ like$ docker unpause test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        6379/tcp            test
likedeMacBook-Pro:~ like$ 
  • 重启:

docker restart [OPTIONS] CONTAINER [CONTAINER...]

  • OPTIONS:
    -t: 延时重启, 默认10s

例: 延时20s重启test 容器

likedeMacBook-Pro:~ like$ docker restart -t 20 test
test

3.4 容器的关闭/终止/删除

  • 关闭:

延时关闭容器, 默认10s
docker stop [-t 延时时间] 容器名/ 容器id
例: 关闭test容器

likedeMacBook-Pro:~ like$ docker stop test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   9 minutes ago       Exited (0) 2 seconds ago                       test
  • 终止:

直接马上关闭
docker kill 容器名/ 容器id

例: 先通过start 命令启动test 容器,在终止容器

likedeMacBook-Pro:~ like$ docker start test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   10 minutes ago      Up 3 seconds        6379/tcp            test
likedeMacBook-Pro:~ like$ docker kill test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   11 minutes ago      Exited (137) 2 seconds ago                       test

  • 删除:

    1. 删除未运行的容器:

docker rm 容器名/容器ID

例: 删除(exited状态)的test 容器

likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
3f367d1e9e6a        redis               "docker-entrypoint.s…"   19 hours ago        Exited (137) 19 hours ago                       test
likedeMacBook-Pro:~ like$ docker rm test
test
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
likedeMacBook-Pro:~ like$ 

    1. 删除运行的容器:

docker rm -f 容器名/容器ID

例:

//   显示所有镜像
likedeMacBook-Pro:~ like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              c188f257942c        2 weeks ago         94.9MB
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB

//  创建并运行redis镜像 的容器, 并命名为test
likedeMacBook-Pro:~ like$ docker run -itd --name test  redis
83e3fc034a66a18e033e25c98fcd106582a56f8727b6fd120407eed5c96b5632

// 查看容器
likedeMacBook-Pro:~ like$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
83e3fc034a66        redis               "docker-entrypoint.s…"   9 seconds ago       Up 8 seconds        6379/tcp            test

// 删除容器
likedeMacBook-Pro:~ like$ docker rm -f test
test

// 查看容器
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

-3. 批量删除:

docker rm -f $(docker ps -aq)
docker rm. -f `docker ps -aq`

原理:
docker ps -aq: 只显示所有容器的Id
先通过docker ps -aq获取所有的容器id, 然后执行rm 指令,实现批量删除容器

例:

// 创建test1 和test2 连个容器
likedeMacBook-Pro:~ like$ docker run -itd --name test1  redis
bd3e862c6c29f83918d552c625dc7bbb16b9b826e929d4f33b49fb94a8809f5b
likedeMacBook-Pro:~ like$ docker run -itd --name test2  redis
72c3dee89cdda161262448245ef3f31304407abcb3e0a098f9b3bc0cf3c2767f

//  查看容器状态
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
72c3dee89cdd        redis               "docker-entrypoint.s…"   4 seconds ago       Up 4 seconds        6379/tcp            test2
bd3e862c6c29        redis               "docker-entrypoint.s…"   9 seconds ago       Up 9 seconds        6379/tcp            test1
// 删除所有容器
likedeMacBook-Pro:~ like$ docker rm -f $(docker ps -aq)
72c3dee89cdd
bd3e862c6c29
likedeMacBook-Pro:~ like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3.5 容器的关闭/终止/删除

  • 进入容器:
    1. 创建并进入:

docker run -it --name 容器名 镜像名 shell命令

例:


likedeMacBook-Pro:images like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              93fd78260bd1        2 weeks ago         86.2MB
redis               latest              c188f257942c        2 weeks ago         94.9MB
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB
likedeMacBook-Pro:images like$ docker run -it --name test ubuntu bash
// 进入test 容器
root@8bbd5524c408:/# 
    1. 手动进入:

以守护进程启动容器
docker run -itd --name 容器名 镜像名 shell命令
进入容器
docker exec -it 容器名/容器ID bash

例:

likedeMacBook-Pro:images like$ docker run -itd --name test ubuntu bash
41742fde9ca4226da248c96e6964b9aaef3bd16b5e84730b7fe9750a68939a5f
likedeMacBook-Pro:images like$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
41742fde9ca4        ubuntu              "bash"              13 seconds ago      Up 11 seconds                           test
 
likedeMacBook-Pro:images like$ docker exec -it test bash
root@41742fde9ca4:/# 
  • ** 退出容器**:

输入: exit
注意如果在docker run 时没有指定-rm , exit 之后, 需要手动删除容器

例:

likedeMacBook-Pro:images like$ docker run -itd --name test ubuntu bash
41742fde9ca4226da248c96e6964b9aaef3bd16b5e84730b7fe9750a68939a5f
likedeMacBook-Pro:images like$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
41742fde9ca4        ubuntu              "bash"              13 seconds ago      Up 11 seconds                           test
root@41742fde9ca4:/# exit
exit
likedeMacBook-Pro:images like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
41742fde9ca4        ubuntu              "bash"              About a minute ago   Up About a minute                       test
likedeMacBook-Pro:images like$ 

3.6 容器的日志/信息/端口/重命名

  • 查看容器的日志信息:

docker logs 容器名/容器ID

  • 查看容器的详细信息:

docker inspect 容器名/容器ID

例:

likedeMacBook-Pro:images like$ docker run -itd --name test ubuntu bash
8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161
likedeMacBook-Pro:images like$ docker inspect test
[
    {
        "Id": "8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161",
        "Created": "2018-12-04T11:20:36.898518821Z",
        "Path": "bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3837,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-12-04T11:20:37.416540351Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:93fd78260bd1495afb484371928661f63e64be306b7ac48e2d13ce9422dfee26",
        "ResolvConfPath": "/var/lib/docker/containers/8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161/hostname",
        "HostsPath": "/var/lib/docker/containers/8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161/hosts",
        "LogPath": "/var/lib/docker/containers/8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161/8ab7646210c6967a9578aa61b76f573dd6c2df503f04d5a32c7d2f91ed37b161-json.log",
        "Name": "/test",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "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,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/e6d5ad37b570804029b3beac2578563f2362c5bf79d21e41da39632faca708ce-init/diff:/var/lib/docker/overlay2/c0bf5d22aa7a88e99d0b7db3628a4bb4c3f175b688e676bdefee6a253ef362cd/diff:/var/lib/docker/overlay2/983304467a4abbf47642bc6d6649e95033e912cb739628027157db947111710f/diff:/var/lib/docker/overlay2/dae99ae8e29284a46b72be3616c1168886d3db934ee23f0fa05d33db4db094c7/diff:/var/lib/docker/overlay2/b169cfcef76674f2073c968b8372712bbae4586216e46645ea2103b90a5b766a/diff",
                "MergedDir": "/var/lib/docker/overlay2/e6d5ad37b570804029b3beac2578563f2362c5bf79d21e41da39632faca708ce/merged",
                "UpperDir": "/var/lib/docker/overlay2/e6d5ad37b570804029b3beac2578563f2362c5bf79d21e41da39632faca708ce/diff",
                "WorkDir": "/var/lib/docker/overlay2/e6d5ad37b570804029b3beac2578563f2362c5bf79d21e41da39632faca708ce/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "8ab7646210c6",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "bash"
            ],
            "Image": "ubuntu",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "b2f7161877c30b1f4381e5091029da2422e97e1cc43ffb2a5c2a92cdfc0f4a12",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/b2f7161877c3",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "b8245d551b9743a12e856e78572318a1fa952c741800094810ad62055325afc7",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "aa60b30f9150abd8f33614fd78b05e6a9efd4107a4d679aefdc6a447ca317019",
                    "EndpointID": "b8245d551b9743a12e856e78572318a1fa952c741800094810ad62055325afc7",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

  • 查看容器的端口信息:

查看本机和容器的端口映射
docker port 容器名/容器ID

  • 容器重命名:

docker rename 旧的容器名 新的容器名

例:

// 创建并运行容器
likedeMacBook-Pro:~ like$ docker run -itd --name test redis
d46e9f768536098731bba5c6ce0f073b864a0ab2420996f2f82f8868aaea3b5e
likedeMacBook-Pro:~ like$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d46e9f768536        redis               "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        6379/tcp            test
// 重命名
likedeMacBook-Pro:~ like$ docker rename test test2
likedeMacBook-Pro:~ like$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d46e9f768536        redis               "docker-entrypoint.s…"   20 seconds ago      Up 19 seconds       6379/tcp            test2

3.5 基于容器导入导出镜像

  • 导出:

docker export 容器名(mycontainer)/容器id > 导出的镜像的名字

likedeMacBook-Pro:images like$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8ab7646210c6        ubuntu              "bash"              35 minutes ago      Up 35 minutes                           test
likedeMacBook-Pro:images like$ docker export test > myubuntu
likedeMacBook-Pro:images like$ ls
myubuntu    nginx.tar   redis.tar   ubuntu.tar  ubuntuPlus.tar

: export导出容器中的镜像, 会丢失镜像的历史记录

  • 导入:

cat 要导入的镜像 | docker import - 导入的镜像名字(自己起)
相比较于 docker load -i/--input 镜像名 , 优势在于可以改名

例:导入之前导出的myubuntu镜像

likedeMacBook-Pro:images like$ ls
myubuntu    nginx.tar   redis.tar   ubuntu.tar  ubuntuPlus.tar
likedeMacBook-Pro:images like$ cat myubuntu | docker import - mytest
sha256:2be6428a084da79e4107e08e5433771a27c21f36200f08f447ac4f198ea59a7b
likedeMacBook-Pro:images like$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mytest              latest              2be6428a084d        4 seconds ago       69.8MB

// 查看历史信息, 发现丢失原来镜像的历史信息
likedeMacBook-Pro:images like$ docker history mytest
IMAGE               CREATED              CREATED BY          SIZE                COMMENT
2be6428a084d        About a minute ago                       69.8MB              Imported from -

注:

所有的docker 指令, 都可以通过 --help option 来查看指令的使用方法。

例:



likedeMacBook-Pro:images like$ docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,968评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,601评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,220评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,416评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,425评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,144评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,432评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,088评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,586评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,028评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,137评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,783评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,343评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,333评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,559评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,595评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,901评论 2 345

推荐阅读更多精彩内容