【Docker学习笔记】Docker常用命令学习

Docker的常用命令

帮助命令

docker --version  # 查看版本信息
docker info # 查看详细信息
docker --help # 查看帮助信息

镜像命令

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

master@lizhipengdeMacBook-Pro ~ % docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

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

# 可选项
  -a, --all             列出所有镜像
  -q, --quiet           只显示镜像的ID
   

docker search 镜像搜索

master@lizhipengdeMacBook-Pro ~ % docker search php
NAME                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
php                        While designed for web development, the PHP …   5375                [OK]

# 可选项,通过搜索过滤
-- filter=STARS=3000 # 搜索STARS大于3000的

master@lizhipengdeMacBook-Pro ~ % docker search php --filter=STARS=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
php                 While designed for web development, the PHP …   5375                [OK]

docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
master@lizhipengdeMacBook-Pro ~ % docker pull php
# 不写tag默认为latset
Using default tag: latest
6ec8c9369e08: Pull complete
081a822af595: Pull complete
bb5bea655fca: Pull complete
1e5d9e6a44c7: Pull complete
da8a51e623d9: Pull complete
cd2df0beae26: Pull complete
514e63ef36f2: Pull complete
e8488b2212fe: Pull complete
042251161f0b: Pull complete
# 签名
Digest: sha256:b868fcb2d9be8322ad884c863af3255332eb357fc9fe0156107d32e405e18c54
Status: Downloaded newer image for php:latest
# 地址
docker.io/library/php:latest

# 指定版本下载
master@lizhipengdeMacBook-Pro ~ % docker pull php:7.3

docker rmi 删除镜像

# 删除指定ID的镜像,可删除多个 docker rmi -f 容器ID 容器ID 容器ID 容器ID
master@lizhipengdeMacBook-Pro ~ % docker rmi -f 7d7c6be4f840  
Untagged: php:7.3
Untagged: php@sha256:c8fa7be665b534667af68e6eef204b2512be252b0e73e76caf3cedfd6c5ff8fd
Deleted: sha256:7d7c6be4f84094b562dedd634f0c5883689ddba2316c6c6e44215527f50e4632
Deleted: sha256:4f0604b633e709da00af8241135609c17a5df727f11988d710b8d0b28d42d4b9
Deleted: sha256:201ae7bf350add1f09532c7ecbfd6e261e6d5e211e6aa1f04afa77893fa4939f
Deleted: sha256:f7688e2cf3f87f3ff8710998afb7b2816118362b200480e9242b4ff5fbc31663
Deleted: sha256:933ab32183b38f25973dc9318f8e112c58545ea0857b62f2c6fcd5f9fb69d370
Deleted: sha256:af05e4b6ed7832860b7bcc4bbb412be4ddae206051258a8bf59ae041f68e3d42
Deleted: sha256:a75f698e001d9b4b5efaf8b0148cdacc29620a8bd37cfa6436d40e9b0104da6b

# 删除全部的镜像
docker rmi -f $(docker images -aq)

容器命令

新建容器并启动

docker run [可选参数] image

# 参数说明
-- name=“name”  # 容器名字
-d				# 后台方式运行
-it				# 交互方式运行,进入容器查看内容
-P				# 指定容器的端口 -p 8080:8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
	容器端口
-p				# 随机指定端口


# 测试,启动并进入容器

master@lizhipengdeMacBook-Pro ~ % docker run -it php /bin/bash
root@1907dcea0da4:/# 

# 退出
root@1907dcea0da4:/# exit;
exit

列出所有的运行的容器

# docker ps 命令
# docker ps	 列出当前正在运行的容器
# docker ps -a 列出所有的容器
# docker ps -n=m 列出最近创建的m个容器
# docker ps -aq  列出所有的容器的CONTAINER ID

master@lizhipengdeMacBook-Pro ~ % docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

master@lizhipengdeMacBook-Pro ~ % docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
1907dcea0da4        php                 "docker-php-entrypoi…"   8 minutes ago       Exited (0) 5 minutes ago                       vibrant_faraday

master@lizhipengdeMacBook-Pro ~ % docker ps -n=1
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
1907dcea0da4        php                 "docker-php-entrypoi…"   8 minutes ago       Exited (0) 6 minutes ago                       vibrant_faraday

master@lizhipengdeMacBook-Pro ~ %  docker ps -aq
1907dcea0da4

退出容器

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

删除容器

docker rm 容器id  # 删除指定的容器
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q| xargs docker rm # 删除所有的容器

# 删除运行中的容器,报异常,需强制删除 rm -f
master@lizhipengdeMacBook-Pro ~ % docker rm a037f8f3bf19
Error response from daemon: You cannot remove a running container a037f8f3bf19e64cd1ac8a9164c09fbe0ffdde568f852923f83f988b901bd573. Stop the container before attempting removal or force remove

master@lizhipengdeMacBook-Pro ~ % docker ps -a -q| xargs docker rm
master@lizhipengdeMacBook-Pro ~ % docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

启动和停止容器的操作

docker start 容器id   # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id    # 停止容器
docker kill 容器id    # 强制停止当前容器


master@lizhipengdeMacBook-Pro ~ % docker start d8b4bdded727
d8b4bdded727

master@lizhipengdeMacBook-Pro ~ % docker restart d8b4bdded727
d8b4bdded727

master@lizhipengdeMacBook-Pro ~ % docker stop d8b4bdded727
d8b4bdded727

