Docker 常用命令

帮助命令

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

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

镜像命令

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

$ docker images

Docker 常用命令

REPOSITORY:镜像的仓库源
TAG:镜像的id
IMAGE ID:镜像的id
CREATED:镜像创建的时间
SIZE:镜像的大小

可选项:

Options:
  -a, --all             # 列出所有的镜像
  -q, --quiet         # 只显示镜像的id

docker search 搜索镜像命令
可选项,通过收藏来过滤

-- filter=stars=3000    # 搜索出来的镜像就是STARTS大于3000的

$ docker search mysql --filter=stars=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9771                [OK]
mariadb             MariaDB is a community-developed fork of MyS…   3566                [OK]
  • 下载镜像
docker pull 镜像名字[:tag]

$ docker pull mysql
Using default tag: latest    # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
6ec8c9369e08: Pull complete    # 分层下载,docker image的核心 联合文件系统
177e5de89054: Pull complete
ab6ccb86eb40: Pull complete
e1ee78841235: Pull complete
09cd86ccee56: Pull complete
78bea0594a44: Pull complete
caf5f529ae89: Pull complete
cf0fc09f046d: Pull complete
4ccd5b05a8f6: Pull complete
76d29d8de5d4: Pull complete
8077a91f5d16: Pull complete
922753e827ec: Pull complete
Digest: sha256:fb6a6a26111ba75f9e8487db639bc5721d4431beba4cd668a4e922b8f8b14acc    # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest    # 真实地址

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

# 指定版本下载
$ docker pull mysql:8.0.2

$ docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec8c9369e08: Already exists
177e5de89054: Already exists
ab6ccb86eb40: Already exists
e1ee78841235: Already exists
09cd86ccee56: Already exists
78bea0594a44: Already exists
caf5f529ae89: Already exists
4e54a8bcf566: Pull complete
50c21ba6527b: Pull complete
68e74bb27b39: Pull complete
5f13eadfe747: Pull complete
Digest: sha256:97869b42772dac5b767f4e4692434fbd5e6b86bcb8695d4feafb52b59fe9ae24
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
  • 删除镜像
$ docker rmi -f 容器id    # 删除指定镜像
$ docker rmi -f 容器id 容器id 容器id    # 删除多个镜像
$ docker rmi -f $(docker images -aq)    # 删除全部镜像
$ docker rmi -f 8679ced16d20
Untagged: mysql:5.7
Untagged: mysql@sha256:97869b42772dac5b767f4e4692434fbd5e6b86bcb8695d4feafb52b59fe9ae24
Deleted: sha256:8679ced16d206961b35686895b06cfafefde87ef02b518dfc2133081ebf47cda
Deleted: sha256:355f87dc5125a32cc35898a4dde17fb067585bc0d86704b5a467c0ccc0eea484
Deleted: sha256:8299d5c38042216210125535adb2600e46268a0e2b9ec799d12ea5b770236e79
Deleted: sha256:07311a303b2c7cf2ac6992aaf68e12326fe7255985166939cbab7d18b10e0f47
Deleted: sha256:306c9bc1ce2997d000bb6f1ea4108420d9752df93ce39164b7a2f876b954afc4

容器命令

说明:有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习。

$ docker pull centos
  • 新建容器并启动
$ docker run [可选参数] image

# 参数说明
--name="Name"    容器名字 tomcat01    tomcat02...用来区分容器
-d    后台运行
-it    使用交互方式运行,进入容器查看内容
-P    指定容器的端口 -p 8080:8080
        -p ip:主机端口:容器端口
        -p 主机端口:容器端口(常用)
        -p 容器端口
        容器端口
-p    随机指定端口
  • 测试
# 启动并进入容器,
$ docker run -it centos /bin/bash
[root@337adc837117 /]#
# 查看容器内的centos,基础版本,很多命令都不完善
[root@337adc837117 /]# ls
# 从容器中,退回主机
[root@337adc837117 /]# exit
  • 列出所有的运行的容器
