Docker学习笔记 02概念和命令

Docker是怎么工作的?

Docker是一个Client - Server 结构的系统,Docker的守护进程运行在宿主机上,通过Socket从客户端访问。Docker-Server收到Docker-Client的指令,就会执行命令。

Docker为什么比VM快?

1.Docker有更少的抽象层
2.Docker利用的是宿主机的内核,VM需要Guest OS(操作系统内核)

帮助命令
docker version
docker info        #系统信息,包括镜像和容器的数量
docker xxx --help

官方文档:https://docs.docker.com/refer...

镜像命令
docker images --help
docker images 查看所有本地镜像
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   4 months ago   13.3kB

REPOSITORY  #镜像仓库源  
TAG         #镜像标签
IMAGE ID    #镜像ID   
CREATED     #镜像创建时间   
SIZE        #镜像大小

#可选项
-a --all      #列出所有镜像
-q --quiet    #只显示镜像ID   
docker search 搜索镜像
docker search mysql

#可选项
-f --filter #过滤条件

--fitler=STARS=3000 #收藏3000以上的镜像
docker pull 下载镜像

Dockerhub地址:https://hub.docker.com

docker pull [OPTIONS] NAME[:TAG|@DIGEST]
OPTIONS #可选项
NAME    #镜像名
TAG     #镜像标签

#指定版本下载
docker pull mysql:5.7
docker rmi 删除镜像
docker rmi -f 容器ID1 ... 容器IDN
#删除全部镜像
docker rmi -f $(docker images -aq)
容器命令
docker run 新建并启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
 
#参数说明
--name #容器名
-d     #后台方式运行  
-it    #交互方式运行,进入容器查看  
-p     #指定容器端口
-P     #随机指定端口

#测试:启动并进入容器
docker pull centos
docker run -it centos /bin/bash      
#退出容器
exit #容器停止并退出
CTRL + P + Q #不停止退出
docker ps 列出容器
docker ps #列出当前正在运行的容器

-a   #列出当前正在运行的容器+历史运行过的容器
-n=? #最近创建的容器
-q   #只显示容器编号
docker rm 删除容器
docker rm 容器ID
docker rm $(docker ps -aq) #删除所有容器

-f #强制删除(包括运行中的容器)
docker start 启动容器
docker start 容器ID
docker restart 容器ID
docker stop 容器ID
docker kill 容器ID
docker logs 查看日志
docker logs -tf --tail 10 容器ID #显示指定行数
docker logs -t -f 容器ID         #显示日志
docker top 查看进程
docker top 容器ID
docker inspect 查看元数据
docker inspect 容器ID

[
    {
        "Id": "d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63",
        "Created": "2021-07-29T23:22:16.737750806Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2021-07-29T23:31:58.686730736Z",
            "FinishedAt": "2021-07-29T23:32:10.840176294Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/hostname",
        "HostsPath": "/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/hosts",
        "LogPath": "/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63-json.log",
        "Name": "/optimistic_bartik",
        "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/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587-init/diff:/var/lib/docker/overlay2/27c6cfd367d905528b550279efab95fa7f52c97abb12625131c408b6dea72fe7/diff",
                "MergedDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/merged",
                "UpperDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/diff",
                "WorkDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "d3b9799417b6",
            "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": "0ac7744565902b645737fcc3eabd47c9c27a7babaa0856ec55e466a7237e6ba8",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/0ac774456590",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2d10dc4aa739ceb9a9aad3c64688a40431ff71e5dfaed298ec83265434d7863c",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
docker exec\attach 进入当前正在运行的容器
docker exec -it 容器ID /bin/bash  
#进入容器后开启新的终端,可以操作

docker attach 容器ID
#进入容器正在执行的终端,不会启动新的进程
docker cp 拷贝文件
docker cp 容器ID:容器内路径 宿主机路径
其他
docker run -d 容器ID
docker ps 发现容器停止了

原因:docker容器在后台运行,必须有一个前台进程,如果docker发现没有应用,就会自动停止
小结

Docker学习笔记 02概念和命令_第1张图片

你可能感兴趣的:(docker)