master@lizhipengdeMacBook-Pro ~ % docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d8b4bdded727        php                 "docker-php-entrypoi…"   2 minutes ago       Up 2 seconds                            hardcore_driscoll
master@lizhipengdeMacBook-Pro ~ % docker kill d8b4bdded727
d8b4bdded727

常用其他命令

后台启动容器

# 命令 docker run -d 镜像名
master@lizhipengdeMacBook-Pro ~ % docker run -d php
65e612a4798f69ee95aada31436b82b8398b9ac727366276d43f73535f834aa7

# 问题docker ps 发现php 停止了

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

查看日志

# 命令 docker logs -f -t --tail 日志条数 

master@lizhipengdeMacBook-Pro ~ % docker logs -f -t --tail 10 92e9a5eccd14

# 显示日志
-t		# 显示时间戳
-f 		# 显示日志
--tial number # 要显示日志条数

查看容器中进程信息

# 命令 docker top 容器id

查看镜像的元数据

# 命令  docker inspect 容器id
# 测试
master@lizhipengdeMacBook-Pro ~ % docker inspect 92e9a5eccd14
[
    {
        "Id": "92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc",
        "Created": "2020-07-29T14:26:28.6879799Z",
        "Path": "docker-php-entrypoint",
        "Args": [
            "/bin/sh",
            "-c",
            "while true;do echo lizhipeng;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2726,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-07-29T14:26:29.1080868Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:960551db44e4a4d6d6317fc799517e71a62018bf89a57cffea950f8ca118c615",
        "ResolvConfPath": "/var/lib/docker/containers/92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc/hostname",
        "HostsPath": "/var/lib/docker/containers/92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc/hosts",
        "LogPath": "/var/lib/docker/containers/92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc/92e9a5eccd14fcf068dc61a5f2c430dddeea8b0ff24c8f10e2ebcf45962515dc-json.log",
        "Name": "/jovial_vaughan",
        "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/190a8cb16f746a2d67b867bff9dd4b6f307bde61f4f37b3b92d8397a241a7631-init/diff:/var/lib/docker/overlay2/009c4848a039b27aaffc2d4141cb189536075c938aadb4527eee622960b77b5a/diff:/var/lib/docker/overlay2/ec0ff6603ebc0ab32a2981886992de373f1827c3a021e6980b2c652df350b581/diff:/var/lib/docker/overlay2/760ece6e83cd0146e2dc42108c2c7c696645f636f365ae027893d740e7b70a42/diff:/var/lib/docker/overlay2/26902cbee80b10a35106d68a6f5065c0a418c27c528f88b9dd7424a23990a848/diff:/var/lib/docker/overlay2/25e33d8716a91730d213dad26645d833d48e58abda359b1c7f830c290599f599/diff:/var/lib/docker/overlay2/8c444a320cacc709aad97afe5f5b1325873f1f6b65f571cb7e35b27490eb6703/diff:/var/lib/docker/overlay2/2a517e0e44e3b80201274de63a7a7efab5a414a965d640f265a28d67ea169059/diff:/var/lib/docker/overlay2/eedc3535793c93c73f49d47096a492d20bcc94bd01f52604e47f26eb1f3405e1/diff:/var/lib/docker/overlay2/fce6cce592e88606181c919e3426f4b52767a9cbe22a9034df1bd870eaa06fac/diff",
                "MergedDir": "/var/lib/docker/overlay2/190a8cb16f746a2d67b867bff9dd4b6f307bde61f4f37b3b92d8397a241a7631/merged",
                "UpperDir": "/var/lib/docker/overlay2/190a8cb16f746a2d67b867bff9dd4b6f307bde61f4f37b3b92d8397a241a7631/diff",
                "WorkDir": "/var/lib/docker/overlay2/190a8cb16f746a2d67b867bff9dd4b6f307bde61f4f37b3b92d8397a241a7631/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "92e9a5eccd14",
            "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",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkg-config \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64",
                "PHP_LDFLAGS=-Wl,-O1 -pie",
                "GPG_KEYS=42670A7FE4D0441C8E4632349E4FDC074A4EF02D 5A52880781F755608BF815FC910DEB46F53EA312",
                "PHP_VERSION=7.4.8",
                "PHP_URL=https://www.php.net/distributions/php-7.4.8.tar.xz",
                "PHP_ASC_URL=https://www.php.net/distributions/php-7.4.8.tar.xz.asc",
                "PHP_SHA256=642843890b732e8af01cb661e823ae01472af1402f211c83009c9b3abd073245",
                "PHP_MD5="
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo lizhipeng;sleep 1;done"
            ],
            "Image": "php",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": [
                "docker-php-entrypoint"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "2575a22b7b7f59d1fe9938757acc7098e65783f308a06246d77e8d29ebc3f0a4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/2575a22b7b7f",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "007672071e54dfcfbf43cb8f0806a1c899105f62d511a43fea1623730cb851a6",
            "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": "af3ed9ef1e4b900d6d85afca15973e8d0afc67bc2c5de1e97e89ad9fe19cea51",
                    "EndpointID": "007672071e54dfcfbf43cb8f0806a1c899105f62d511a43fea1623730cb851a6",
                    "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 

master@lizhipengdeMacBook-Pro ~ % docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
92e9a5eccd14        php                 "docker-php-entrypoi…"   11 minutes ago      Up 11 minutes                           jovial_vaughan
master@lizhipengdeMacBook-Pro ~ % docker exec -it 92e9a5eccd14 /bin/bash
root@92e9a5eccd14:/# 
posted @ 2020-07-29 22:45  雪随冬行  阅读(520)  评论(0编辑  收藏  举报