Docker常用命令

本文详细介绍了Docker的基础命令,包括查看Docker版本、系统信息、搜索镜像、下载镜像、删除镜像等操作。通过实例演示了如何拉取不同版本的MySQL镜像,并展示了`docker inspect`用于查看容器元数据的功能。此外,还讲解了如何在容器与主机间拷贝文件,并总结了Docker的关键操作。
摘要由CSDN通过智能技术生成

帮助命令

docker version   # 显示docker的版本信息
docker info      # 显示docker的系统信息,包括镜像和容器的数量
decker 命令 --help     # 帮助命令
帮助文档地址:https://docs.docker.com/engine/reference/commandline/docker/

镜像命令

docker images 查看所有本地的主机上的镜像
[root@iZbp1ce780a5j2zww8kl9hZ www]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 months ago   13.3kB

在这里插入图片描述

docker search 搜索镜像
[root@iZbp1ce780a5j2zww8kl9hZ www]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10940     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4137      [OK]  

在这里插入图片描述

docker pull 下载镜像
docker pull 镜像名[:版本号]
[root@iZbp1ce780a5j2zww8kl9hZ www]# docker pull mysql
Using default tag: latest               # 如果不写tag(版本号), 默认就是latest(最新的版本)
latest: Pulling from library/mysql
69692152171a: Pull complete             # 分层下载,docker iamge的核心联合文件系统
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969   # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest           # 真实地址

等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest

指定版本下载
[root@iZbp1ce780a5j2zww8kl9hZ www]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
a023ae82eef5: Pull complete
e76c35f20ee7: Pull complete
e887524d2ef9: Pull complete
ccb65627e1c3: Pull complete
Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

在这里插入图片描述
docker rmi 删除容器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@iZbp1ce780a5j2zww8kl9hZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@iZbp1ce780a5j2zww8kl9hZ /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
09faddea7de9   centos         "/bin/bash"   2 minutes ago   Exited (0) 2 minutes ago             gracious_engelbart
f0fbede8e423   d1165f221234   "/hello"      26 hours ago    Exited (0) 26 hours ago              dazzling_feynman

[root@iZbp1ce780a5j2zww8kl9hZ /]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
09faddea7de9   centos    "/bin/bash"   4 minutes ago   Exited (0) 3 minutes ago             gracious_engelbart

[root@iZbp1ce780a5j2zww8kl9hZ /]# docker ps -aq
09faddea7de9
f0fbede8e423

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
查看镜像的元数据

# 命令
docker inspect 容器id

# 测试
[root@iZbp1ce780a5j2zww8kl9hZ /]# docker inspect b92fcc5be694
[
    {
        "Id": "b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf",
        "Created": "2021-06-02T15:30:55.47507759Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 14800,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-06-02T15:30:55.783918494Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf/hostname",
        "HostsPath": "/var/lib/docker/containers/b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf/hosts",
        "LogPath": "/var/lib/docker/containers/b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf/b92fcc5be694430be203bb5f0aaab30c1c7bfe599834c764ed3f3625ecc9eccf-json.log",
        "Name": "/quizzical_burnell",
        "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",
                "/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/50a4c42976bdb0d079b781f96e3bae5b4befed082c71ec77f725543ffe48600a-init/diff:/var/lib/docker/overlay2/3b5419269ce53c2460ce4b47a59422ab1b744c955d9585a952d328596bf3baa3/diff",
                "MergedDir": "/var/lib/docker/overlay2/50a4c42976bdb0d079b781f96e3bae5b4befed082c71ec77f725543ffe48600a/merged",
                "UpperDir": "/var/lib/docker/overlay2/50a4c42976bdb0d079b781f96e3bae5b4befed082c71ec77f725543ffe48600a/diff",
                "WorkDir": "/var/lib/docker/overlay2/50a4c42976bdb0d079b781f96e3bae5b4befed082c71ec77f725543ffe48600a/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "b92fcc5be694",
            "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": "20201204",
                "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": "c94d9f057844953faf56cab3a3827a1b3aa92cdad41bee76c2a246ebd636db78",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/c94d9f057844",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "79cf65f30ec837e53458901f637fe930fec520279cde5bd7c6e150995dfc4041",
            "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": "40685a4b33e8d3d586cb21686cf52b5d1701800de40b94af8304e93ab8444015",
                    "EndpointID": "79cf65f30ec837e53458901f637fe930fec520279cde5bd7c6e150995dfc4041",
                    "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
                }
            }
        }
    }
]

在这里插入图片描述在这里插入图片描述
从容器拷贝东西到主机上

docker 测评容器id:容器内路径 目的的主机路径

测试
查看当前主机下
[root@iZbp1ce780a5j2zww8kl9hZ home]# ls
lhf.java
[root@iZbp1ce780a5j2zww8kl9hZ home]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
b92fcc5be694   centos    "/bin/bash"   25 minutes ago   Up 25 minutes             quizzical_burnell

# 进入docker容器内部
[root@iZbp1ce780a5j2zww8kl9hZ home]# docker attach b92fcc5be694
[root@b92fcc5be694 /]# cd /home
[root@b92fcc5be694 home]# ls
# 在容器内新建一个文件
[root@b92fcc5be694 home]# touch test.java
[root@b92fcc5be694 home]# ls
test.java
[root@b92fcc5be694 home]# exit
exit
[root@iZbp1ce780a5j2zww8kl9hZ home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZbp1ce780a5j2zww8kl9hZ home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
b92fcc5be694   centos    "/bin/bash"   26 minutes ago   Exited (0) 25 seconds ago             quizzical_burnell
# 将这文件拷贝出来到主机上
[root@iZbp1ce780a5j2zww8kl9hZ home]# docker cp b92fcc5be694:home/test.java /home
[root@iZbp1ce780a5j2zww8kl9hZ home]# ls
lhf.java  test.java

# 拷贝是一个手动过程,之后可以使用 -v 卷的技术可以实现自动同步 /home /home

小结
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值