欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker常用命令

程序员文章站 2022-06-03 08:50:25
...


1. docker search [名称]    

    从仓库中查找镜像    eg: docker search jdk

Docker常用命令

NAME:镜像仓库名
DESCRIPTION:描述信息
STARS:和Github的Star一个道理
OFFICIAL:是否官方镜像
AUTOMATED:是否由Docker Hub自动构建

2. docker pull  [名称]    

从仓库中下载镜像    eg: docker pull docker.io/codenvy/jdk8_maven3_tomcat8

Docker常用命令

Docker常用命令

这样就下载完成了。

3. docker images

查看本地所有镜像   eg:  docker images

Docker常用命令

REPOSITORY:镜像仓库的名字
TAG:该镜像的版本号
IMAGE ID:镜像的ID
CREATED:创建时间
SIZE:镜像的大小

4. docker run --name 容器名 -i -t 镜像ID /bin/bash

该命令会基于指定镜像创建一个新的容器

–name:为容器设置一个名字 
-i -t:使得容器启动后能获取该容器的shell
镜像ID:指定容器要基于哪一个镜像运行
最后一个参数是运行时需要需要执行的命令,这里执行/bin/bash使得容器运行后启动一个bash供我们和容器交互。

eg:  docker run --name jdk8_maven3_tomcat8 -i -t 0cec97ba8d8a /bin/bash

Docker常用命令

        启动后我们将进入容器内部,获得一个用于和容器交互的shell,我们可以在这里对容器作任何操作,并且和操作一台Linux系统没有任何区别。

5. docker ps -a

查看已经创建的容器

Docker常用命令

-a:能查看所有已经被创建的容器;不加这个参数仅会显示正在运行的容器。 
CONTAINER ID:该容器的ID
IMAGE:该容器使用的镜像
COMMAND:启动时执行的命令
CREATED:容器创建时间
STATUS:当前运行状态
PORTS:该容器所开启的端口号
NAMES:容器的名字

6. docker start 容器名

启动已经创建的容器

该命令执行后容器会在后台运行,只返回一个容器名,若需要获取容器交互式shell,需要继续执行 7 命令。

7. docker attach 容器名    或者   docker exec -it 容器ID /bin/bash

进入容器内部,并获取了容器的shell

8. docker inspect 容器名

获取容器参数。

上述指令可以查看指定容器的所有配置信息,这些信息以JSON的形式返回,如下:

