Docker(三)Docker的基础命令

Docker1.png

帮助命令


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

docker官方文档地址:https://docs.docker.com/compose/reference/

镜像命令


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

[root@vm22 ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
waibi/centos          1.0       161ad1397191   7 hours ago     231MB
tomcat                2.0       8663b1d60f71   8 hours ago     684MB
nginx                 latest    605c77e624dd   9 months ago    141MB
#解释
REPOSITORY    镜像的仓库源
TAG           镜像的标签
IMAGE ID      镜像的ID
CREATED       镜像的创建时间
SIZE          镜像的大小
#可选项
-a --all      显示所有镜像
-q --quiet    只显示景象ID

docker search 搜索镜像

[root@vm22 ~]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13234     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5062      [OK]       
#可选项
--filter=starts=3000    #搜索出来镜像starts大于3000的镜像

docker pull 下载镜像

下载镜像 $ docker pull 镜像名[:tag]
[root@vm22 ~]# docker pull mysql
Using default tag: latest  #如果指定tag,将默认下载latest
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: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

docker rmi 删除镜像

[root@vm22 ~]# docker rmi -f 3218b38490ce #删除指定镜像
[root@vm22 ~]# docker rmi -f IMAGE ID IMAGE ID #删除多个镜像
[root@vm22 ~]# docker rmi -f $(docker images -aq) #删除全部镜像

容器命令


启动容器

$ docker run IMAGE [COMMAND] [ARG...]
# 参数说明
--name="name"  给容器命名
-d  后台运行
-it  使用交互方式运行,进入容器查看内容
-p  指定容器端口 -p 8080:8080
        -p 主机端口:容器端口
        -p IP:主机端口:容器端口
-P  随机指定端口

#启动并进入容器
[root@vm22 ~]# docker run -it centos /bin/bash
[root@bdcf79e05d39 /]# ls  # 查看容器内的centos
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@bdcf79e05d39 /]# exit  # 退出容器 
exit

列出所有正在运行的容器

# docker ps 命令
-a # 列出当前正在运行的容器+历史运行过的容器
-n=?  # 显示最近创建的容器
-q  # 只显示容器ID
[root@vm22 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@vm22 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                          PORTS     NAMES
bdcf79e05d39   centos    "/bin/bash"   3 minutes ago   Exited (0) About a minute ago             brave_jepsen

退出容器

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

删除容器

docker rm 容器id  #删除指定容器
docker rm -f $(docker ps -aq)  # 删除所有容器

启动和停止容器操作

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

其他常用命令


后台启动容器

# 命令 doceker run -d 镜像
[root@vm22 ~]# docker run -d centos

查看日志

docker logs -f -t --tail 容器
# 测试
[root@vm22 ~]# docker run -d centos /bin/bash -c "while true;do echo print logs;sleep 1;done "
[root@vm22 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
82bb167358c7   centos    "/bin/bash -c 'while…"   34 seconds ago   Up 33 seconds             hungry_mclean
# 显示日志
  -tf 
  --tail number
[root@vm22 ~]# docker logs -t -f --tail 4 82bb167358c7
2022-09-28T17:41:05.146127148Z print logs
2022-09-28T17:41:06.151409147Z print logs
2022-09-28T17:41:07.157108266Z print logs
2022-09-28T17:41:08.161395599Z print logs

查看容器中进程信息

# 命令 docker top 容器ID
[root@vm22 ~]# docker top 82bb167358c7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4773                4753                0                   01:39               ?                   00:00:00            /bin/bash -c while true;do echo print logs;sleep 1;done
root                5172                4773                0                   01:44               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

# 命令 docker inspect 容器ID
[root@vm22 ~]# docker inspect 82bb167358c7
[
    {
        "Id": "82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b",
        "Created": "2022-09-28T17:39:20.314052558Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo print logs;sleep 1;done "
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4773,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-09-28T17:39:20.57549319Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/hostname",
        "HostsPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/hosts",
        "LogPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b-json.log",
        "Name": "/hungry_mclean",
        "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/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7-init/diff:/var/lib/docker/overlay2/d95e550d4f62886c70a537524378ebfba5e96932146481454237df6005a0cb8c/diff",
                "MergedDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/merged",
                "UpperDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/diff",
                "WorkDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "82bb167358c7",
            "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/bash",
                "-c",
                "while true;do echo print logs;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": "0cd61c994ce552f0c40d92076a80223aa1ca509814ced1ba2e05ca3006f800a1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/0cd61c994ce5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "4791507c4b3d9221dcca506844705803e4d6faafeacfa206225650490224ba02",
            "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": "2893fe225a852d859fb8e831ff668623b9f220c3f47a8b1b83bb31691a8961b5",
                    "EndpointID": "4791507c4b3d9221dcca506844705803e4d6faafeacfa206225650490224ba02",
                    "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 /bin/bahs  # 进入容器后开启一个新的终端
docker attch 容器ID # 进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker cp 容器ID:容器内路径 目标路径
[root@vm22 ~]# docker cp cfb377b1b56f:/home/docker-cp /home
[root@vm22 ~]# ls /home
docker-cp  docker-test-volume  mysql  test

学习视频:https://www.bilibili.com/video/BV1og4y1q7M4/?spm_id_from=333.999.0.0&vd_source=4297c14c1c7438e292ea9310e1abeb31

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,681评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,710评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,623评论 0 334
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,202评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,232评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,368评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,795评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,461评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,647评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,476评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,525评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,226评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,785评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,857评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,090评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,647评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,215评论 2 341

推荐阅读更多精彩内容