Docker----常用命令(镜像和容器的常用命令)

Posted zyh-coder0927

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker----常用命令(镜像和容器的常用命令)相关的知识,希望对你有一定的参考价值。

帮助命令

docker version          #显示docker的版本信息

docker info              #显示docker的系统信息,包括镜像和容器的数量

docker命令--help      #帮助命令


帮助文档的地址:https://docs.docker.com/engine/reference/commandlinel

 

镜像命令:

docker images   显示所有镜像

docker images -a    显示所有的镜像

docker images  -aq      显示所有镜像的id

 

搜索命令

docker search   镜像名称    

技术图片

 

 

docker search mysql --filter=stars=3000     搜索大于3000提交量的mysql镜像

 

docker pull 拉取镜像命令

 

docker pull  镜像名        不指定镜像版本则默认拉取最新版本镜像

 

技术图片

 

 

docker pull mysql:5.7       指定版本的拉取镜像

 

技术图片

 

 

上述拉取指定5.7版本的命令实际上等价于直径拉取镜像真是地址的命令:docker pull docker.io/library/mysql:5.7

 

docker rmi 删除镜像

docker rmi -f  镜像id       删除指定镜像

docker rmi  -f  $(docker images -aq)    删除所有的镜像

docker rmi -f  镜像id  镜像id  镜像id      删除指定的多个镜像

 

容器命令:

新建容器并启动


docker run [可选参数] image
#参数说明

  • --name="Name"容器名字 tomcat01 tomcat02,用来区分容器
  • -d后台方式运行
  • -it使用交互方式运行,进入容器查看内容
    • -p容器的端口-p 8080:8080
    • -p ip:主机端口:容器端口
    • -p主机端口:容器端口(常用)
  • -p容器端口
  • -P随机指定端口

 

 

退出容器

exit 退出当前所在的容器(注意这种方式容器会停止运行)

 

快捷键退出后台运行:ctrl + P + Q

 

容器进程查看命令: docker ps

 

docker ps     查看正在运行的容器

 

docker ps -a   查看所有正在运行和运行过得容器

 

docker ps -n=个数      显示最近创建的指定个数的容器

 

docker ps -aq        显示所有容器的id

 

删除容器:docker rm  容器id

docker rm 容器id   删除指定的容器(不能删除正在运行的容器,如果要强制删除则使用docker rm -f

 

docker rm -f $(docker ps -aq)     删除所以的容器

 

 

启动和停止容器的操作


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



常用其他命令后台启动容器

 


#命令docker run-d镜像名!
[rootakuangshen/#docker run-d centos
#问题docker ps,发现centos停止了
#常见的坑:docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提性服务,就会立刻停止,就是没有程序了

但是ctrl +P +Q 快捷键可以正常后台退出

 

查看容器日志

docker logs -tf --tail  条数  容器id      查看指定容器的指定条数的日志

 

查看容器内的进程

 

docker  top  容器id

技术图片

 

 

 

 

查看docker容器元数据

docker inspect  容器id 

查看县关于容器的所有设置信息

[root@localhost ~]# docker inspect 1fefd1d2ea51
[
    {
        "Id": "1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1",
        "Created": "2020-11-11T12:38:58.99005358Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "--cluster-enabled",
            "yes",
            "--cluster-config-file",
            "nodes-node-02.conf",
            "--port",
            "6380"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3526,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-11-18T01:31:16.80516048Z",
            "FinishedAt": "2020-11-18T01:07:40.896724932Z"
        },
        "Image": "sha256:5958914cc55880091b005658a79645a90fd44ac6a33abef25d6be87658eb9599",
        "ResolvConfPath": "/var/lib/docker/containers/1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1/hostname",
        "HostsPath": "/var/lib/docker/containers/1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1/hosts",
        "LogPath": "/var/lib/docker/containers/1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1/1fefd1d2ea51fb1d0323b2c9c14cf701d7ef9cbecf1cf0d6834ad89254e2aac1-json.log",
        "Name": "/redis-node02",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "redis-node02:/data"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "host",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "always",
                "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/2daa20e0ca5755bfde26935e859445f60e6f66f53a79d9734f766f713ec9d88a-init/diff:/var/lib/docker/overlay2/e8e48785efcb0fe20fed042dcd8c024e3ab7d9cf5d1be980f3a60e4121f26a82/diff:/var/lib/docker/overlay2/037252eba91a6a6878e14dc54370fc88cb28ae9381c75abbf9a207b17bb137a6/diff:/var/lib/docker/overlay2/b73ff579994d590a4ac10ab4761bb1173bced4893074cf543d9bcc94687db765/diff:/var/lib/docker/overlay2/9e8d2755a7bfb9652fb53df5f29b5ae1179c3aa6f889f664549f521861fa0097/diff:/var/lib/docker/overlay2/8d38e9f7d8a9cd01871ecf82379fae729b7c69b9e55b58d3ea160d97f0b5ccf9/diff:/var/lib/docker/overlay2/46f9f7bd24cbd4dec55bf1d767e5923ea09fe28ad9f4087dd5b6ce6367e9ca17/diff",
                "MergedDir": "/var/lib/docker/overlay2/2daa20e0ca5755bfde26935e859445f60e6f66f53a79d9734f766f713ec9d88a/merged",
                "UpperDir": "/var/lib/docker/overlay2/2daa20e0ca5755bfde26935e859445f60e6f66f53a79d9734f766f713ec9d88a/diff",
                "WorkDir": "/var/lib/docker/overlay2/2daa20e0ca5755bfde26935e859445f60e6f66f53a79d9734f766f713ec9d88a/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "redis-node02",
                "Source": "/var/lib/docker/volumes/redis-node02/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "localhost.localdomain",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
                "6379/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "REDIS_VERSION=5.0.2",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.2.tar.gz",
                "REDIS_DOWNLOAD_SHA=937dde6164001c083e87316aa20dad2f8542af089dfcb1cbb64f9c8300cd00ed"
            ],
            "Cmd": [
                "--cluster-enabled",
                "yes",
                "--cluster-config-file",
                "nodes-node-02.conf",
                "--port",
                "6380"
            ],
            "Image": "redis:5.0.2",
            "Volumes": {
                "/data": {}
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f664f271af59a90220abd606f14757ab1c78ed113b8983647eb1f81b00fc2551",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/default",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "host": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "397dd96386f4b6c0d856cdd476ba6b5609c970d0f8955965dec39559f93331f7",
                    "EndpointID": "6c1a0c138d5d13411157cd59954254a79bf30520b43b0b000908a4e144757d93",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

 

 

进入当前正在运行的容器:

方式1:

docker exec -it  容器id  bashshell

比如进入正在运行的centos容器:

docker exec -it 

技术图片

 

 

 

方式2:

docker attach  容器id     进入指定容器的上一次离开的进入状态

技术图片

 

 

 

#docker exec#进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach#进入容器正在执行的终端,不会启动新的进程!

 

从容器内拷贝文件到宿主机上:(可以直接拷贝)

技术图片

 




















以上是关于Docker----常用命令(镜像和容器的常用命令)的主要内容,如果未能解决你的问题,请参考以下文章

docker常用命令

docker常用命令

Docker----常用命令(镜像和容器的常用命令)

Docker镜像和容器常用命令

Docker 从入门到实践系列三 - Docker 常用命令

Docker 从入门到实践系列三 - Docker 常用命令