Docker超详细的入门学习通俗易懂(第一讲)

学习一个新技术的同时我们要去了解这个技术为什么会产生,并且去了解其中的发展历史,这部分就不在这里涉及,有兴趣的小伙伴可以自行去查阅资料。

Docker的常用命


docker version #查看docker的版本信息
docker info  #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令

帮助文档地址:Reference documentation | Docker Documentation

Docker 的镜像命令


docker images 查看所有本地的主机镜像

[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        latest    3218b38490ce   3 months ago   516MB
centos       latest    5d0da3dc9764   6 months ago   231MB
# 解释
REPOSITORY 镜像的仓库源
TAG        镜像的标签
IMAGE ID   镜像的id
CREATED    镜像的创建时间
SIZE       镜像的大小
#可选项
  -a, --all           #列出所有的镜像         
  -f, --filter       
  -q, --quiet         #只列出镜像的id

docker search 镜像名寻找镜像

[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                            MySQL is a widely used, open-source relation…   12343     [OK]       
mariadb                          MariaDB Server is a high performing open sou…   4748      [OK]       
#可选项 ,通过某个条件过滤
--filter 
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker search mysql -f=stars=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12343     [OK]    #这个就是通过stars来过滤掉收藏少于5000的

docker pull 下载镜像

#下载镜像 docker pull 镜像名[:tag]
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker pull mysql
Using default tag: latest #如果不写tag 默认就是latest
#docker  分层下载 docker images 的核心 联合文件系统
latest: Pulling from library/mysql
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Image is up to date for mysql:latest
docker.io/library/mysql:latest #真实地址
#等价于
docker pullmysql
docker pull docker.io/library/mysql:latest
#指定版本下载(前提是在官方文档中存在)

docker rmi 删除镜像

#递归删除所有镜像
docker rmi  -f $(docker images -aq)
#删除指定的容器
docker rmi -f 镜像id 镜像id 镜像id

容器命令


说明:有了镜像我们才可以创建容器,linux,下载一个centos镜像来测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image
#参数说明
--name="Name"  容器名字 用来区分容器
-d             后台方式运行
-it            交互方式运行,查看容器内容
-p             指定容器的端口 -p 8080:8080
    -p 主机端口:容器端口(常用)
    -p 容器端口
    容器端口
-P             随机指定端口

#测试
#启动并进入容器,启动之后主机名发生变化,变成了容器名称
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker run -it centos /bin/bash
[root@de1dd68c2b54 /]# ls  #查看容器内的centos  基础版本,很多的命令都是不完善的! 容器内外互不影响
#从容器中退回主机
exit

列出所有运行的容器

#docker ps 命令
   #列出当前正在运行的容器
-a #列出当前正在运行的容器+带出历史运行过的容器
-n=?#显示最近创建的容器
-q #只显示容器的编号

退出容器

exit #直接退出容器并停止
ctrl + p + q #同时按,容器不停止退出

删除容器

docker rm 容器id   #删除指定的容器,如果不能正常删除 rm -f 强制删除
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a q|xargs docker rm #删除所有的容器

启动和停止容器的操作

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

常用的其它命令


后台启动容器

#命令 docker run -d 镜像名
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker run centos
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# 
#问题 docker ps,发现centos 停止了
#常见的坑:docker 容器使用后台运行,就必须要有一个前台的进程,docker发现没有应用就会自动停止

查看日志

docker logs -f -t --tail [可以加一个条数] 容器id
            -tf

查看容器中的进程信息 ps

 # 查看容器的进程信息
 docker top 容器id
 [root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
6b2c44b314d2   centos    "/bin/bash"   19 seconds ago   Up 18 seconds             loving_nobel
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker top 6b2c44b314d2 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                1767403             1767383             0                   20:35               pts/0               00:00:00            /bin/bash

查看镜像的元数据

#命令
docker inspect 容器id
#测试
[root@iZf8zaekpumjn6q1vv1fmiZ ~]# docker inspect 6b2c44b314d2 
[
    {
        "Id": "6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8",
        "Created": "2022-04-02T12:35:40.855786026Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1767403,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-04-02T12:35:41.081927012Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8/hostname",
        "HostsPath": "/var/lib/docker/containers/6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8/hosts",
        "LogPath": "/var/lib/docker/containers/6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8/6b2c44b314d26619db070b8196d0361f94bcdd9140817f8dc65ad106c47ec1d8-json.log",
        "Name": "/loving_nobel",
        "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/0e082c53d0b5991703327adff04eecbc474ffade4d3939ca990eacf6ac1894c4-init/diff:/var/lib/docker/overlay2/f38688666bec5108266470d0ce29274808c124f282ef7de79f4fb25f52625a74/diff",
                "MergedDir": "/var/lib/docker/overlay2/0e082c53d0b5991703327adff04eecbc474ffade4d3939ca990eacf6ac1894c4/merged",
                "UpperDir": "/var/lib/docker/overlay2/0e082c53d0b5991703327adff04eecbc474ffade4d3939ca990eacf6ac1894c4/diff",
                "WorkDir": "/var/lib/docker/overlay2/0e082c53d0b5991703327adff04eecbc474ffade4d3939ca990eacf6ac1894c4/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "6b2c44b314d2",
            "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": "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": "8aa3245a6a015a06bae8be39245c00342a4b3090b4a2b2f6230259a379b31e69",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/8aa3245a6a01",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f54cf1f1f145fec3335446056b5095fd975f01f5a0edd0e06d0c575aa925f8dc",
            "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": "91e5acf1e4943bec8c9aff6cdd6c36b64eac03a3f1d4f08d4e5487836020738c",
                    "EndpointID": "f54cf1f1f145fec3335446056b5095fd975f01f5a0edd0e06d0c575aa925f8dc",
                    "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,linux,tomcat,容器)