docker命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker命令相关的知识,希望对你有一定的参考价值。

获取镜像:

docker pull NAME[:TAG]

例如:

docker pull redis

默认是拉取最新版本的ubuntu镜像,也可以用TAG来拉取指定版本的镜像,例如:

docker pull redis:3.2


查看本地镜像

docker images
dockerREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis                     3.2                 e97b1f10d81a        4 weeks ago         99.7MB
ubuntu                    latest              452a96d81c30        5 weeks ago         79.6MB

第一个字段是指镜像来自于哪个仓库,

TAG:标签信息,通常用版本来定义

IMAGE ID:镜像唯一ID号

CREATED:镜像创建时间

SIZE:镜像大小


查看镜像详细信息

docker inspect image_id


例如:

docker inspect e97b1f10d81a
docker[
    {
        "Id": "sha256:e97b1f10d81a1da230e6327f5d7a13b1e5b3716bb979cdd583c608ecae88adef",
        "RepoTags": [
            "redis:3.2"
        ],
        "RepoDigests": [
            "[email protected]:08d54e5c3ef779601f8db9b8fa3c67dab235559501df2b3d7b29ad088cffd02a"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2018-05-02T04:58:15.600462978Z",
        "Container": "1865914dcaf5fd0bf73d468197394e446ee195366474ba021ad3e05c63e42c6f",
        "ContainerConfig": {
            "Hostname": "1865914dcaf5",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "6379/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "REDIS_VERSION=3.2.11",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.2.11.tar.gz",
                "REDIS_DOWNLOAD_SHA=31ae927cab09f90c9ca5954aab7aeecc3bb4da6087d3d12ba0a929ceb54081b5"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"redis-server\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e492993dac9b9b56a0138584a82120321a781a90dfd6ea7d4be9796befc5fd3d",
            "Volumes": {
                "/data": {}
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "6379/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "REDIS_VERSION=3.2.11",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.2.11.tar.gz",
                "REDIS_DOWNLOAD_SHA=31ae927cab09f90c9ca5954aab7aeecc3bb4da6087d3d12ba0a929ceb54081b5"
            ],
            "Cmd": [
                "redis-server"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:e492993dac9b9b56a0138584a82120321a781a90dfd6ea7d4be9796befc5fd3d",
            "Volumes": {
                "/data": {}
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": [],
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 99713084,
        "VirtualSize": 99713084,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/a1d2d89c4c73c7f99eb919514f08e1b6d1ae3029067088fe7a3d928145bb5c81/diff:/var/lib/docker/overlay2/64268fa606b7c2fc67618349903f041022294862fca78f85b2e86043d605f5f9/diff:/var/lib/docker/overlay2/93464e9ab4ab7ab0157e0b624650ab8bea63d945ea0f45b9d2cb3e442a88a6c0/diff:/var/lib/docker/overlay2/94498d8f8d29f86637266b84de944dc190a5f35d4cfd301c3ba928777c765fda/diff:/var/lib/docker/overlay2/4da4c723380aa1501f962009fc737489eeb14335d68f7d265b65049246b0f24c/diff",
                "MergedDir": "/var/lib/docker/overlay2/72fe95c14d19dee7469c7312191060bc7af8820ba3cb71cf7ff85ac36794925e/merged",
                "UpperDir": "/var/lib/docker/overlay2/72fe95c14d19dee7469c7312191060bc7af8820ba3cb71cf7ff85ac36794925e/diff",
                "WorkDir": "/var/lib/docker/overlay2/72fe95c14d19dee7469c7312191060bc7af8820ba3cb71cf7ff85ac36794925e/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:ba291263b0854589e32a6fa7775c898d662ed835cd686ac9ac2d33dcefa91a39",
                "sha256:3d00edfc21708104f503f45fc30f3584b85654d93cb815d5cf155159499e4ab3",
                "sha256:e9a42011bbb5977e682dffdd8dcda960434e21dcc5fabc3de74355946bd5e109",
                "sha256:7a3bb4a3a1c4799c47827ea6e52734a4ec85c6694dda2762edb05a620ff63cef",
                "sha256:848db56a6bea48c0267a336d3aae69665421dcdbd7db98869894bcb6877c5d07",
                "sha256:75c6ebaf02dfc37a49055d355b1ee5a61059410a183c9f432d2912674f3954c5"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]


查看某一项内容:

docker inspect -f {{".Created"}} e97b1f10d81a
docker2018-05-02T04:58:15.600462978Z


搜索镜像:默认是从官方的源中搜索镜像信息

docker search TERM

例如:

 ~ docker search mysql
dockerNAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   6327                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   1987                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   453                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   342                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       99                                      [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          85                                      
centurylink/mysql                                      Image containing mysql. Optimized to be link…   60                                      [OK]
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   52                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          35                                      [OK]
tutum/mysql                                            Base docker image to run a MySQL database se…   32                                      
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   28                                      
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   25                                      
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   19                                      [OK]
bitnami/mysql                                          Bitnami MySQL Docker Image                      15                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   14                                      
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        13                                      [OK]
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   8                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
circleci/mysql                                         MySQL is a widely used, open-source relation…   5                                       
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                3                                       [OK]
mysql/mysql-router                                     MySQL Router provides transparent routing be…   2                                       
openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openzip…   1                                       
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0

星级越高,排名越靠前,official字段为OK的,表示为官方镜像。


删除镜像

docker rmi NAME:TAG

例如:

dockerUntagged: redis:3.2
Untagged: [email protected]:08d54e5c3ef779601f8db9b8fa3c67dab235559501df2b3d7b29ad088cffd02a
Deleted: sha256:e97b1f10d81a1da230e6327f5d7a13b1e5b3716bb979cdd583c608ecae88adef
Deleted: sha256:2ba10eb7ce40ff2fb8240b89efdfcaca65e766e322dd931811a70c089ce2524f
Deleted: sha256:3ca88aa54c7634d16d8f6717aebdcef639cead694ced3c393b237117a5e1e30f
Deleted: sha256:f115d9da9e55bc62cf359227dd64edc01911f7b1ed52afbffd7b00a2e6d5d102
Deleted: sha256:ba4122a82d251fc6b432237079bbb0d645ef47049e800c1f9e56d61fc8e823cf
Deleted: sha256:2425077af8e28faee2557ddabb85a00938d81d13cf8c41897958d07aaeaa39d2
Deleted: sha256:ba291263b0854589e32a6fa7775c898d662ed835cd686ac9ac2d33dcefa91a39
docker images 
dockerREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu                  latest                452a96d81c30        5 weeks ago         79.6MB

注意:当一个镜像有多个标签时,删除镜像只是删除了该镜像对应的标签,并不影响镜像文件,如果只剩下一个标签,则会彻底删除镜像文件本身,如上面的例子

如果使用镜像ID删除,则会先尝试删除该镜像所有的TAG,然后再彻底删除镜像文件本身


使用镜像启动一个容器:

docker run -itd ubuntu /bin/bash


查看本机上存在的容器:

 docker ps -a
dockerCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
3b00082711a9        ubuntu              "/bin/bash"         21 hours ago        Exited (255) 20 hours ago                       nostalgic_minsky
969a66036c68        ubuntu              "/bin/bash"         21 hours ago        Exited (127) 21 hours ago                       epic_banach

  

当镜像创建了容器后,删除时会报错,

docker rmi ubuntu               
dockerError response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container 3b00082711a9 is using its referenced image 452a96d81c30

这时候只能使用“-f“强制删除

docker rmi -f ubuntu
dockerUntagged: ubuntu:latest
Untagged: [email protected]:c8c275751219dadad8fa56b3ac41ca6cb22219ff117ca98fe82b42f24e1ba64e
Deleted: sha256:452a96d81c30a1e426bc250428263ac9ca3f47c9bf086f876d11cb39cf57aeec

docker images
dockerREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE







以上是关于docker命令的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段——cli的终端命令大全

VSCode自定义代码片段4——cli的终端命令大全

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

markdown docker-compose片段