# docker ps 命令
-a    # 列出当前正在运行的容器+带出历史运行过的容器
-n=?    # 显示最近创建的几个容器
-q    # 只显示容器的编号

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
337adc837117        centos              "/bin/bash"         5 minutes ago       Exited (0) About a minute ago                       dazzling_johnson
bac8cdc864c0        hello-world         "/hello"            20 hours ago        Exited (0) 20 hours ago                             romantic_cartwright

Docker 常用命令

  • 退出容器
exit    # 直接容器停止并退出

Ctrl+P+Q 容器不停止退出

Docker 常用命令

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

Docker 常用命令

  • 启动和停止容器的操作
    $ docker start 容器id    #启动容器
    $ docker restart 容器id
    $ docker stop 容器id    #停止当前正在运行的容器
    $ docker kill 容器id    #强制停止当前容器

其它命令

  • 后台启动容器
# 命令 docker run -d 镜像名
$ docker run -d centos

# 问题 docker ps,发现centos停止了

# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了。
  • 查看日志
$ docker logs -tf    #查看所有日志,-f代表format,-t时间戳
$ docker logs -f -t --tail nunber 容器id    #显示最近的几条日志

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

Docker 常用命令

  • 查看容器中的进程信息
$ docker top 容器id

Docker 常用命令

  • 查看镜像的元数据
$ docker inspect 容器id
$ docker inspect 0274048b4a70
[
    {
        "Id": "0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f",
        "Created": "2020-07-28T07:10:54.60528018Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2132,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-07-28T07:12:59.293433712Z",
            "FinishedAt": "2020-07-28T07:10:58.925790055Z"
        },
        "Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
        "ResolvConfPath": "/var/lib/docker/containers/0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f/hostname",
        "HostsPath": "/var/lib/docker/containers/0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f/hosts",
        "LogPath": "/var/lib/docker/containers/0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f/0274048b4a70397575bf3e28c6ccbdcabdec919a968adfcf0643d240ba83af2f-json.log",
        "Name": "/sweet_kilby",
        "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/ab11dcc7c8f59497838a5d386678ad1bab0fe598b362029408d76e3a5a09e86f-init/diff:/var/lib/docker/overlay2/751a4a95bdbb48f264e3ff3135e877f7012b57a336cf2afbe58a75e375264eaa/diff",
                "MergedDir": "/var/lib/docker/overlay2/ab11dcc7c8f59497838a5d386678ad1bab0fe598b362029408d76e3a5a09e86f/merged",
                "UpperDir": "/var/lib/docker/overlay2/ab11dcc7c8f59497838a5d386678ad1bab0fe598b362029408d76e3a5a09e86f/diff",
                "WorkDir": "/var/lib/docker/overlay2/ab11dcc7c8f59497838a5d386678ad1bab0fe598b362029408d76e3a5a09e86f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "0274048b4a70",
            "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": "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": "ea87bb4665a0120dd05c00039cc2f55831a3065078dfb54c88260a3ec78ddbc1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/ea87bb4665a0",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "68e044efbb8e08c691cce70281dacda3e9a7ca05d8e81eb9f455cb008d45da16",
            "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": "7f65e1d8deb9e23f4f26d3117e7ecf54e69e927d9735f66cb248f2f03e78a330",
                    "EndpointID": "68e044efbb8e08c691cce70281dacda3e9a7ca05d8e81eb9f455cb008d45da16",
                    "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 bashShell
$ docker exec -it 0274048b4a70 /bin/bash

Docker 常用命令

方式二

$ docker attach 容器id
$ docker attach 0274048b4a70
正在执行当前的代码。。。

Docker 常用命令

区别

$ docker exec    # 进入容器后开启一个新的终端,可以在里面操作(常用)
$ docker attach    # 进入容器正在执行的终端,不会启动新的进程
  • 从容器内拷贝文件到主机上
$ docker cp 容器id:容器内路径 目的的主机路径

$ docker cp 3b4011be5c56:/home/test.java ~/File/test

Docker 常用命令

拷贝是一个手动的过程,未来我们使用 -v 卷的技术,可以实现自动同步容器内的/home和主机的/home

Docker 常用命令

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!