码迷,mamicode.com
首页 > 其他好文 > 详细

入门:Docker常用命令及解释

时间:2018-12-08 23:46:38      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:说明   repos   ada   sig   重启   process   fda   logout   ade   

一、 容器生命周期管理

1. create

命令解释:创建一个容器,但不启动,容器名称为 mynginx

leoheng-MacBook-Pro:~ leo$ docker create --name mynginx   nginx:latest
Unable to find image ‘nginx:latest‘ locally
latest: Pulling from library/nginx
a5a6f2f73cd8: Pull complete 
1ba02017c4b2: Pull complete 
33b176c904de: Pull complete 
Digest: sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba
Status: Downloaded newer image for nginx:latest
3a9e561b7629c6880c776a9331dcc9d052685f08c98f58c940e1a164c1f8d77c
leoheng-MacBook-Pro:~ leo$ 

2. run

命令解释:创建一个容器并运行它
可选参数:
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-p: 端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="mynginx": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
-h "leo": 指定容器的hostname;
-e username="uleo": 设置环境变量;
--env-file=[]: 从指定文件读入环境变量;
--cpuset="0-2" 或者 --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值;
--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
--link=[]: 添加链接到另一个容器;
--expose=[]: 开放一个端口或一组端口;

leoheng-MacBook-Pro:~ leo$ docker run -it -d -p 222:22 -p 8080:80 --restart=always  --name nginx nginx:latest
feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f
leoheng-MacBook-Pro:~ leo$ docker ps –a #查看容器进程
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   About a minute ago   Up About a minute   0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$

3. start/stop/restart

命令解释:容器的启动,关闭,重启

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker stop feea481119cb
feea481119cb
leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
leoheng-MacBook-Pro:~ leo$ docker restrat feea481119cb
docker: ‘restrat‘ is not a docker command.
See ‘docker --help‘
leoheng-MacBook-Pro:~ leo$ docker restart feea481119cb
feea481119cb
leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   3 minutes ago       Up 2 seconds        0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ 

4. pause/unpause

命令解释:容器的暂停或恢复暂停容器,暂停状态无法正常使用该容器。

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   4 minutes ago       Up About a minute   0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker pause feea481119cb
feea481119cb
leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   6 minutes ago       Up 2 minutes (Paused)   0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker unpause feea481119cb
feea481119cb
leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   6 minutes ago       Up 2 minutes        0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ 

5. exec

命令解释:在运行的容器中执行命令或者进去容器

leoheng-MacBook-Pro:~ leo$ docker exec -it nginx  /bin/bash
root@feea481119cb:/# hostname 
feea481119cb
root@feea481119cb:/#

6. kill

命令解释:终止一个运行中的容器
参数:
-s :向容器发送终止信号

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   14 minutes ago      Up 10 minutes       0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker kill nginx
nginx
leoheng-MacBook-Pro:~ leo$ docker  ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
leoheng-MacBook-Pro:~ leo$

7. rm

命令解释:删除一个或者多个容器
参数:
-f :通过终止信号强制删除一个运行中的容器
-l :移除容器间的网络连接,而非容器本身
-v :删除与容器关联的卷

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   16 minutes ago      Up 41 seconds       0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker rm nginx
Error response from daemon: You cannot remove a running container feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f. Stop the container before attempting removal or force remove
leoheng-MacBook-Pro:~ leo$ docker stop  nginx
nginx
leoheng-MacBook-Pro:~ leo$ docker rm nginx
nginx
leoheng-MacBook-Pro:~ leo$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
leoheng-MacBook-Pro:~ leo$ 

二、 容器操作

1. ps

命令解释:列出容器
参数:
-a :显示所有的容器,包括未运行的容器。
-f :根据条件过滤显示的内容
--format :指定返回值的模版
-l :显示最近创建的容器
-n :列出最近创建的n个容器
--no-trunc :不截断输出
-q :静默模式,只显示容器编号
-s :显示总的文件大小。

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
leoheng-MacBook-Pro:~ leo$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
feea481119cb        nginx:latest        "nginx -g ‘daemon of…"   16 minutes ago      Exited (137) About a minute ago                       nginx
leoheng-MacBook-Pro:~ leo$

2. inspect

命令解释:获取容器/镜像的元数据
参数:
-f : 指定返回值的模版文件
-s :显示在难过的文件大小
--type :为指定类型返回json

