Docker常用命令

一、常用命令

  • docker -version:版本号
  • docker info:详细信息,镜像和容器
  • docker 命令 --help:帮助命令

镜像命令

  • docker imgaes:查看镜像(-a:全部、-f:过滤、-q:只显示ID)
  • docker search:搜索镜像 docker search mysql --filter=STARS=9000
  • docker pull:下载镜像 docker pull mysql:5.7Docker常用命令_第1张图片
  • docker rmi:删除镜像。docker rmi -f $(docker images -aq)

二、容器命令

说明:有了镜像才会有容器

1、下载容器

docker pull centos

2、新建容器并启动

docker run [参数] image

  • --name="Name":容器名称,用来区分容器
  • -d:后台运行
  • -it:使用交互方式运行,可以进入容器内部
  • -p:制定容器的端口 8080:8081,跟主机映射起来
  • -P:随机指定端口
    • 主机端口:容器端口(常用的)
    • 容器端口
    • 主机端口
    • ip:主机端口:容器端口

启动并进入容器,基础镜像很多命令都完善的

Docker常用命令_第2张图片

退出:exit直接容器停止并退出、ctrl+p+q容器不停止退出

3、查看启动的容器

docker ps

  • 列出正在运行的容器
  • -a:列出全部的容器,包含历史容器
  • -n=?:显示最近创建的容器
  • -q:只显示容器的编号

 4、删除容器

docker rm 容器ID:删除指定的容器,不能删除正在运行的容器,需要加 -f 参数

docker rm -rf $(docker ps -aq):删除全部容器

docker ps -a -q | xargs docker rm:删除全部容器

5、启动和停止容器的操作

docker start 容器ID:启动

docker restart 容器ID:重启

docker stop 容器ID:停止

docker kill 容器:强制停止

三、常用的其他命令

docker run -d 镜像名:发现停止了,docker容器使后台运行,必须要有一个前台进程,docker发现没有应用了,就会自动停止容器,例如安装nginx,容器启动后发现没有提供服务,就会自己停止

1、查看日志命令

docker logs -f -t -n --tail 容器IDDocker常用命令_第3张图片

2、查看容器中的进程信息

docker top 容器ID

3、查看镜像的元数据

docker inspect 容器ID

[
    {
        "Id": "839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c",
        "Created": "2021-12-11T06:42:41.384471712Z",
        "Path": "/entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2241,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-12-16T11:30:35.321792888Z",
            "FinishedAt": "2021-12-13T14:08:12.220366833Z",
            "Health": {
                "Status": "healthy",
                "FailingStreak": 0,
                "Log": [
                    {
                        "Start": "2021-12-16T14:46:37.348469638Z",
                        "End": "2021-12-16T14:46:37.416738721Z",
                        "ExitCode": 0,
                        "Output": "mysqld is alive\n"
                    },
                    {
                        "Start": "2021-12-16T14:47:07.497470263Z",
                        "End": "2021-12-16T14:47:07.56773618Z",
                        "ExitCode": 0,
                        "Output": "mysqld is alive\n"
                    },
                    {
                        "Start": "2021-12-16T14:47:37.65155168Z",
                        "End": "2021-12-16T14:47:37.710534305Z",
                        "ExitCode": 0,
                        "Output": "mysqld is alive\n"
                    },
                    {
                        "Start": "2021-12-16T14:48:07.793479013Z",
                        "End": "2021-12-16T14:48:07.859810096Z",
                        "ExitCode": 0,
                        "Output": "mysqld is alive\n"
                    },
                    {
                        "Start": "2021-12-16T14:48:37.935498722Z",
                        "End": "2021-12-16T14:48:38.002074013Z",
                        "ExitCode": 0,
                        "Output": "mysqld is alive\n"
                    }
                ]
            }
        },
        "Image": "sha256:087c6f86492cf3e450b2a029c0155c46fca92244037affefa19ae62feda13f79",
        "ResolvConfPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/hostname",
        "HostsPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/hosts",
        "LogPath": "/var/lib/docker/containers/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c/839732d7b8242996992e80f0d09657d125e85ad13c0fc65ce5219301f127405c-json.log",
        "Name": "/mysql",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3306/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3306"
                    }
                ]
            },
            "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/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec-init/diff:/var/lib/docker/overlay2/b622e2d9709a3a1e87c183789a1a86e023887a94bc5b4855ad2d425b7304bbc9/diff:/var/lib/docker/overlay2/07b950211f35d22dae0b191e9fba485cd0f12ff8bf68f6244c1c44988574117c/diff:/var/lib/docker/overlay2/de1a9d1f7eb9a52d802ffbca9ff580bd2297e0532dae3e45f4cb6ee1daa1d8cc/diff:/var/lib/docker/overlay2/91434ddbdd4de70e58d68f514a2d10bc162f31ef5230f206bb64cb179c0e0897/diff:/var/lib/docker/overlay2/af86b037eab2a9dc213b95ace81b6cd4be5e13c70723beef16b73dc7e668815c/diff:/var/lib/docker/overlay2/40c739f7875bb531a9929cffa3d125c01edca633ab706052334e270bba012f16/diff:/var/lib/docker/overlay2/afdd3b6330502077f613c286af31c159c1db383700981ba8a6fbd401f456424d/diff",
                "MergedDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/merged",
                "UpperDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/diff",
                "WorkDir": "/var/lib/docker/overlay2/43b4c46a6e7f9d96af29faac02322429b3060df6359c80d4e626197cbc736eec/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "839732d7b824",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {},
                "33061/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=123456",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "MYSQL_UNIX_PORT=/var/lib/mysql/mysql.sock"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Healthcheck": {
                "Test": [
                    "CMD-SHELL",
                    "/healthcheck.sh"
                ]
            },
            "Image": "mysql/mysql-server",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "/entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "aec0d51e01996e9eb0452f8897086e593a7589d0408876be85e42a5d2b1f4e79",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3306"
                    }
                ],
                "33060/tcp": null,
                "33061/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/aec0d51e0199",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "8537b8f765850be343d8bb1f933972c7617ced81851ae5b0d2c924474ffd2b5f",
            "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": "29ba535e1950ba283d300a1879c4279b2c1f672a203ae07864fc257aa4fbee1d",
                    "EndpointID": "8537b8f765850be343d8bb1f933972c7617ced81851ae5b0d2c924474ffd2b5f",
                    "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
                }
            }
        }
    }
]

3、进入当前容器

需要进入容器修改配置

docker exec -it 容器ID bashShell

docker  attach 容器ID :正在执行当前的代码

exec 进入容器后开启一个新的终端,可以在里面操作,想进入容器正在执行的终端就使用attach

4、拷贝容器中的文件到宿主机

docker cp 容器ID:容器内路径   宿主机路径

四、小结

Docker常用命令_第4张图片

你可能感兴趣的:(docker,docker,容器,运维)