docker 的基本命令

帮助命令

  • docker version
    • 显示docker的版本信息
  • docker info
    • 显示docker的详细信息
  • docker 命令 --help
    • 查看命令的帮助文档

镜像命令

  • docker images
    • 查看本地的所有镜像
    • 参数
      • -a 显示所有镜像
      • -q 只显示id
  • docker search
    • 搜索镜像,也可以去docker hub 去搜索
    • 参数
      • –filter
        • docker search mysql --filter=STARS=3000 只搜索3000以上stars的
  • docker pull 镜像
    • 下载镜像,后面可以跟上版本,docker pull 镜像:tag,如果不写默认是latest,也就是最新的
    • 分层下载,docker image的核心 联合文件系统,这几个概念之后讲
  • docker rmi 镜像id
    • 删除镜像,rm就是删除 就是image的意思
    • 删除所有镜像
      • docker rmi $(docker images -aq)

容器命令

  • docker run [可选参数] image
    • 启动容器
    • 参数
      • –name=“name” 容器名称
      • -d 以后台方式运行,类似nohub
      • -it 使用交互方式运行,进入容器查看内容
      • -p 指定容器的端口
        • -p ip:主机端口:容器端口
        • -p 主机端口:容器端口(常用)
        • -p 容器端口
      • -P 随机指定端口
    • 测试
# 以交互方式进入容器
fujunhua@fujunhuadeMacBook-Air ~ % docker run -it centos /bin/bash
[root@c76e60706682 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@c76e60706682 /]#
  • exit
    • 退出容器命令
    • 退出不停止 : ctrl + p +q
  • docker ps
    • 查看运行的容器
    • -a 参数可以查看之前运行过的历史记录+当前所有运行的容器
    • -n 显示最近创建的容器 -n=1 只显示最新的一个
    • -q 显示容器编号
  • docker rm 容器id
    • 删除全部容器 docker rm -f $(docker ps -aq)
    • 注意 : 如果要删除正在运行的容器,就要加上参数-f
  • docker start 容器id
    • 启动容器
  • docker restart 容器id
    • 重启容器
  • docker stop 容器id
    • 停止正在运行的id
  • docker kill 容器id
    • 强制停止容器

常用的其他命令

  • docker logs [可选参数] 容器id
    • 查看日志
    • -tf 追踪数据并显示时间戳
    • –tail 条数 显示尾部的多少条数据
  • docker top 容器id
    • 查看容器内部的进程信息
  • 进入正在运行的容器
    • 方式一
      • docker exec 容器id
    • 方式二
      • docker attach 容器id
    • 区别
      • 方式一会新开一个客户端,二方式二会进入当前正在执行的终端中
  • 从容器内拷贝文件到本主机上
    • docker cp 容器di:路径 主机路径
  • docker inspect 容器id
    • 查看容器的元数据
[
    {
        "Id": "1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4",
        "Created": "2022-01-16T07:53:58.7423733Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo hello;sleep 1;done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2022-01-16T07:53:59.2984293Z",
            "FinishedAt": "2022-01-16T07:55:13.3905686Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4/hostname",
        "HostsPath": "/var/lib/docker/containers/1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4/hosts",
        "LogPath": "/var/lib/docker/containers/1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4/1034277f7d1e825bbed15173a441ba63f3bfcacc9263be3c793955ba1bdfbff4-json.log",
        "Name": "/condescending_wu",
        "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": "private",
            "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": null,
            "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/532d282bceb5837f369d07e93049428b6186522d7fbcc14fcd484e628bf32eac-init/diff:/var/lib/docker/overlay2/e02ce3392809941fc8f32a20554f285a62341cb1c417b3691f9fa3fef4fbe6bf/diff",
                "MergedDir": "/var/lib/docker/overlay2/532d282bceb5837f369d07e93049428b6186522d7fbcc14fcd484e628bf32eac/merged",
                "UpperDir": "/var/lib/docker/overlay2/532d282bceb5837f369d07e93049428b6186522d7fbcc14fcd484e628bf32eac/diff",
                "WorkDir": "/var/lib/docker/overlay2/532d282bceb5837f369d07e93049428b6186522d7fbcc14fcd484e628bf32eac/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "1034277f7d1e",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash",
                "-c",
                "while true;do echo hello;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "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": "8972234fb6a32e394f62df5fa6f360e7fde0ef8dc16604e3c3669adc67329d78",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/8972234fb6a3",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "92c36ac958105306e65d52f80c779b6b856913108b786248f0c97a4750ccab9f",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

总结

![image.png](https://www.icode9.com/i/ll/?i=img_convert/a2af10834f3294af389e878c98c54ce1.png#clientId=uf85421a1-f985-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=557&id=u54549c33&margin=[object Object]&name=image.png&originHeight=557&originWidth=777&originalType=binary&ratio=1&rotation=0&showTitle=false&size=313971&status=done&style=none&taskId=uda0277ff-e1c2-48fa-9590-0fca9ac46c4&title=&width=777)

上一篇:java web学习总结(二十三) -------------------编写自己的JDBC框架


下一篇:Spring+Hibernate+Struts(SSH)框架整合