Docker学习笔记(2)Docker的常用命令

目录

帮助命令

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

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

镜像命令

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

[root@localhost zhaowenxu]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   6 months ago   13.3kB

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

# 可选项
  -a, --all	# Show all images (default hides intermediate images) (列出所有镜像)
  -q, --quiet	# Only show image IDs (只显示镜像ID)

docker search搜索镜像

[root@localhost zhaowenxu]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11461     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4351      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   848                  [OK]

# 可选项,通过收藏来过滤
--filter=STARS=3000	#搜索出来的镜像就是STARS大于3000的

[root@localhost zhaowenxu]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11461     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4351      [OK]   

docker pull下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@localhost zhaowenxu]# docker pull mysql
Using default tag: latest	# 如果不写tag,默认latest
latest: Pulling from library/mysql
a330b6cecb98: Pull complete 	# 分层下载,docker image的核心,联合文件系统
9c8f656c32b8: Pull complete 
88e473c3f553: Pull complete 
062463ea5d2f: Pull complete 
daf7e3bdf4b6: Pull complete 
1839c0b7aac9: Pull complete 
cf0a0cfee6d0: Pull complete 
1b42041bb11e: Pull complete 
10459d86c7e6: Pull complete 
b7199599d5f9: Pull complete 
1d6f51e17d45: Pull complete 
50e0789bacad: Pull complete 
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c	# 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	# 真实地址

# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest

# 指定版本下载
[root@localhost zhaowenxu]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a330b6cecb98: Already exists 
9c8f656c32b8: Already exists 
88e473c3f553: Already exists 
062463ea5d2f: Already exists 
daf7e3bdf4b6: Already exists 
1839c0b7aac9: Already exists 
cf0a0cfee6d0: Already exists 
fae7a809788c: Pull complete 
dae5a82a61f0: Pull complete 
7063da9569eb: Pull complete 
51a9a9b4ef36: Pull complete 
Digest: sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi -f 容器id删除镜像

docker rmi -f 容器id			# 删除指定的容器
docker rmi -f 容器id 容器id 容器id	# 删除指定的多个容器
docker rmi -f $(docker images -aq)	# 删除全部容器

容器命令

说明:有镜像才可以创建容器

docker pull centos

# 参数说明
--name="Name"	容器名,用于区分容器
-d	后台方式运行
-it	使用交互方式运行
-p	指定容器的端口
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
-P	随机指定端口

# 测试,启动并进入容器
docker run -it centos /bin/bash

列出当前正在运行的容器

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

[root@localhost zhaowenxu]# ps
   PID TTY          TIME CMD
  2890 pts/0    00:00:00 su
  2904 pts/0    00:00:00 bash
 55237 pts/0    00:00:00 ps
[root@localhost zhaowenxu]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost zhaowenxu]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
9db50506acc9   centos         "/bin/bash"   5 minutes ago   Exited (0) 2 minutes ago             zealous_pascal
91c9ecaac68b   d1165f221234   "/hello"      39 hours ago    Exited (0) 39 hours ago              bold_borg
[root@localhost zhaowenxu]# 

退出容器

exit	# 直接容器停止并退出
Ctrl + P + Q	# 不停止退出

删除容器

docker rm 容器id	# 删除指定的容器,不能删除正在运行的容器
docker rm -f $(docker pa -aq)	# 删除所有容器
docker ps -a -q|xargs docker rm	# 删除所有容器

启动和停止容器的操作

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

常用的其他命令

后台启动容器

# 命令 docker run -d 镜像名
docker run -d centos

# 问题:发现centos停止了

# 常见的坑:docker容器使用后台运行,就必须要有一个新的前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后发现自己没有提供服务,就会立刻停止

查看日志

docker logs -f -t --tail 容器

# 自己编写一段shell脚本
docker run -d centos /bin/sh -c "while true; do echo tttt;sleep 1;done"

# 显示日志
-tf	# 显示日志
--tail number	# 显示日志条数

查看容器中的进程信息

# 命令 docker top 容器id
[root@localhost zhaowenxu]# docker top f5b5f280723b
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                56237               56219               0                   00:47               pts/0               00:00:00            /bin/bash

查看镜像的元数据

# 命令
docker inspect 容器id

# 测试
[root@localhost zhaowenxu]# docker inspect f5b5f280723b
[
    {
        "Id": "f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be",
        "Created": "2021-09-25T07:47:35.734377527Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 56237,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-09-25T07:47:36.224850639Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be/hostname",
        "HostsPath": "/var/lib/docker/containers/f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be/hosts",
        "LogPath": "/var/lib/docker/containers/f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be/f5b5f280723ba620e896bc7f5b6d6baab8d6d1aa20af318c8ceb623ad59f12be-json.log",
        "Name": "/determined_ellis",
        "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/c66623ae65d49eaaf6f6d6ed67cce903dbc48280c2088de84674cf203fd58bc3-init/diff:/var/lib/docker/overlay2/20dad1b8cdbd7eb608ef889df6ac1805bfbda88770591e59e8802f70689be4b0/diff",
                "MergedDir": "/var/lib/docker/overlay2/c66623ae65d49eaaf6f6d6ed67cce903dbc48280c2088de84674cf203fd58bc3/merged",
                "UpperDir": "/var/lib/docker/overlay2/c66623ae65d49eaaf6f6d6ed67cce903dbc48280c2088de84674cf203fd58bc3/diff",
                "WorkDir": "/var/lib/docker/overlay2/c66623ae65d49eaaf6f6d6ed67cce903dbc48280c2088de84674cf203fd58bc3/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "f5b5f280723b",
            "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": "bc3879595c2b5204361794cf0b63171598f1bfe8a3b7ea4d9f2acc1a46f4f97c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/bc3879595c2b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "68eebab4c5e66566cd040306e3b8bee711409ef2fe4b6cdb6a19dc5f8780a08a",
            "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": "312ede194eeae6a2486f369b45e2363a351fe32419db3c0830f41cb33b754794",
                    "EndpointID": "68eebab4c5e66566cd040306e3b8bee711409ef2fe4b6cdb6a19dc5f8780a08a",
                    "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 exec -it 容器id

# 测试
[root@localhost zhaowenxu]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
f5b5f280723b   centos    "/bin/bash"   21 minutes ago   Up 20 minutes             determined_ellis
[root@localhost zhaowenxu]# docker exec -it f5b5f280723b /bin/bash
[root@f5b5f280723b /]# 

# 方式二
docker attach 容器id

docker exec	# 进入容器后开启一个新的终端
docker attach	# 进入容器正在执行的终端,不会启动新的进程

从容器拷贝文件到主机

# 进入docker容器内部

docker cp 容器id:容器内路径 目的主机路径

# 拷贝是一个手动过程,未来使用 -v 卷的技术,可以实现自动
上一篇:docker常用 镜像命令 以及 帮助命令


下一篇:【IDEA与git集成】