[
    {
        "Id": "67fe2d720f6f1b6302a8acf3f8ce0e3009913945a5627b1fde90b61c508bdf59",
        "Created": "2018-05-23T03:52:25.616205188Z",
        "Path": "/entrypoint.sh",
        "Args": [
            "/bin/bash"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4379,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-05-23T03:52:25.999074385Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:0cec97ba8d8ad572e2689428a149a2687a8df6b2177922fc3760f3a98b8c6965",
        "ResolvConfPath": "/var/lib/docker/containers/67fe2d720f6f1b6302a8acf3f8ce0e3009913945a5627b1fde90b61c508bdf59/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/67fe2d720f6f1b6302a8acf3f8ce0e3009913945a5627b1fde90b61c508bdf59/hostname",
        "HostsPath": "/var/lib/docker/containers/67fe2d720f6f1b6302a8acf3f8ce0e3009913945a5627b1fde90b61c508bdf59/hosts",
        "LogPath": "",
        "Name": "/jdk8_maven3_tomcat8",
        "RestartCount": 0,
        "Driver": "overlay2",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "journald",
                "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": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "docker-runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "overlay2",
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/486284930eca8852ea9ad8dcfa0b07483c137f427ae8cedcd7d31a904b19b33a-init/diff:/var/lib/docker/overlay2/3d6afb4850b759370f5ae8e1a2f405bafce4c354fa7d7e331a140d3ecaf1348e/diff:/var/lib/docker/overlay2/4ae6f57fa1a6b583a1aab61823af93bbf8c14018f74a8672230aae25ef39551a/diff:/var/lib/docker/overlay2/ca67f90056366dcf40e02e2c4d72bdf0d8c9c1fc189d780d9e80b214b169e3c7/diff:/var/lib/docker/overlay2/37c8cd72578613fdb1c01adc2e91e4f690e66df2bffe98ac6f5c55f18099f118/diff:/var/lib/docker/overlay2/d2efe9eddc27c3fb6661aab9282038eb8c6d0e8bc36e336ccaf148cdae34ea65/diff:/var/lib/docker/overlay2/a371e4dc8c9deff9640cee882de9385f32289f85e14036b2bdc49551d4d6250e/diff:/var/lib/docker/overlay2/9c52327a55f50872fb6401a75b7f92f21a13c979998b37f7b04ebd9e6d1f8661/diff:/var/lib/docker/overlay2/63e0818188fe71e9b31c4125cc399d7a9f716087940089e9c44ca4e672cc7390/diff:/var/lib/docker/overlay2/a83e7341adfa6588b05f5f5ad966da5c812a1cf6b178e45d5c7c9229afde95b3/diff:/var/lib/docker/overlay2/7ca8b47b752fd4fab3d200c4a7627afaa6c87aa13ecbb43a7d32240be4c23b2b/diff:/var/lib/docker/overlay2/f45ad142daa606d7912db1563e886e582fb7075bad2eeafbaba8b22798f99540/diff:/var/lib/docker/overlay2/7722e29b7c3a6289b948a408833a300f27062369594251aae9833e9624c10065/diff:/var/lib/docker/overlay2/30005d0cee0bbace7e2d639b38d82ee085f0f3e16ef8d2c739dfe8f2c8261daf/diff:/var/lib/docker/overlay2/aae2cdc8e34e18ff8ba89695755b04ecf86a49ffe36f7b7d2784a4e34494baa4/diff:/var/lib/docker/overlay2/387caa32b42ddf2c9cc8510d1b9fe4b7309436f92d2af5aac0d2f9717f2c9634/diff:/var/lib/docker/overlay2/414bdb726dcabe9fbb48e64c5eec601885fc9076d806c125a360a9473719bbbd/diff:/var/lib/docker/overlay2/8a5cf3a6695480523ff62a1f46128c7c77c86455560dbb25b19a20992a7fa41b/diff:/var/lib/docker/overlay2/9b06e2d2d989cf3ceb44d0ef4b89c35651d8c1a6254db8db833a5b4722464351/diff:/var/lib/docker/overlay2/a98ec24573ba91353ee6a109ecf35bed11eee5bd8c9406477d5350e1effafc6b/diff:/var/lib/docker/overlay2/2b56d81c0c1cae3b36ae06394c11a9975dfcc61b23d4ec97e8d40b3ade27dd12/diff:/var/lib/docker/overlay2/9bd5821a8fe01e6568a4094d484059afa6dd3214efea9bd81227996a216e7d39/diff:/var/lib/docker/overlay2/01570ae31d003cdd8e6b9d59d22d8880f49c8e6fd59d29eb0ee2f03f5dc0be69/diff:/var/lib/docker/overlay2/c15949c7f46c74160a8a14a7fa15b6adf408a57b2b626bb85330ce92465beab1/diff:/var/lib/docker/overlay2/e495d13c99f14b2fff650f7c923f872d417cef50ea9ec61e2ef9f18fe8b16eb7/diff:/var/lib/docker/overlay2/a1c2cdbe5c9c5dc7af9c6bf373487c62048f299c44185f2a281fa3080ba32cf3/diff:/var/lib/docker/overlay2/aaedbdc0cfb80232b12140c3b915cc08e3e710e5dc9f72639fda8bb0669ce443/diff:/var/lib/docker/overlay2/881064ac9d3e8e04d85b7c3f1045db466c2e1f3e6b6b15a3e08abc3da45905dd/diff",
                "MergedDir": "/var/lib/docker/overlay2/486284930eca8852ea9ad8dcfa0b07483c137f427ae8cedcd7d31a904b19b33a/merged",
                "UpperDir": "/var/lib/docker/overlay2/486284930eca8852ea9ad8dcfa0b07483c137f427ae8cedcd7d31a904b19b33a/diff",
                "WorkDir": "/var/lib/docker/overlay2/486284930eca8852ea9ad8dcfa0b07483c137f427ae8cedcd7d31a904b19b33a/work"
            }
        },
        "Mounts": [],
        "Config": {
            "Hostname": "67fe2d720f6f",
            "Domainname": "",
            "User": "user",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "4200/tcp": {},
                "8000/tcp": {},
                "8080/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/home/user/maven3/bin:/opt/jdk1.8.0_51/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "CODENVY_WEB_SHELL_PORT=4200",
                "JAVA_HOME=/opt/jdk1.8.0_51",
                "CODENVY_APP_PORT_8080_HTTP=8080",
                "M2_HOME=/home/user/maven3"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "0cec97ba8d8a",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "che:server:4200:protocol": "http",
                "che:server:4200:ref": "shellinabox",
                "che:server:8000:protocol": "http",
                "che:server:8000:ref": "tomcat8-debug",
                "che:server:8080:protocol": "http",
                "che:server:8080:ref": "tomcat8"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "c4e42629b087f14de15363746baeecb2fc4c239ca55979b2d26c1d21a42f55a4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "4200/tcp": null,
                "8000/tcp": null,
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/c4e42629b087",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "baa4476f70eb0bac1bab0a294468036ef3b3da414aee159dbe325fdda8487d21",
            "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": "dc0b3db21e9f247cf005f772fec875cc4eb81f6082dd6b03cfd37243b9b45f1e",
                    "EndpointID": "baa4476f70eb0bac1bab0a294468036ef3b3da414aee159dbe325fdda8487d21",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02"
                }
            }
        }
    }
]

可以使用-f或–format参数指定查看某一个参数:(建议使用)

docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名

9. 容器内执行命令

a.如果你已经进入容器内部,并获取了容器的shell,那么就执行执行命令即可;

b.如果容器在后台运行,那么你可以通过 docker exec 命令直接在宿主机上执行某个容器的命令

eg: docker exec [参数] 容器名 命令

上述命令会在指定的容器中执行指定的命令。 
如果你的命令无需交互,那么需要在参数部分加上-d,使得该命令在容器后台执行即可; 
如果你的命令在执行过程中需要用户输入,那么需要在参数部分加上-i -t,
并使用命令:/bin/bash,从而你就能进入容器内部,获取一个交互式的shell。

10. 停止容器

a. 当你在容器内部,只要执行exit命令即可停止并退出容器。

b.当容器处于后台运行的状态,你需要通过如下命令停止容器: docker stop 容器名

11. docker rm 容器名

 删除容器



转载请务必保留此出处(原作者):https://blog.csdn.net/zhuzhezhuzhe1/article/details/80417091


版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

https://blog.csdn.net/zhuzhezhuzhe1/article/details/80417091


相关标签: Docker IT linux