• Docker——常用命令


    1. 常用命令

      docker version    # 显示docker的版本信息
      docker info       # 显示docker的系统信息,包括镜像和容器的数量
      docker 命令 --help # 帮助命令,中间的"命令"替换为某个具体的指令,比如"image"
      
    2. 帮助文档地址

    3. 镜像命令

      • docker images:查看所有本地的主机上的镜像

        [root@iZwz908j8pbqd86doyrez5Z ~]# docker images
        REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
        hello-world         latest              bf756fb1ae65        8 months ago        13.3kB
        
        #解释:
        REPOSITORY 镜像的仓库源
        TAG 	   镜像的标签
        IMAGE ID   镜像的id
        CREATED    镜像的创建事件
        SIZE       镜像的大小
        
        #  可选项
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker images --help
        
        Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]
        
        List images
        
        Options:
          -a, --all             # 列出所有镜像
          -q, --quiet           # 只显示镜像的id
          
        #列出所有镜像的id
        docker images -aq
        
      • docker search:搜索镜像

        [root@iZwz908j8pbqd86doyrez5Z ~]# docker search mysql
        NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
        mysql                             MySQL is a widely used, open-source relation…   9945                [OK]                
        mariadb                           MariaDB is a community-developed fork of MyS…   3638                [OK]                
        
        Options:
          -f, --filter filter   Filter output based on conditions provided
        
        #只显示收藏数量在3000以上的
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker search mysql --filter=STARS=3000
        NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
        mysql               MySQL is a widely used, open-source relation…   9945                [OK]                
        mariadb             MariaDB is a community-developed fork of MyS…   3639                [OK] 
        

        注:这里的内容从本质上说和在hub.docker上搜索的结果是一样的

      • docker pull:下载镜像

        #下载镜像 docker pull 镜像名[:tag](tag是版本号)
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker pull mysql
        Using default tag: latest  默认是最新版的
        latest: Pulling from library/mysql
        bf5952930446: Pull complete  #分层下载,docker images核心 联合文件系统
        8254623a9871: Pull complete 
        938e3e06dac4: Pull complete 
        ea28ebf28884: Pull complete 
        f3cef38785c2: Pull complete 
        894f9792565a: Pull complete 
        1d8a57523420: Pull complete 
        6c676912929f: Pull complete 
        3cdd8ff735c9: Pull complete 
        4c70cbe51682: Pull complete 
        e21cf0cb4dc3: Pull complete 
        28c36cd3abcc: Pull complete 
        Digest: sha256:6ded54eb1e5d048d8310321ba7b92587e9eadc83b519165b70bbe47e4046e76a
        Status: Downloaded newer image for mysql:latest
        docker.io/library/mysql:latest  真实地址
        
        #由真实地址得出的两个等价命令
        docker pull mysql
        docker pull docker.io/library/mysql:latest
        
        #指定版本下载(版本可查看hub.docker.com)
        docker mysql:5.7
        

        注:可以用的版本号应该来自

      • docker rmi:删除镜像

        #根据id删除镜像
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker rmi -f 镜像id
        #根据id删除多个镜像
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker rmi -f 镜像id 镜像id
        #删除所有镜像
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker rmi -f $(docker images -aq)
        
    4. 容器命令

      说明:要现有一个镜像才能创建容器,这里以一个centos镜像测试学习

      docker pull centos
      
      • docker run[可选参数] image:新建容器并启动

        #参数说明
        --name="Name"     #给容器取名
        -d                #以后台方式运行,类似于nohup
        -it            	  #使用交互方式运行,进入容器查看内容
        -p                #指定容器的端口 -p 8080:8080
        -p                #指定随机端口
        
        #测试
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker run -it centos /bin/bash #以bash的形式运行指令
        [root@2dc2e3d5eb90 /]# ls  #由前缀可以看出已经进入了容器
        bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
        dev  home  lib64  media       opt  root  sbin  sys  usr
        [root@2dc2e3d5eb90 /]# exit  #退出容器的命令
        exit
        
      • docker ps:查看正在运行的容器

        #参数说明
        -a     #all,查看所有历史上运行过的容器
        -n=?   #显示最近创建的n个容器
        -q     #与docker images命令的附加-q参数相同,即只显示容器id
        
        #测试:
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps -a
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
        2dc2e3d5eb90        centos              "/bin/bash"         4 minutes ago       Exited (0) 3 minutes ago                       inspiring_gagarin
        5dcce85f2112        bf756fb1ae65        "/hello"            12 hours ago        Exited (0) 12 hours ago                        distracted_lederberg
        5adf71025107        bf756fb1ae65        "/hello"            13 hours ago        Exited (0) 13 hours ago                        busy_mahavira
        
      • 退出容器

        exit           #停止容器并退出
        ctrl + P + Q   #容器不停止退出
        
      • 删除容器

        docker rm 容器id                  #删除指定id的容器,且该容器必须为已经停止的容器
        docker rm -f 容器id               #删除指定id的容器,且该容器可以为已经运行的容器
        docker rm -f $(docker ps -aq)    #删除所有的容器
        docker ps -a -q|xargs docker rm  #删除所有的容器
        

        注:完全退出容器(如:使用exit命令)并不代表就是将这个容器删了,使用docker ps -aq命令还是可以查看到它们.想要真的删除还是要使用docker rm命令

      • 启动和停止容器的操作

        docker start 容器id    #启动容器
        docker restart 容器id  #重启容器
        docker stop 容器id     #停止当前正在运行的容器(让容器自己停止)
        docker kill 容器id     #强制停止当前容器(还有外部手段强制容器停止)
        
    5. 常用其它命令

      • docker run -d 镜像名:后台启动容器

        注:启动完成后,运行docker ps,会发现根本搜寻不到,因为容器启动后必须要有执行的任务,否则docker发现没有应用,就会自动停止

      • docker log:查看日志

        docker logs -f -t --tail 日志条数 容器id    #查看日志
        
        #测试
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker logs -f -t --tail 10 267946578eae
        2020-09-10T01:52:34.002346246Z [root@267946578eae /]# exit
        2020-09-10T01:52:34.002385790Z exit
        2020-09-10T01:53:35.193824155Z [root@267946578eae /]# exit
        
      • docker top 容器id:查看容器中进程信息(该容器需要为运行状态)

        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker start 267946578eae
        267946578eae
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
        267946578eae        centos              "/bin/bash"         28 minutes ago      Up 4 seconds                            zealous_hawking
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker top 267946578eae
        UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
        root                6854                6836                0                   10:20               pts/0               00:00:00            /bin/bash
        
      • docker inspect 容器id:查看元数据

        #测试
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker inspect 267946578eae
        [
            {
                "Id": "267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032", #id的全路径
                "Created": "2020-09-10T01:52:28.101148498Z",
                "Path": "/bin/bash",
                "Args": [],
                "State": {
                    "Status": "running",  #状态:正在运行
                    "Running": true,
                    "Paused": false,
                    "Restarting": false,
                    "OOMKilled": false,
                    "Dead": false,
                    "Pid": 6854,
                    "ExitCode": 0,
                    "Error": "",
                    "StartedAt": "2020-09-10T02:20:30.117511469Z",
                    "FinishedAt": "2020-09-10T01:53:35.203909156Z"
                },
                "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",  #镜像来源
                "ResolvConfPath": "/var/lib/docker/containers/267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032/resolv.conf",
                "HostnamePath": "/var/lib/docker/containers/267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032/hostname",
                "HostsPath": "/var/lib/docker/containers/267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032/hosts",
                "LogPath": "/var/lib/docker/containers/267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032/267946578eaefdc9065879219c72b656f3cf874cb1bc99af005e115597310032-json.log",
                "Name": "/zealous_hawking",
                "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,
                    "Capabilities": null,
                    "Dns": [],
                    "DnsOptions": [],
                    "DnsSearch": [],
                    "ExtraHosts": null,
                    "GroupAdd": null,
                    "IpcMode": "private",
                    "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,
                    "DeviceRequests": null,
                    "KernelMemory": 0,
                    "KernelMemoryTCP": 0,
                    "MemoryReservation": 0,
                    "MemorySwap": 0,
                    "MemorySwappiness": null,
                    "OomKillDisable": false,
                    "PidsLimit": null,
                    "Ulimits": null,
                    "CpuCount": 0,
                    "CpuPercent": 0,
                    "IOMaximumIOps": 0,
                    "IOMaximumBandwidth": 0,
                    "MaskedPaths": [
                        "/proc/asound",
                        "/proc/acpi",
                        "/proc/kcore",
                        "/proc/keys",
                        "/proc/latency_stats",
                        "/proc/timer_list",
                        "/proc/timer_stats",
                        "/proc/sched_debug",
                        "/proc/scsi",
                        "/sys/firmware"
                    ],
                    "ReadonlyPaths": [
                        "/proc/bus",
                        "/proc/fs",
                        "/proc/irq",
                        "/proc/sys",
                        "/proc/sysrq-trigger"
                    ]
                },
                "GraphDriver": {
                    "Data": {
                        "LowerDir": "/var/lib/docker/overlay2/2cfa27ccb7989cc146409f7d5c11960fb676fda394d1d7ca32019d3b9d560a8a-init/diff:/var/lib/docker/overlay2/fe1013e525ea3b1dae1b9f612d861ff90165ff5dbb377e81ec4758166f07f00a/diff",
                        "MergedDir": "/var/lib/docker/overlay2/2cfa27ccb7989cc146409f7d5c11960fb676fda394d1d7ca32019d3b9d560a8a/merged",
                        "UpperDir": "/var/lib/docker/overlay2/2cfa27ccb7989cc146409f7d5c11960fb676fda394d1d7ca32019d3b9d560a8a/diff",
                        "WorkDir": "/var/lib/docker/overlay2/2cfa27ccb7989cc146409f7d5c11960fb676fda394d1d7ca32019d3b9d560a8a/work"
                    },
                    "Name": "overlay2"
                },
                "Mounts": [],
                "Config": {
                    "Hostname": "267946578eae",
                    "Domainname": "",
                    "User": "",
                    "AttachStdin": true,
                    "AttachStdout": true,
                    "AttachStderr": true,
                    "Tty": true,
                    "OpenStdin": true,
                    "StdinOnce": true,
                    "Env": [
                        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
                    ],
                    "Cmd": [
                        "/bin/bash"
                    ],
                    "Image": "centos",
                    "Volumes": null,
                    "WorkingDir": "",
                    "Entrypoint": null,
                    "OnBuild": null,
                    "Labels": {
                        "org.label-schema.build-date": "20200809",
                        "org.label-schema.license": "GPLv2",
                        "org.label-schema.name": "CentOS Base Image",
                        "org.label-schema.schema-version": "1.0",
                        "org.label-schema.vendor": "CentOS"
                    }
                },
                "NetworkSettings": {
                    "Bridge": "",
                    "SandboxID": "351a0522bad7713d555a0fda889b3c43396a4f9aa49a794c2947401227a83823",
                    "HairpinMode": false,
                    "LinkLocalIPv6Address": "",
                    "LinkLocalIPv6PrefixLen": 0,
                    "Ports": {},
                    "SandboxKey": "/var/run/docker/netns/351a0522bad7",
                    "SecondaryIPAddresses": null,
                    "SecondaryIPv6Addresses": null,
                    "EndpointID": "399a6f23e15b8c7c6d198d85b9b2ea02ab92950aff1f726a63555b588289128b",
                    "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": "e33b6b9e8a86c82a5eeb03578b023317ed6aebed6d32dd8d04a5b138ffea5dfe",
                            "EndpointID": "399a6f23e15b8c7c6d198d85b9b2ea02ab92950aff1f726a63555b588289128b",
                            "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
                        }
                    }
                }
            }
        ]
        
      • 进入当前正在运行的容器

        #我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
        
        #方式1
        docker exec -it 容器id /bin/bash #以交互方式,通过bash/sh命令行进入
        
        #测试
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
        267946578eae        centos              "/bin/bash"         41 minutes ago      Up 13 minutes                           zealous_hawking
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker exec -it 267946578eae /bin/bash
        [root@267946578eae /]# ps -ef
        UID        PID  PPID  C STIME TTY          TIME CMD
        root         1     0  0 02:20 pts/0    00:00:00 /bin/bash
        root        14     0  0 02:34 pts/1    00:00:00 /bin/bash
        root        27    14  0 02:34 pts/1    00:00:00 ps -ef
        
        #方式二
        docker attach 容器id  #与第一种方式的区别,是进入了一个新的命令行,而不是开启了一个新的窗口
        
        #区别:
        #docker exec:相当于打开了一个新的终端
        #docker attach:进入容器正在执行的终端,且不会启动新的进程!!
        
      • 从容器内拷贝文件到主机上

        #将容器内的文件拷贝到主机
        docker cp 容器id:容器内路径 目的的主机路径
        
        #test
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker ps
        CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
        267946578eae        centos              "/bin/bash"         59 minutes ago      Up 31 minutes                           zealous_hawking
        [root@iZwz908j8pbqd86doyrez5Z ~]# docker attach 267946578eae
        [root@267946578eae /]# ls
        bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
        dev  home  lib64  media       opt  root  sbin  sys  usr
        [root@267946578eae /]# pwd
        /
        [root@267946578eae /]# touch test.java  #在容器中创建文件
        [root@267946578eae /]# ls
        bin  etc   lib	  lost+found  mnt  proc  run   srv  test.java  usr
        dev  home  lib64  media       opt  root  sbin  sys  tmp        var
        [root@267946578eae /]# exit
        exit
        [root@iZwz908j8pbqd86doyrez5Z ~]# ls
        aa.sh  file_clear.sh  for.sh  load_data.sh
        [root@iZwz908j8pbqd86doyrez5Z ~]# pwd
        /root
        [root@iZwz908j8pbqd86doyrez5Z ~]# mkdir test  #创建test文件夹,用来存放copy过来的文件
        [root@iZwz908j8pbqd86doyrez5Z ~]# ls
        aa.sh  file_clear.sh  for.sh  load_data.sh  test
        [root@iZwz908j8pbqd86doyrez5Z /]# docker cp 267946578eae:/test.java /root/test  #拷贝
        [root@iZwz908j8pbqd86doyrez5Z /]# cd /root/test
        [root@iZwz908j8pbqd86doyrez5Z test]# ls
        test.java
        
        #拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现自动同步
        
    6. 联系图

  • 相关阅读:
    ASP.NET Core多环境配置文件问题
    .NET Core中Object Pool的简单使用
    Refit在ASP.NET Core中的实践
    HttpClientFactory与Steeltoe结合来完成服务发现
    用HttpClientFactory来实现简单的熔断降级
    看看.NET Core几个Options的简单使用
    再探Circuit Breaker之使用Polly
    谈谈Circuit Breaker在.NET Core中的简单应用
    在.NET Core中使用简单的插件化机制
    谈谈ASP.NET Core中的ResponseCaching
  • 原文地址:https://www.cnblogs.com/Arno-vc/p/13649598.html
Copyright © 2020-2023  润新知