查看nginx:latest的元信息

leoheng-MacBook-Pro:~ leo$ docker inspect nginx:latest
[
    {
        "Id": "sha256:568c4670fa800978e08e4a51132b995a54f8d5ae83ca133ef5546d092b864acf",
        "RepoTags": [
            "nginx:latest"
        ],
        "RepoDigests": [
            "nginx@sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-11-27T22:21:45.207738888Z",
        "Container": "1455cab9c97f6945336dd97d5dfca0429eacbb739fcd362cc528ed8a175fcc7b",
        "ContainerConfig": {
            "Hostname": "1455cab9c97f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.15.7-1~stretch",
                "NJS_VERSION=1.15.7.0.2.6-1~stretch"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:cf9409661039371fa3448619fb988625e4542108921c4c8f923c5bccc51601eb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.15.7-1~stretch",
                "NJS_VERSION=1.15.7.0.2.6-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:cf9409661039371fa3448619fb988625e4542108921c4c8f923c5bccc51601eb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": [],
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 109116878,
        "VirtualSize": 109116878,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/b2a9c894aa016618fdae88f2cd152cf11f1f05461d927d4682efe60c4366f038/diff:/var/lib/docker/overlay2/a9bc2292493221b766a47272eb1d9d7c2b0049bda74a745dd82bc82443f5ec1f/diff",
                "MergedDir": "/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/merged",
                "UpperDir": "/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/diff",
                "WorkDir": "/var/lib/docker/overlay2/b9af4051377bdbc6e1435834a73ed8c23d5bb89bfef33eb199347ff315038f32/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3",
                "sha256:ad5345cbb119f7c720123e3adf28b164143e4157ca6e46a629ca694e75f7825f",
                "sha256:ece4f9fdef598687f23d39643bacbf2c609201b087b93bbae81b931da72d2a64"
            ]
        },
        "Metadata": {
            "LastT
agTime": "0001-01-01T00:00:00Z"
        }
    }
]
leoheng-MacBook-Pro:~ leo$

获取容器的ip方法

leoheng-MacBook-Pro:~ leo$ docker inspect -f {{.NetworkSettings.Networks.bridge.IPAddress}}  nginx
172.17.0.2

或者

leoheng-MacBook-Pro:~ leo$ docker inspect --format=‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘ nginx
172.17.0.2
leoheng-MacBook-Pro:~ leo$

3. top

命令解释:查看容器中运行的进程信息,支持ps的参数

leoheng-MacBook-Pro:~ leo$ docker top nginx
PID                 USER                TIME                COMMAND
3208                root                0:00                nginx: master process nginx -g daemon off;
3251                101                 0:00                nginx: worker process
leoheng-MacBook-Pro:~ leo$

查看所有容器的进程信息

leoheng-MacBook-Pro:~ leo$ for i in `docker ps |grep Up|awk ‘{print $1}‘` ; do echo \ && docker top $i ; done

PID                 USER                TIME                COMMAND
3208                root                0:00                nginx: master process nginx -g daemon off;
3251                101                 0:00                nginx: worker process
leoheng-MacBook-Pro:~ leo$

4. logs

命令解释:获取容器的日志
参数:
-f :跟踪日志输出
--since :显示某个开始时间的所有日志
-t :显示时间戳
--tail :仅列出最新N条容器日志

