当前位置 博文首页 > 范栩的博客:Docker的基本命令、小练习、docker可视化以及commit

    范栩的博客:Docker的基本命令、小练习、docker可视化以及commit

    作者:[db:作者] 时间:2021-08-31 09:45

    Docker的基本命令、小练习、docker可视化、commit镜像

    镜像命令

    [root@VM-8-14-centos docker]# docker images
    # 解释
    REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
    hello-world   latest    d1165f221234   5 months ago   13.3kB
    REPOSITORY	镜像的仓库源
    TAG			镜像的标签
    IMAGE ID 	镜像的id
    CREATED		镜像的创建时间
    SIZE		镜像的大小
    # 可选项
    -a, --all	#列出所有的命令
    -q,--quiet #只显示id
    

    docker search 搜索镜像

    [root@VM-8-14-centos docker]# docker search mysql
    NAME                         DESCRIPTION                                     STARS    
    mysql                        MySQL is a widely used, open-source relation…   11294         
    mariadb                      MariaDB Server is a high performing open sou…   4289           
    mysql/mysql-server           Optimized MySQL Server Docker images. Create…   837           
    #可选项  通过收藏来过滤
    --filter=STARS=3000		搜索出来的镜像就是STARS数量大于3000的
    

    docker pull 下载镜像

    [root@VM-8-14-centos docker]# docker pull mysql
    Using default tag: latest			#没写tag的话,默认是使用最新版本latest
    latest: Pulling from library/mysql
    e1acddbe380c: Pulling fs layer 		#分层下载,docker的核心,联合文件系统
    bed879327370: Pulling fs layer 
    03285f80bafd: Pulling fs layer 
    ccc17412a00a: Pull complete 
    1f556ecc09d1: Pull complete 
    adc5528e468d: Pull complete 
    1afc286d5d53: Pull complete 
    6c724a59adff: Pull complete 
    0f2345f8b0a3: Pull complete 
    c8461a25b23b: Pull complete 
    3adb49279bed: Pull complete 
    77f22cd6c363: Pull complete 
    Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926	#签名
    Status: Downloaded newer image for mysql:latest	
    docker.io/library/mysql:latest		#真实地址
    
    #docker pull mysql 等价于 docker pull docker.io/library/mysql:latest
    
    
    #指定版本下载
    [root@VM-8-14-centos docker]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    e1acddbe380c: Already exists 	#已经有了的就不会进行下载
    bed879327370: Already exists 	#这样更节省内存
    03285f80bafd: Already exists 
    ccc17412a00a: Already exists 
    1f556ecc09d1: Already exists 
    adc5528e468d: Already exists 
    1afc286d5d53: Already exists 
    4d2d9261e3ad: Pull complete 	#只下载了四个层级
    ac609d7b31f8: Pull complete 
    53ee1339bc3a: Pull complete 
    b0c0a831a707: Pull complete 
    Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7
    
    

    docker 删除镜像

    #通过id删除一个镜像,也可以通过名字删除
    [root@VM-8-14-centos docker]# docker rmi -f 6c20ffa54f86
    Untagged: mysql:5.7
    Untagged: mysql@sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
    Deleted: sha256:6c20ffa54f8674203e91e3225e489aa505fa04b8d482954a8b6d7414842c6de4
    Deleted: sha256:7ecf077dd9d6725f0e594b9cd5eb214ad1b3764fa75bf5940ab08b05b8bae184
    Deleted: sha256:c28edab85681de37f9d2a75dde941a20d2e1ac6e4efb7dee391913d3ec722fef
    Deleted: sha256:6c11b187da1ecb65ce10b25f54354f3515ce9d26fabcd859cdb43dbd625df907
    Deleted: sha256:d0dfc5be0883ed4af9f642a8e4f2312a93c4241ec8e89f22219b0f0a0d26c8d3
    
    
    #删除所有的docker镜像
    [root@VM-8-14-centos docker]# docker rmi -f $(docker images -aq)
    Untagged: mysql:latest
    Untagged: mysql@sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926
    Deleted: sha256:5a4e492065c722ec8cc7413552bafc6fd5434c5ad90797e898ccc4e347e21aa5
    Deleted: sha256:816bf92c28337cd97393cb1fab7697398c9b657baa8e80ad394e1193bdd7c323
    Deleted: sha256:4d9cad11f64589b397b1033d6f14cea1794efbee0a9923932fb3394a67cb9812
    Deleted: sha256:d8cc28a31b469770ffe2239a29aaef8745fedc4df3c545b1615ba864796709a3
    Deleted: sha256:c65a377e07f1205f3c28f7067d2ac4da3a65fbe7c1b4606b48028db3818d2f4c
    Deleted: sha256:7dcd840f1d4be1a565f470453489932a17c62ed36b959d7839f004606ba22f0b
    Deleted: sha256:1438a7497295aed687ea1875073a1f9faa6a308f787767b952da4a1a21e2c2c6
    Deleted: sha256:c728863b4742db55b9630b0129abac718c8c9704e5ed86e8af93dfb47a0f6214
    Deleted: sha256:0f12df3ff796b6fb619c63bf2f78f16d1033746e19f75ffc2d8d72ea69242ad4
    Deleted: sha256:3072318e40ebff019298a59290e615ad6170a651f3121c0e356dcf3dd7426136
    Deleted: sha256:a69e3133dd3c0a81bd439120abb8f98e43c1581629896725b968db906c8fdfac
    Deleted: sha256:6d9498fd63c38c4b0b9bbc563087b4bc164b2fb5211832a8d43c60a9692a7f84
    Deleted: sha256:f68ef921efae588b3dd5cc466a1ca9c94c24785f1fa9420bea15ecc2dedbe781
    Untagged: hello-world:latest
    Untagged: hello-world@sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
    Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
    
    #查看剩余的docker镜像
    [root@VM-8-14-centos docker]# docker images
    REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
    
    

    容器命令

    说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

    docker pull centos
    

    新建容器并启动

    docker run [可选参数] image
    
    #参数说明
    --name="Name" 	容器名字	tomcat1 tomcat2 用于区分容器
    -d				后台方式运行
    -it	 			使用交互方式运行,进入容器查看内容
    -p				指定容器的端口 -p 8080:8080
    			-p ip:主机端口:容器端口
    			-p 主机端口:容器端口(常用)
    			-p 容器端口
    			容器端口
    #测试		启动并进入容器
    [root@VM-8-14-centos docker]# docker run -it centos /bin/bash
    [root@ac71b5898083 /]# 
    
    [root@ac71b5898083 /]# ls	#查看容器内的centos,基础版本,很多命令都不完善
    bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    
    #退出容器命令
    [root@9af1a7b253ec /]# exit
    exit
    [root@VM-8-14-centos /]# 
    
    

    列出所有运行的容器

    #docker ps 命令
    		#列出当前正在运行的容器
    -a		#列出当前正在运行的容器+历史运行过的容器
    -n=?	#显示最近创建的容器
    -q		#只显示容器的编号
    
    [root@VM-8-14-centos /]# docker ps  #运行的容器
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    [root@VM-8-14-centos /]# docker ps -a   #运行过的容器
    CONTAINER ID   IMAGE          COMMAND       CREATED              STATUS                          PORTS     NAMES
    9af1a7b253ec   centos         "/bin/bash"   About a minute ago   Exited (0) About a minute ago             happy_nash
    ac71b5898083   centos         "/bin/bash"   3 minutes ago        Exited (0) 2 minutes ago                  sleepy_sutherland
    e1c59d0e993b   d1165f221234   "/hello"      24 hours ago         Exited (0) 24 hours ago                   quizzical_albattani
    [root@VM-8-14-centos /]# 
    
    

    退出容器

    exit	#容器直接停止退出
    Ctrl+P+Q #不停止退出
    

    删除容器

    docker rm 容器id						#删除指定的容器,不能删除正在运行的容器,如果要强制删除就rm -f
    docker rm -f $(docker ps -aq)		 #删除所有的容器	
    docker ps -a -q|xargs docker rm		 #删除所有的容器
    

    启动和停止容器的操作

    docker start 容器id		#启动容器
    docker restart 容器id		#重启容器
    docker stop 容器id		#停止当前正在运行的容器
    docker kill 容器id		#强制停止当前容器
    

    常用其他命令

    后台启动容器

    #命令docker run -d 镜像名!
    [root@VM-8-14-centos /]# docker run -d centos
    #问题 docker ps 发现centos 停止了
    #docker容器使用后台运行,就必须要有一个前台进程,否则docker发现没有应用,就会自动停止
    #例如nginx,容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
    

    查看日志

    [root@VM-8-14-centos /]# docker run -d centos /bin/bash -c "while true;do echo fanxu;sleep 1;done" 
    88e36a8819f26f5d12a8fd658adfbb97ed60901a71a8d878a0d8174b98b72dd7
    
    
    [root@VM-8-14-centos /]# docker logs -tf --tail 10 88e36a8819f2
    2021-08-18T14:17:58.098503211Z fanxu
    2021-08-18T14:17:59.100849355Z fanxu
    2021-08-18T14:18:00.103158743Z fanxu
    2021-08-18T14:18:01.105462026Z fanxu
    2021-08-18T14:18:02.107828632Z fanxu
    
    

    查看容器中进程信息

    [root@VM-8-14-centos /]# docker top 81d323846d11
    UID       PID       PPID      C         STIME     TTY            TIME         CMD
    root      20586     20568     0         22:20     pts/0          00:00:00     /bin/bash
    

    查看镜像的源数据

    #命令
    docker inspect 容器id
    
    [root@VM-8-14-centos /]# docker inspect 81d323846d11
    [
        {
            "Id": "81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02",
            "Created": "2021-08-18T14:20:55.98150614Z",
            "Path": "/bin/bash",
            "Args": [],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 21525,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2021-08-18T14:24:47.97344561Z",
                "FinishedAt": "2021-08-18T14:21:26.415512597Z"
            },
            "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
            "ResolvConfPath": "/var/lib/docker/containers/81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02/resolv.conf",
            "HostnamePath": "/var/lib/docker/containers/81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02/hostname",
            "HostsPath": "/var/lib/docker/containers/81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02/hosts",
            "LogPath": "/var/lib/docker/containers/81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02/81d323846d11ab383afd98a34aef5feff5fd2646865a0023e04be4967e712c02-json.log",
            "Name": "/sad_dewdney",
            "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,
                "CgroupnsMode": "host",
                "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"