1.帮助命令

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

帮助文档地址

2.镜像命令

  1. docker images 查看所有本地主机的镜像
[root@hecs~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB
# 解释
REPOSITORY      镜像仓库源  
TAG                镜像的标签
IMAGE ID         镜像的id
CREATED            镜像创建时间
SIZE             镜像的大小

#可选项
  -a, --all             # 列出所有镜像
  -q, --quiet          # 只显示镜像的id
  1. docker search 搜索镜像
[root@hecs~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9686                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3523                [OK]  

# 可选项,通过收藏来过滤
    --filter=STARS=3000     搜索出来的镜像就是STARS大于3000的
    
# 执行结果
[root@hecs ~]# docker search mysql --filter=STARS=3000
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql               MySQL is a widely used, open-source relation…   9686                [OK]                
mariadb             MariaDB is a community-developed fork of MyS…   3523                [OK] 
  1. docker pull 下载镜像,后面一般加版本号
#下载镜像 docker pull 镜像名 [:tag]

[root@hecs ~]# docker pull mysql
Using default tag: latest        # 如果不写 tag 版本,默认就是最新的
latest: Pulling from library/mysql
8559a31e96f4: Pull complete     # 分层下载,docker image的核心 联合文件系统
d51ce1c2e575: Pull complete 
c2344adc4858: Pull complete 
fcf3ceff18fc: Pull complete 
16da0c38dc5b: Pull complete 
b905d1797e97: Pull complete 
4b50d1c6b05c: Pull complete 
c75914a65ca2: Pull complete 
1ae8042bdd09: Pull complete 
453ac13c00a3: Pull complete 
9e680cd72f08: Pull complete 
a6b5dc864b6c: Pull complete 
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest    # 真实地址

# 这两个代码等价
docker pull mysql 
docker pull docker.io/library/mysql:latest

#指定版本下载
[root@hecs~]# docker pull mysql:8.0
8.0: Pulling from library/mysql
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6
Status: Downloaded newer image for mysql:8.0
docker.io/library/mysql:8.0

  1. docker rmi 删除镜像
docker rmi -f 容器id  # 通过id删除镜像
docker rmi -f 容器id 容器id 容器id  # 通过id删除多个镜像
docker rmi -f $(docker images -aq) # 删除全部镜像

3.容器命令

  1. 说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos 
  1. 新建容器并启动
docker run [可选参数] image

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

#测试
[root@2fc2a4826a82 /]# docker run -it centos /bin/bash # 启动并进入容器

# 查看容器内的centos,基础版本,很多命令是不全的
[root@2fc2a4826a82 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器退回主机
[root@2fc2a4826a82 /]# exit
exit
  1. docker ps 列出所有运行中的命令
docker ps        # 列出正在运行的容器
docker ps -a    # 列出当前运行的容器 和 曾运行过的容器
docker ps -a -n=?    # 列出最近创建的容器
docker ps -aq         # 只显示容器的编号

[root@hecs ]# docker ps        # 列出正在运行的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@hecs ]# docker ps -a    # 列出当前运行的容器 和 曾运行过的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
2fc2a4826a82        centos              "/bin/bash"         5 minutes ago       Exited (0) 3 minutes ago                       mystifying_kowalevski

[root@hecs~]# docker ps -aq    # 只显示容器的编号
2fc2a4826a82
  1. 退出容器
exit    # 直接容器停止并退出
ctrl + p + Q # 容器不停止退出
  1. 删除容器
docker rm 容器id        # 删除容器,不能删除正在运行的容器
docker rm -f 容器id # 强制删除容器
docker rm -f $(docker ps -aq)    # 递归删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
  1. 启动和停止容器的操作
docker start 容器id        #启动容器
docker restart 容器id    #重启容器
docker stop 容器id        #停止当前正在运行的容器
docker kill 容器id         #强制停止当前容器

4.常用命令

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

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

#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动关闭
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有进程了
  1. 查看日志
docker logs -f -t --tail 容器 ,没有日志

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

# 执行结果
[root@hecs~]# docker ps
CONTAINER ID        IMAGE               
a4be9897af89        centos   

# 显示日志
-tf        # 显示日志
--tail 数字    # 要显示日志的数量
[root@hecs ~]# docker logs -tf --tail 10  a4be9897af89
  1. 查看容器中进程信息 ps
# 命令 docker top 容器id
[root@hecs ~]# docker top a4be9897af89
UID                 PID                 PPID                C                   STIME               TTY                 TIME                
root                13466               13450               0                   13:39               ?                   00:00:00           
root                13871               13466               0                   13:45               ?                   00:00:00            
  1. 查看镜像的元数据
# 命令 docker inspect 容器id
[root@hecs ~]# docker inspect a4be9897af89
[
    {
        "Id": "a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32",
        "Created": "2020-06-29T05:39:59.073122824Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo violet;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 13466,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-06-29T05:39:59.494363561Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
        "ResolvConfPath": "/var/lib/docker/containers/a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32/hostname",
        "HostsPath": "/var/lib/docker/containers/a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32/hosts",
        "LogPath": "/var/lib/docker/containers/a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32/a4be9897af89f402e04c205f9f6b6097c7c207a940565ac104ef54c3bf35eb32-json.log",
        "Name": "/wizardly_pike",
        "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/b071452c71b9fd6dfb15b5d9a463808028508f677d4d8f1be1c4f3b2cf303c39-init/diff:/var/lib/docker/overlay2/3e51580ac4e83d2a54b4eb7fc656342a1b8a4de7c94d1b700e38d8272958f0f3/diff",
                "MergedDir": "/var/lib/docker/overlay2/b071452c71b9fd6dfb15b5d9a463808028508f677d4d8f1be1c4f3b2cf303c39/merged",
                "UpperDir": "/var/lib/docker/overlay2/b071452c71b9fd6dfb15b5d9a463808028508f677d4d8f1be1c4f3b2cf303c39/diff",
                "WorkDir": "/var/lib/docker/overlay2/b071452c71b9fd6dfb15b5d9a463808028508f677d4d8f1be1c4f3b2cf303c39/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "a4be9897af89",
            "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 violet;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": "9afc30c3fd63abde667b2424475711afc20cd61cf2036b98377d9215858dfe07",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/9afc30c3fd63",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "e9c8632c1520fe7cb3fa1cf3d968a431d4e56c7cc0f037a406b09a6038568558",
            "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": "1b1009c8e0924f6d8da035fa8580632962e96ad1e2d43a1c6308a0fc98883548",
                    "EndpointID": "e9c8632c1520fe7cb3fa1cf3d968a431d4e56c7cc0f037a406b09a6038568558",
                    "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
                }
            }
        }
    }
]
  1. 进入当前正在运行的容器
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

#命令
docker exec -it 容器id bashShell

# 测试
[root@hecs ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a4be9897af89        centos              "/bin/sh -c 'while t…"   12 minutes ago      Up 12 minutes                           wizardly_pike
[root@hecs ~]# docker exec -it a4be9897af89 /bin/bash
[root@a4be9897af89 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@a4be9897af89 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 05:39 ?        00:00:00 /bin/sh -c while true;do echo violet;sleep 1;done
root       795     0  1 05:53 pts/0    00:00:00 /bin/bash
root       818     1  0 05:53 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root       819   795  0 05:53 pts/0    00:00:00 ps -ef

# 方式二
docker attach 容器id

# 测试
[root@hecs ~]# docker attach a4be9897af89
正在执行当前代码...

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

[root@hecs home]# docker cp a49f0237565f:/home/text.txt /home
[root@hecs home]# ls
text.txt  violet.txt  www

5.小结