• 关于docker的常用命令


    作者:一乐
    链接:https://www.zhihu.com/question/436296965/answer/2478383514
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    -------------------- 关于docker的常用命令------------------------------一、帮助命令docker version # 显示docker的基本信息
    docker info # 系统信息,镜像和容器的数量
    docker 命令 --help # 全部信息
    官网文档:https://docs.docker.com/reference/二、镜像命令[增删改查]:查看所有镜像 docker images搜索镜像 docker search 镜像名下载镜像 docker pull 镜像名[:tag]删除镜像 docker rmi -f 镜像id删除所有镜像 docker rmi -f $(docker images -aq)1、查看所有镜像:docker images查看所有本地主机上的镜像[root@192 ~]# docker images

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

    --all , -a Show all images (default hides intermediate images) # 显示所有
    --digests Show digests
    --filter , -f Filter output based on conditions provided
    --format Pretty-print images using a Go template
    --no-trunc Don’t truncate output
    --quiet , -q Only show numeric IDs # 只显示id
    2、搜索镜像:docker search搜索仓库中的镜像,相当于网页搜索 推荐到docker hub官网搜索:http://hub.docker.com docker search mysql

    docker search --help
    设置搜索条件:# 解释
    Options:
    -f, --filter filter Filter output based on conditions provided
    --format string Pretty-print search using a Go template
    --limit int Max number of search results (default 25)
    --no-trunc Don't truncate output


    docker search mysql --filter=STARS=3000 # 搜索出Stars大于3000的
    3、下载镜像:docker pull版本到官网查看,官网:https://hub.docker.com/_/mysql★ docker的精髓:分层下载 image的核心,联合文件系统 (1)下载镜像命令:docker pull 镜像名[:tag]不写tag,默认是lasest docker的精髓:分层下载 <img src="https://pica.zhimg.com/50/v2-a800c7b99e47f051e0ab101626d48389_720w.jpg?source=1940ef5c" data-caption="" data-size="normal" data-rawwidth="818" data-rawheight="351" data-default-watermark-src="https://pic1.zhimg.com/50/v2-9caff61c92a709f19133535d3cb74a22_720w.jpg?source=1940ef5c" class="origin_image zh-lightbox-thumb" width="818" data-original="https://picx.zhimg.com/v2-a800c7b99e47f051e0ab101626d48389_r.jpg?source=1940ef5c"/>4、删除镜像:docker rmi删除镜像命令:docker rmi 镜像id/镜像名称# 删除一个 可以通过名称 也可以指定id -f表示强制
    docker rmi -f 9cfcce23593a
    # 删除多个 用空格分隔id
    docker rmi -f 镜像id 镜像id 镜像id
    # 删除所有
    docker rmi -f $(docker images -aq) # images -aq就是查所有镜像id,从而递归删除
    三、容器命令镜像创建运行一个docker容器实例 docker run [可选参数] image# 启动并进入容器docker run -it centos /bin/bash # 等价于:先run启动,然后通过exex /bin/bash 进入容器 docker run -it centos docker exec -it 容器id /bin/bash 查看运行的容器:docker ps [可选参数] 退出容器:【停止容器】exit 【非停止容器】ctr+p+q 删除容器:docker rm [可选参数] 容器id # 删除所有容器 -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 1、案例:centos容器的测试(1)下载centos 镜像docker pull centos
    (2)新建容器实例并启动docker run [可选参数] image

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

    -p 随机指定端口

    # 测试,启动并进入容器
    docker run -it centos /bin/bash
    # 等价于:
    docker run -it centos
    docker exec -it 容器id /bin/bash
    # 查看目录,查看容器内的centos,基本版本,很多命令都是不完善的!
    ls
    # 退出,从容器中退回主机
    exit
    (3)查看运行的容器# 查看正在运行的容器
    docker ps
    # 查看所有(包括曾经)运行的容器
    docker ps -a
    # 显示最近创建的容器,设置显示个数
    docker ps -a - n=?
    # 只显示容器的编号(容器id)
    docker ps -aq

    [root@192 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    [root@192 ~]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    9939864fa2e6 centos "bin/bash" 4 minutes ago Exited (0) 4 minutes ago unruffled_knuth
    5f42e9930435 centos "/bin/bash" 8 minutes ago Exited (0) 4 minutes ago lucid_cannon
    a89ddb393d3d bf756fb1ae65 "/hello" 19 hours ago Exited (0) 19 hours ago gracious_bhabha
    [root@192 ~]# docker ps -a -n=2
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    9939864fa2e6 centos "bin/bash" 6 minutes ago Exited (0) 6 minutes ago unruffled_knuth
    5f42e9930435 centos "/bin/bash" 10 minutes ago Exited (0) 7 minutes ago
    [root@192 ~]# docker ps -aq
    9939864fa2e6
    5f42e9930435
    a89ddb393d3d

    (4)退出容器# 容器停止退出
    exit
    # 容器不停止退出 注意必须在英文输入法下,中文输入法不行
    Ctrl + P + Q

    [root@192 ~]# docker run -it centos /bin/bash
    [root@bfcea13c40cd /]# [root@192 ~]# docker ps ##注意这里会自动给个命令,删掉
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    bfcea13c40cd centos "/bin/bash" About a minute ago Up About a minute stoic_wilson
    edbd9366d959 centos "/bin/bash" 7 minutes ago Up 7 minutes affectionate_bartik
    [root@192 ~]# docker exec -it edbd9366d959 /bin/bash ## 再次进入
    [root@edbd9366d959 /]# exit ##停止并推出
    exit

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

    [root@192 ~]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    bfcea13c40cd centos "/bin/bash" 29 minutes ago Up 29 minutes stoic_wilson
    edbd9366d959 centos "/bin/bash" 35 minutes ago Up 35 minutes affectionate_bartik
    9939864fa2e6 centos "bin/bash" 48 minutes ago Exited (0) 48 minutes ago unruffled_knuth
    5f42e9930435 centos "/bin/bash" 52 minutes ago Exited (0) 49 minutes ago lucid_cannon
    a89ddb393d3d bf756fb1ae65 "/hello" 20 hours ago Exited (0) 20 hours ago gracious_bhabha
    [root@192 ~]# docker rm 5f42e9930435
    5f42e9930435
    [root@192 ~]# docker rm edbd9366d959 # 注意正在运行的容器不能删除
    Error response from daemon: You cannot remove a running container edbd9366d9596c744dd449119269b04de2f2a494e7fc471f6396bcefd94c33fe. Stop the container before attempting removal or force remove


    [root@192 ~]# docker ps -aq # 所有容器id
    bfcea13c40cd
    edbd9366d959
    9939864fa2e6
    a89ddb393d3d
    [root@192 ~]# docker rm -f $(docker ps -aq) # 全部删除
    bfcea13c40cd
    edbd9366d959
    9939864fa2e6
    a89ddb393d3d

    (6)启动和停止/杀死容器的操作docker start
    docker restart
    docker stop
    docker kill
    四、常用其他命令后台启动docker命令:参数是 -d docker run -d 镜像名/镜像id 查看日志[没有报错信息或者报错信息不足以解决问题时,需要查看日志]: docker logs docker logs [-f -t --tail n] 容器id 查看进程(ps): docker top 容器id 查看正在运行的容器信息: docker inspect 容器id 进入当前正在运行的容器 docker exec -it 容器id /bin/bash 从容器内拷贝文件到主机上 容器数据拷贝到主机: docker cp 容器id 宿主机的文件目录 查看内容占用:docker stats 1、后台启动docker,参数是 -ddocker run -d 镜像名
    问题:docker 容器使用后台运行,用docker ps 查看的时候 发现停止了 原因:后台运行,docker发现前台没有,容器启动后,发现自己没有提供服务,会立刻停止 2、查看日志docker logs
    docker logs -f -t --tail n 容器id

    [root@192 ~]# docker logs --help

    Usage: docker logs [OPTIONS] CONTAINER

    Fetch the logs of a container

    Options:
    --details Show extra details provided to logs
    -f, --follow Follow log output
    --since string Show logs since timestamp (e.g.
    2013-01-02T13:23:37) or relative
    (e.g. 42m for 42 minutes)
    --tail string Number of lines to show from the
    end of the logs (default "all")
    -t, --timestamps Show timestamps # 时间戳
    --until string Show logs before a timestamp (e.g.
    2013-01-02T13:23:37) or relative
    (e.g. 42m for 42 minutes)


    # 脚本
    while true;do echo shenzai;sleep

    # 查看日志,由于没有运行脚本,所以啥也没显示
    [root@192 ~]# docker logs -f -t --tail 10 c2887d35c71d
    ^C # ctrl+c退出

    # 运行centos里面加个脚本
    [root@192 ~]# docker run -d centos /bin/sh -c "while true;do echo shenzai;sleep 1;done"

    [root@192 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    cb6d7fbc3f27 centos "/bin/sh -c 'while t…" 7 seconds ago Up 6 seconds

    # 查看日志 发现隔一秒打印一条
    [root@192 ~]# docker logs -f -t --tail 10 cb6d7fbc3f27
    3、查看运行的容器,查看容器中的进程信息 pstop 命令,方便以后杀死进程命令:docker top 容器iddocker top 3cdbca722bab

    UID PID PPID C STIME TTY TIME
    root 24624 24603 0 22:22 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
    4、查看正在运行的容器信息(镜像元数据):inspect命令:docker inspect 容器id[root@192 ~]# docker inspect cb6d7fbc3f27
    [
    {
    # 容器的完整id
    "Id": "cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388",

    # 创建时间
    "Created": "2020-06-17T12:00:50.706906186Z",

    # 脚本位置
    "Path": "/bin/sh",

    # 运行的脚本
    "Args": [
    "-c",
    "while true;do echo shenzai;sleep 1;done"
    ],
    "State": {
    "Status": "running", # 状态,正在运行
    "Running": true,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 1909, # 父进程id
    "ExitCode": 0,
    "Error": "",
    "StartedAt": "2020-06-17T12:00:51.093617477Z",
    "FinishedAt": "0001-01-01T00:00:00Z"
    },

    # 来源镜像
    "Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
    "ResolvConfPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hostname",
    "HostsPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/hosts",
    "LogPath": "/var/lib/docker/containers/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388/cb6d7fbc3f27a064137d58282de97b97365dea2705211ebfbad642079cc1b388-json.log",


    "Name": "/dreamy_almeida",
    "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,
    "Capabilities": null,
    "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/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907-init/diff:/var/lib/docker/overlay2/7f79322e0f58d651a84a555dadd83d92537788172525945d3f538dd95dce336c/diff",
    "MergedDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/merged",
    "UpperDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/diff",
    "WorkDir": "/var/lib/docker/overlay2/3675586ebbd79cd72d2562a90c9380627a331c563724c0dac091f92600af4907/work"
    },
    "Name": "overlay2"
    },

    "Mounts": [], # 挂载

    # 基本配置
    "Config": {
    "Hostname": "cb6d7fbc3f27",
    "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"
    ], # 基本环境变量,这里没有Java

    # 基本命令
    "Cmd": [
    "/bin/sh",
    "-c",
    "while true;do echo shenzai;sleep 1;done"
    ],
    "Image": "centos",
    "Volumes": null,
    "WorkingDir": "",
    "Entrypoint": null,
    "OnBuild": null,
    "Labels": {
    "org.label-schema.build-date": "20200611",
    "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": "4d701985d7e77aa153790b697b2f38a61e20555c224b7675e4bf650b82799882",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {},
    "SandboxKey": "/var/run/docker/netns/4d701985d7e7",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.3",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:03",
    "Networks": {
    "bridge": {
    "IPAMConfig": null,
    "Links": null,
    "Aliases": null,
    "NetworkID": "22b0fd2290ccbc4e066a75d3f01bd8bf32ee4352c5bbcfc9f911287219219571",
    "EndpointID": "8a6c71e2bafb19ca7dfd85445ccc4bef6d17467360a243d624089e676a24a018",
    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.3",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:11:00:03",
    "DriverOpts": null
    }
    }
    }
    }
    ]

    5、进入当前正在运行的容器命令:docker exec -it 容器id bashSHELL● 区别:是否开启一个新的线程 docker exec 进入容器后开启一个新的终端,可以在里面操作(常用) docker attach 进入容器正在执行的终端,不会启动新的进程 # 我们通常容器都是使用后台方式运行的
    docker exec -it 容器id bashSHELL

    # 测试
    [root@192 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    c2887d35c71d centos "/bin/bash" 35 minutes ago Up 35 minutes
    [root@192 ~]# docker exec -it c2887d35c71d /bin/bash
    [root@c2887d35c71d /]# ls
    bin etc lib lost+found mnt proc run srv tmp var
    dev home lib64 media opt root sbin sys usr
    [root@c2887d35c71d /]# ps -ef
    UID PID PPID C STIME TTY TIME CMD
    root 1 0 0 11:57 pts/0 00:00:00 /bin/bash
    root 14 0 0 12:32 pts/1 00:00:00 /bin/bash
    root 28 14 0 12:32 pts/1 00:00:00 ps -ef
    [root@c2887d35c71d /]# c2887d35c71d
    [root@c2887d35c71d /]# exit
    exit

    # 方式二
    [root@192 ~]# docker attach c2887d35c71d
    [root@c2887d35c71d /]#
    6、从容器内拷贝文件到主机上 cp# 运行
    [root@192 ~]# docker run -it centos
    # ctrl P Q 不关闭退出,查看
    [root@0569081aa89c /]# [root@192 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    0569081aa89c centos "/bin/bash" 19 seconds ago Up 19 seconds
    hopeful_chebyshev

    # 查看主机home下无文件
    [root@192 ~]# cd /home
    [root@192 home]# ls

    # 进入正在运行的容器
    [root@192 home]# docker attach 0569081aa89c

    # 进入容器home目录
    [root@0569081aa89c /]#

    cd /home

    # 在目录中创建java文件
    [root@0569081aa89c home]#

    touch test.java

    # 退出并停止容器
    [root@0569081aa89c home]#

    exit
    exit

    # 查看现在运行的容器
    [root@192 home]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    # 容器虽然被停止,但是数据都会保留
    [root@192 home]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    0569081aa89c centos "/bin/bash" 3 minutes ago Exited (0) 8 seconds ago hopeful_chebyshev
    f589e5684a01 centos "/bin/bash" 44 minutes ago Exited (0) 44 minutes ago cranky_easley
    cb6d7fbc3f27 centos "/bin/sh -c 'while t…" 54 minutes ago Exited (137) 42 minutes ago dreamy_almeida
    c2887d35c71d centos "/bin/bash" 58 minutes ago Exited (127) 16 minutes ago vigorous_kare
    8ce188e5fee3 centos "/bin/bash" About an hour ago Exited (0) About an hour ago tender_dirac
    7b1a7dd10ea4 centos "/bin/bash" 9 hours ago Exited (0) 9 hours ago fervent_mirzakhani

    # 容器数据拷贝到主机
    [root@192 home]# docker cp 0569081aa89c:/home/test.java /home
    [root@192 home]# ls
    test.java

    # 拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步 /home /home
    7、查看内存占用

    statsdocker stats

  • 相关阅读:
    我的架构经验小结(四)-- 实战中演化的三层架构
    DCFramework 动态分布式计算框架(01)-- 基础结构
    XNA基础(01) —— 游戏循环
    使用动态代理记录方法执行的时间
    高性能的大型系统经验 -- 数据查询与分页
    成功的捷径?
    如何激励你的员工?
    DCFramework 动态分布式计算框架(00) -- 序
    在应用程序之间传递动态程序集
    圈 Circle--ESBasic 可复用的.NET类库(03)
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/16779216.html
Copyright © 2020-2023  润新知