leoheng-MacBook-Pro:~ leo$ docker logs -f nginx 
172.17.0.1 - - [08/Dec/2018:03:22:35 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
2018/12/08 03:22:36 [error] 24#24: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.1.3:8080", referrer: "http://192.168.1.3:8080/"
172.17.0.1 - - [08/Dec/2018:03:22:36 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.1.3:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
172.17.0.1 - - [08/Dec/2018:03:22:47 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
172.17.0.1 - - [08/Dec/2018:03:23:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15" "-"
2018/12/08 03:23:48 [error] 24#24: *5 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.1.3:8080", referrer: "http://192.168.1.3:8080/"
172.17.0.1 - - [08/Dec/2018:03:23:48 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://192.168.1.3:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15" "-"

查看容器nginx从2018/12/8的最新5条日志

leoheng-MacBook-Pro:~ leo$ docker logs --since="2018-12-08" --tail=5 nginx
172.17.0.1 - - [08/Dec/2018:03:22:36 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.1.3:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
172.17.0.1 - - [08/Dec/2018:03:22:47 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" "-"
172.17.0.1 - - [08/Dec/2018:03:23:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15" "-"
2018/12/08 03:23:48 [error] 24#24: *5 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.1.3:8080", referrer: "http://192.168.1.3:8080/"
172.17.0.1 - - [08/Dec/2018:03:23:48 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "http://192.168.1.3:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15" "-"
leoheng-MacBook-Pro:~ leo$ 

5. events

命令解释:从服务器获取实时事件
参数:
-f :根据条件过滤事件;
--since :从指定的时间戳后显示所有事件;
--until :流水时间显示到指定的时间为止;

获取2018/12/8的所有事件

leoheng-MacBook-Pro:~ leo$ docker events --since="2018-12-08" 
2018-12-08T09:46:57.832351102+08:00 image pull nginx:latest (maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
2018-12-08T09:46:57.938764857+08:00 container create 3a9e561b7629c6880c776a9331dcc9d052685f08c98f58c940e1a164c1f8d77c (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=mynginx)
2018-12-08T10:23:52.553078826+08:00 container create feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
2018-12-08T10:23:52.678445332+08:00 network connect 2651d136cf62ba9dc0ebe674eca0c1d9f73cf4230e345aec01be0ddd797715bb (container=feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f, name=bridge, type=bridge)
2018-12-08T10:23:53.133906581+08:00 container start feea481119cb053e84bf3bda6f1b29beaddb3f91899f4d28775c4015f0dafa2f (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=nginx)
2018-12-08T10:25:07.949445750+08:00 container destroy 3a9e561b7629c6880c776a9331dcc9d052685f08c98f58c940e1a164c1f8d77c (image=nginx:latest, maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>, name=mynginx)

如果指定的时间是到秒级的,需要将时间转成时间戳。如果时间为日期的话,可以直接使用,如--since="2016-07-01"。

6. wait

命令说明:阻塞运行知道容器停止,然后打印出它的退出代码

leoheng-MacBook-Pro:~ leo$ docker wait nginx

7. attach

命令解释:连接到正在运行中的容器
--sig-proxy:防止CTRL+D 或 CRTL+C退出容器

leoheng-MacBook-Pro:~ leo$ docker attach --sig-proxy=false nginx
172.17.0.1 - - [08/Dec/2018:03:37:47 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15" "-"

8. port

命令解释:列出指定的容器的端口映射,或查找将PRIVATE_PORT NAT到面向公众的端口。

leoheng-MacBook-Pro:~ leo$ docker port nginx 
22/tcp -> 0.0.0.0:222
80/tcp -> 0.0.0.0:8080
leoheng-MacBook-Pro:~ leo$

9. export

命令解释:将文件系统作为一个tar归档文件导出到STDOUT。保存当前镜像状态。

leoheng-MacBook-Pro:~ leo$ docker export -o nginx-leo.tar nginx
leoheng-MacBook-Pro:~ leo$ ls nginx-leo.tar 
nginx-leo.tar
leoheng-MacBook-Pro:~ leo$ 

三、 容器rootfs命令

1. commit

命令解释:从容器创建一个新的镜像,提交到镜像库。
参数:
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。

leoheng-MacBook-Pro:~ leo$ docker commit -a "leo"  -m "leo commit nginx test"  nginx  leo:v1 
sha256:6ec4ef7718a8b311f261e8eb12f2b55a42f631e75ad7cd7a5c105620c6abd7ce
leoheng-MacBook-Pro:~ leo$ docker images  leo:v1
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
leo                 v1                  6ec4ef7718a8        10 seconds ago      109MB
leoheng-MacBook-Pro:~ leo$

2. cp

命令说明:用于容器与主机之间的数据拷贝
参数说明:
-L :保持源目标中的链接

leoheng-MacBook-Pro:~ leo$ echo "nginxtest"  >> test
leoheng-MacBook-Pro:~ leo$ docker cp test  nginx:/
leoheng-MacBook-Pro:~ leo$ docker exec -it nginx /bin/bash
root@96ddce20c551:/# cat test
nginxtest
root@96ddce20c551:/#

3. diff

命令说明:检查容器里文件结构的更改

leoheng-MacBook-Pro:~ leo$ docker diff nginx
C /run
A /run/nginx.pid
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
C /root
A /root/.bash_history
A /test
leoheng-MacBook-Pro:~ leo$

四、 镜像仓库

1. login/logout

命令解释:登陆或登出docker镜像仓库
参数:
-u :登陆的用户名
-p :登陆的密码

因为我下载了docker客户端,直接命令输入

leoheng-MacBook-Pro:~ leo$ docker login –u leo –p *******
Authenticating with existing credentials...
Login Succeeded
leoheng-MacBook-Pro:~ leo$ 

2. pull

命令解释:从镜像库拉取镜像活着更新指定镜像
参数:
-a :拉取所有 tagged 镜像
--disable-content-trust :忽略镜像的校验,默认开启

leoheng-MacBook-Pro:~ leo$ docker pull nginx

3. push

命令解释:将本地的镜像上传到镜像仓库,前提要先登录镜像仓库
参数:
--disable-content-trust :忽略镜像的校验,默认开启

leoheng-MacBook-Pro:~ leo$ docker push leo:v1
The push refers to repository [docker.io/library/leo]
fae8ad5437e1: Preparing 
ece4f9fdef59: Preparing 
ad5345cbb119: Preparing 
ef68f6734aa4: Preparing 
denied: requested access to the resource is denied
leoheng-MacBook-Pro:~ leo$ 

4. search

命令解释:从docker hub 中查找镜像
参数:
--automated :只列出 automated build类型的镜像;
--no-trunc :显示完整的镜像描述;
-s :列出收藏数不小于指定值的镜像。

leoheng-MacBook-Pro:~ leo$ docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10502               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1483                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   660                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   450                                     [OK]
kong                                                   Open-source Microservice & API Management la…   257                 [OK]                
webdevops/php-nginx                                    Nginx with PHP-FPM                              118                                     [OK]

五、 本地镜像管理

1. images

命令解释:列出本地镜像
参数
-a :列出本地所有的镜像(含中间映像层,默认情况下,过滤掉中间映像层);
--digests :显示镜像的摘要信息;
-f :显示满足条件的镜像;
--format :指定返回值的模板文件;
--no-trunc :显示完整的镜像信息;
-q :只显示镜像ID。

leoheng-MacBook-Pro:~ leo$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
leo                 v1                  6ec4ef7718a8        5 hours ago         109MB
nginx               latest              568c4670fa80        10 days ago         109MB
leoheng-MacBook-Pro:~ leo$ 

2. tag

命令说明:标记本地镜像,将其归入某一个仓库

leoheng-MacBook-Pro:~ leo$ docker tag 6ec4ef7718a8 leoheng/leo:v1
leoheng-MacBook-Pro:~ leo$ docker images leoheng/leo:v1
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
leoheng/leo         v1                  6ec4ef7718a8        5 hours ago         109MB
leoheng-MacBook-Pro:~ leo$

3. history

命令解释:查看指定镜像的创建历史
参数:
-H :以可读的格式打印镜像大小和日期,默认为true;
--no-trunc :显示完整的提交记录;
-q :仅列出提交记录ID。

leoheng-MacBook-Pro:~ leo$ docker history leoheng/leo:v1
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
6ec4ef7718a8        5 hours ago         nginx -g daemon off;                            108B                leo commit nginx test
568c4670fa80        10 days ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
<missing>           10 days ago         /bin/sh -c #(nop)  STOPSIGNAL [SIGTERM]         0B                  
<missing>           10 days ago         /bin/sh -c #(nop)  EXPOSE 80/tcp                0B                  
<missing>           10 days ago         /bin/sh -c ln -sf /dev/stdout /var/log/nginx…   22B                 
<missing>           10 days ago         /bin/sh -c set -x  && apt-get update  && apt…   53.8MB              
<missing>           10 days ago         /bin/sh -c #(nop)  ENV NJS_VERSION=1.15.7.0.…   0B                  
<missing>           10 days ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.15.7-…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop) ADD file:dab9baf938799c515…   55.3MB              
leoheng-MacBook-Pro:~ leo$ 

4. build

命令解释:用于Dockerfile创建镜像
参数
--build-arg=[] :设置镜像创建时的变量;
--cpu-shares :设置 cpu 使用权重;
--cpu-period :限制 CPU CFS周期;
--cpu-quota :限制 CPU CFS配额;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的内存 id;
--disable-content-trust :忽略校验,默认开启;
-f :指定要使用的Dockerfile路径;
--force-rm :设置镜像过程中删除中间容器;
--isolation :使用容器隔离技术;
--label=[] :设置镜像使用的元数据;
-m :设置内存最大值;
--memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
--no-cache :创建镜像的过程不使用缓存;
--pull :尝试去更新镜像的新版本;
--quiet, -q :安静模式,成功后只输出镜像 ID;
--rm :设置镜像成功后删除中间容器;
--shm-size :设置/dev/shm的大小,默认值是64M;
--ulimit :Ulimit配置。
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
--network: 默认 default。在构建期间设置RUN指令的网络模式

使用当前目录的 Dockerfile 创建镜像,标签为 runoob/ubuntu:v1。

docker build -t runoob/ubuntu:v1 .

使用URL github.com/creack/docker-firefox 的 Dockerfile 创建镜像。

docker build github.com/creack/docker-firefox

也可以通过 -f Dockerfile 文件的位置:

docker build -f /path/to/a/Dockerfile .

在 Docker 守护进程执行 Dockerfile 中的指令前,首先会对 Dockerfile 进行语法检查,有语法错误时会返回:

$ docker build -t test/myapp .
Sending build context to Docker daemon 2.048 kB
Error response from daemon: Unknown instruction: RUNCMD

5. save

命令解释:将指定镜像保存成tar归档文件

leoheng-MacBook-Pro:~ leo$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                       NAMES
96ddce20c551        nginx:latest        "nginx -g ‘daemon of…"   6 hours ago         Up 5 hours          0.0.0.0:222->22/tcp, 0.0.0.0:8080->80/tcp   nginx
leoheng-MacBook-Pro:~ leo$ docker save -o nginx_leo_v1.tar  nginx
leoheng-MacBook-Pro:~ leo$ ls nginx_leo_v1.tar 
nginx_leo_v1.tar
leoheng-MacBook-Pro:~ leo$

6. import

命令解释:从归档文件中创建镜像
参数
-c :应用docker 指令创建镜像;
-m :提交时的说明文字;

leoheng-MacBook-Pro:~ leo$ docker import nginx_leo_v1.tar  leoheng/leo:v1
sha256:7afc0a247e015d0984f67a8b694c7c8e8363138c7ca6e4c3c69b583c2322774e
leoheng-MacBook-Pro:~ leo$

7. rmi

命令解释:删除本地一个或多个镜像
参数:
-f :强制删除;
--no-prune :不移除该镜像的过程镜像,默认移除;

leoheng-MacBook-Pro:~ leo$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
leoheng/leo         v1                  7afc0a247e01        About a minute ago   113MB
nginx               latest              568c4670fa80        10 days ago          109MB
leoheng-MacBook-Pro:~ leo$ docker rmi 7afc0a247e01
Untagged: leoheng/leo:v1
Deleted: sha256:7afc0a247e015d0984f67a8b694c7c8e8363138c7ca6e4c3c69b583c2322774e
Deleted: sha256:c0b54e54ff0bfd2330f0a541c02bf5706674cc3d2b0c7899457a5d543acaacff
leoheng-MacBook-Pro:~ leo$ 

六、 查看docker版本信息

1. info
命令解释:显示docker系统信息,包括镜像和容器数

leoheng-MacBook-Pro:~ leo$ docker info
Containers: 1
 Running: 1
 Paused: 0
 Stopped: 0
Images: 2
Server Version: 18.09.0
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.125-linuxkit
Operating System: Docker for Mac
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.952GiB
Name: linuxkit-025000000001
ID: 4CPI:PVLC:WBJL:QQPX:JP2X:B7RP:MGYE:SA7Y:ULC5:YOSL:RAYE:EMFD

2. version
命令解释:显示docker版本信息
参数:
-f :指定返回值的模板文件。

leoheng-MacBook-Pro:~ leo$ docker version 
Client: Docker Engine - Community
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:47:43 2018
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)+
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:55:00 2018
  OS/Arch:          linux/amd64
  Experimental:     false
leoheng-MacBook-Pro:~ leo$ 

入门:Docker常用命令及解释

标签:说明   repos   ada   sig   重启   process   fda   logout   ade   

原文地址:http://blog.51cto.com/leoheng/2327939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!