Docker常用命令

日常记不住的docker命令,借地存命令!!!

1、帮助命令

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

2、镜像的基本命令

  • docker images 查看所有本地的主机上的镜像
[root@VM-8-3-centos ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.7       c20987f18b13   16 months ago   448MB

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

# 可选项
 -a, --all             # 列出所有镜像
 -q, --quiet           # 显示镜像的id
  • docker search <搜索镜像>
[root@VM-8-3-centos ~]# docker search mysql
NAME                            DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14115     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5389      [OK]       

# 可选项,通过搜索来过滤
[root@VM-8-3-centos ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   14115     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5389      [OK] 
  • docker pull <镜像名称> [:tag]下载镜像
[root@VM-8-3-centos ~]# docker pull mysql
Using default tag: latest # 如果不写tag,默认最新版本
latest: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
688ba7d5c01a: Already exists 
00e060b6d11d: Already exists 
1c04857f594f: Already exists 
4d7cfa90e6ea: Already exists 
e0431212d27d: Already exists 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 防伪标签
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
  • docker rmi -f 镜像id | 镜像名称 删除镜像
[root@VM-8-3-centos ~]# docker rmi -f 3218b38490ce # 删除指定镜像
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
[root@VM-8-3-centos ~]# docker rmi -f $(docker images -aq) # 递归删除所有镜像
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76

3、容器的基本命令

  • docker run [可选参数] image 容器启动
# 参数说明
--name="Name"   容器的名字 test01,test02 用来区分容器
-d              后台方式运行
-it			    使用交互方式运行,进入容器查看内容
-p        		指定容器的端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口
	-p 容器端口
-P 				随机指定端口
# 启动并进入容器
[root@VM-8-3-centos ~]# docker run -it centos /bin/bash
[root@330b456e1e69 /]# ls # 查看容器的基本内容
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#从容器中退出返回主机
[root@c36f498dfbac /]# exit
exit
[root@VM-8-3-centos ~]# ls
  • docker ps 列出所有的运行容器
docker ps # 列出所有的运行容器
-a		  # 列出当前正在运行的容器+历史运行过的容器
-n=<num>  # 显示最近创建的容器
-q		  # 只显示容器的编号
[root@VM-8-3-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-8-3-centos ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS                     PORTS     NAMES
c36f498dfbac   centos         "/bin/bash"               3 minutes ago   Exited (0) 3 minutes ago             inspiring_williams
4a44c5870938   centos         "/bin/bash/"              3 minutes ago   Created                              eloquent_poincare
330b456e1e69   centos         "/bin/bash"               8 minutes ago   Exited (0) 5 minutes ago             zen_dijkstra
2fb6ed6a3898   c20987f18b13   "docker-entrypoint.s…"    3 hours ago     Exited (0) 3 hours ago               recursing_galois
b9f52a5d4ef0   3218b38490ce   "docker-entrypoint.s…"    3 hours ago     Exited (127) 3 hours ago             pedantic_cohen
e0bbb8b62d43   9c7a54a9a43c   "/hello"                  29 hours ago    Exited (0) 29 hours ago              xenodochial_booth
# 退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
  • 删除容器
[root@VM-8-3-centos ~]# docker rm e0bbb8b62d43 # 不能删除正在运行的容器,rm -f 可以强制删除
e0bbb8b62d43
[root@VM-8-3-centos ~]# docker rm -f $(docker ps -aq) # 批量删除
c36f498dfbac
4a44c5870938
330b456e1e69
2fb6ed6a3898
b9f52a5d4ef0
docker ps -a -q|xargs docker rm # 批量删除
  • 启动和停止容器的操作
docker start 容器id    # 启动容器
docker restart 容器id  # 重启容器
docker stop 容器id     # 停止当前正在运行的容器
docker kill 容器id     # 强制停止当前容器

4、日志、元数据、进程的查看

  • 后台启动容器
# 命令 docker run -d 镜像名
[root@VM-8-3-centos ~]# docker run -d centos
8c038e60674b76ae1c309e6a8a511076a0e7d4a64816e0fe82b5bad35de43eed
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker 发现没有应用,就会自动停止
  • 查看日志
[root@VM-8-3-centos ~]# docker run -d centos /bin/sh -c "while true;do echo yxking;sleep 1;done" # 脚本模拟日志内容
508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28
[root@VM-8-3-centos ~]# docker ps
CONTAINER ID   IMAGE     
508f2dede8c8   centos   
# 查看日志内容
-tf           # 显示日志
--tail number # 显示日志有条数
[root@VM-8-3-centos ~]# docker logs -tf --tail 10 508f2dede8c8 
2023-05-12T06:40:41.637361433Z yxking
2023-05-12T06:40:42.639404116Z yxking
2023-05-12T06:40:43.641779946Z yxking
2023-05-12T06:40:44.644328441Z yxking
2023-05-12T06:40:45.646666635Z yxking
2023-05-12T06:40:46.648686718Z yxking
2023-05-12T06:40:47.650657967Z yxking
2023-05-12T06:40:48.652947302Z yxking
2023-05-12T06:40:49.655567460Z yxking
  • 查看容器中进程信息
# 命令 docker top 容器id
[root@VM-8-3-centos ~]# docker top 508f2dede8c8
UID                 PID                 PPID                C                   STIME               TTY                
root                21025               21005               0                   14:38               ?                   
  • 查看镜像的元数据
# 命令 docker inspect 容器id
[root@VM-8-3-centos ~]# docker inspect 508f2dede8c8
[
    {
        "Id": "508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28",
        "Created": "2023-05-12T06:38:51.00362227Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo yxking;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 21025,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-05-12T06:38:51.363210651Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28/hostname",
        "HostsPath": "/var/lib/docker/containers/508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28/hosts",
        "LogPath": "/var/lib/docker/containers/508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28/508f2dede8c8a69a9fb5f00b690820331a070e76ca9862e86bd557337148ed28-json.log",
        "Name": "/priceless_ardinghelli",
        "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,
            "ConsoleSize": [
                23,
                131
            ],
            "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",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "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/d468ab74ab434dbc8c7d877d6301a1e6322542a0ee2118f238e33ffd80e3458f-init/diff:/var/lib/docker/overlay2/98bdf8cdaae9267630f1a388db58f7dc3cbed70e80c9613dfeed6fa77bfe3535/diff",
                "MergedDir": "/var/lib/docker/overlay2/d468ab74ab434dbc8c7d877d6301a1e6322542a0ee2118f238e33ffd80e3458f/merged",
                "UpperDir": "/var/lib/docker/overlay2/d468ab74ab434dbc8c7d877d6301a1e6322542a0ee2118f238e33ffd80e3458f/diff",
                "WorkDir": "/var/lib/docker/overlay2/d468ab74ab434dbc8c7d877d6301a1e6322542a0ee2118f238e33ffd80e3458f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "508f2dede8c8",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo yxking;sleep 1;done"
            ],
            "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": "e666fc3b7f15f279f2920c1a828bda55c0a36635cc170b107c945e83c031a475",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/e666fc3b7f15",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "aae0e8a02f75056ab5f32a3eb15fb70f22eacbca6c96b7b2a80486f4d7566372",
            "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": "c8eaf5281e423c0d7f9ea43f974d40dd446afa68fd2a650f2dfdc67c93046a5b",
                    "EndpointID": "aae0e8a02f75056ab5f32a3eb15fb70f22eacbca6c96b7b2a80486f4d7566372",
                    "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
                }
            }
        }
    }
]

5、进入容器的命令和拷贝命令

  • 进入当前正在进行的容器
# 命令 docker exec -it 容器id bashShell
# 方法1
[root@VM-8-3-centos ~]# docker exec -it 508f2dede8c8 /bin/bash
[root@508f2dede8c8 /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
# 方法2
[root@508f2dede8c8 /]# docker attach 508f2dede8c8

# docker exec   进入容器后开启一个新的终端,可以在里面的系统操作
# docker attach 进入容器正在执行的终端,不会启动新的进程!
  • 从容器内拷贝文件到主机上
# 命令 docker cp 容器id:路径 本机路径
[root@VM-8-3-centos home]# docker cp 508f2dede8c8:/home/test.java /home
                                               Successfully copied 1.54kB to /home
[root@VM-8-3-centos home]# ls
charging                              lighthouse                                  redis
emqx                                  mmtch_smart_charging-0.0.1-SNAPSHOT.jar     sh
gitlab-ce-15.6.2-ce.0.el7.x86_64.rpm  mysql57-community-release-el7-9.noarch.rpm  test.java
jdk1.8.0_231                          project                                     www

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值