三、Docker的常用命令

三、Docker的常用命令,第1张

1.帮助命令

官方帮助文档

https://docs.docker.com/engine/reference/commandline/docker/

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

2.镜像命令

dcoker images  # 查看本地主机上的镜像 (docker image ls)
docker search  # 搜索镜像
docker pull    # 下载镜像 (docker image pull)
docker rmi     # 删除镜像 (docker image rm)

查看本地的所有镜像

docker images

[root@fedora ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
tomcat        8         2d2bccf89f53   6 months ago   678MB
alpine        latest    c059bfaa849c   6 months ago   5.58MB
hello-world   latest    feb5d9fea6a5   9 months ago   13.3kB

# REPOSITORY # 镜像的仓库源
# TAG        # 镜像的标签
# IMAGE ID   # 镜像的id
# CREATED    # 镜像的创建时间
# SIZE       # 镜像的大小
# 可选项
Options:
-a, --all Show all images (default hides intermediate images) #列出所有镜像
-q, --quiet Only show numeric IDs # 只显示镜像的id

[root@fedora ~]# docker images -aq  #显示所有镜像的ID
2d2bccf89f53
feb5d9fea6a5

搜索镜像

docker search

[root@fedora ~]# docker search mysql
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                          MySQL is a widely used, open-source relation…   12751     [OK]       
mariadb                        MariaDB Server is a high performing open sou…   4895      [OK]       
percona                        Percona Server is a fork of the MySQL relati…   579       [OK]       
phpmyadmin                     phpMyAdmin - A web interface for MySQL and M…   555       [OK]  
......

# 可选项,通过搜索来过滤
 --filter=STARS=3000  # 搜索出来的镜像就是STARS大于3000的
 [root@fedora ~]# docker search mysql --filter=STARS=3000 
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12751     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4895      [OK]       
[root@fedora ~]# docker search mysql --filter=STARS=5000 #star大于5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12751     [OK] 

下载镜像

docker pull

# 下载镜像
# docker pull 镜像名[:tag]
[root@fedora ~]# docker pull mysql
Using default tag: latest   # 若不加tag,默认是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete # 分层下载,docker image 的核心,联合文件系统
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
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 pull mysql
docker pull docker.io/library/mysql:latest

# 指定版本下载
[root@fedora ~]# docker pull mysql:5.7
5.7: 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 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

[root@fedora ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   6 months ago   448MB
mysql         latest    3218b38490ce   6 months ago   516MB
hello-world   latest    feb5d9fea6a5   9 months ago   13.3kB

删除镜像

docker rmi

docker rmi -f 镜像ID   # 删除指定的镜像
docker rmi -f 镜像ID 镜像ID 镜像ID # 删除多个镜像
docker rmi -f $(docker images -aq) # 删除所有镜像
[root@fedora ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   6 months ago   448MB
mysql         latest    3218b38490ce   6 months ago   516MB
hello-world   latest    feb5d9fea6a5   9 months ago   13.3kB
[root@fedora ~]# docker rmi -f mysql:5.7 # 可以用镜像名加标签,也可以用镜像ID
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
......
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
[root@fedora ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    3218b38490ce   6 months ago   516MB
hello-world   latest    feb5d9fea6a5   9 months ago   13.3kB
[root@fedora ~]# docker rmi -f $(docker images -aq) # 删除所有的镜像
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
......
Untagged: hello-world:latest
Untagged: hello-world@sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@fedora ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

3.容器命令

有了镜像才可以创建容器,下载一个centos测试学习

[root@fedora ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

新建容器并启动

docker run [可选参数] image

# 参数说明
--name="NAME" # 对容器命名,便于区分容器 
-d            # 以后台方式运行
-it           # 使用交互方式运行,进入容器查看内容
-p            # 指定容器的端口,如-p 8080(宿主机):8080(容器)
   -p ip:主机端口:容器端口
   -p 主机端口:容器端口(常用)
   -p 容器端口
   容器端口
-P(大写)      # 随机指定端口
# 测试,启动并进入容器
[root@fedora ~]# docker run -it centos /bin/bash 
[root@c0b3a59a4b40 /]# ls # 查看容器内的centos,基础版本,很多命令都不完善!
bin  dev  etc  home  lib  lib64  lost found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 从容器中退回主机 
[root@c0b3a59a4b40 /]# exit
exit
[root@fedora ~]# ls
anaconda-ks.cfg

列出所有运行的容器

# docker ps 命令
     # 列出当前正在运行的容器
-a   # 列出当前正在运行的容器 历史运行过的容器
-n=? # 列出最近创建的容器
-q   # 只显示容器的编号

[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@fedora ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
c0b3a59a4b40   centos         "/bin/bash"   8 minutes ago   Exited (0) 7 minutes ago             amazing_mestorf
4e2856b49ab3   feb5d9fea6a5   "/hello"      5 hours ago     Exited (0) 5 hours ago               practical_elion

[root@fedora ~]# docker ps -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
c0b3a59a4b40   centos    "/bin/bash"   8 minutes ago   Exited (0) 8 minutes ago             amazing_mestorf

[root@fedora ~]# docker ps -aq
c0b3a59a4b40
4e2856b49ab3

退出容器

exit     # 退出容器,该容器停止运行
Ctrl P Q # 退出容器,该容器继续运行
[root@fedora ~]# docker run -it centos /bin/bash
[root@850f79cbaca0 /]# exit
exit
[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@fedora ~]# docker run -it centos /bin/bash
[root@3ba161d2b592 /]# [root@fedora ~]# 
[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
3ba161d2b592   centos    "/bin/bash"   26 seconds ago   Up 25 seconds             modest_agnesi

删除容器

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

启动和停止容器

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

4.其它常用命令

后台启动容器

# dcoker run -d 镜像名
[root@fedora ~]# docker run -d centos
e1050d6f94c31e0285f0ac13cd1660db486149b0a6c50b76eb514ce01491c04b
[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
# 问题:dcoker ps,发现centos停止了
# 常见的坑:dcoker容器使用后台运行,就必须要有一个前台进程,docker发现没有进程,就会自动停止
# 如:nginx,容器启动后,发现自己没有提供服务,就会立即停止,就是没有进程了

查看日志

docker logs -f -t --tail 容器,没有日志

#自己编写一段shell脚本
[root@fedora ~]# docker run -d centos
e1050d6f94c31e0285f0ac13cd1660db486149b0a6c50b76eb514ce01491c04b
[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@fedora ~]# docker run -d centos /bin/bash -c "while true;do echo hello;sleep 1;done"
988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca
[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
988efb267904   centos    "/bin/bash -c 'while…"   6 seconds ago   Up 4 seconds             boring_blackburn

# 显示日志
-tf           # 显示日志(一直更新)
--tail number # 显示n行日志

查看容器中进程的信息

# docker top 容器ID
[root@fedora ~]# docker top 988efb267904
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                33380               33362               0                   15:38               ?                   00:00:00            /bin/bash -c while true;do echo hello;sleep 1;done
root                34367               33380               0                   15:50               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的元数据

docker inspect 容器ID

# docker inspect 容器ID
# 测试
[root@fedora ~]# docker inspect 988efb267904
[
    {
        "Id": "988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca",
        "Created": "2022-06-21T07:38:59.595458076Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo hello;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 33380,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-06-21T07:39:00.49608619Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca/hostname",
        "HostsPath": "/var/lib/docker/containers/988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca/hosts",
        "LogPath": "/var/lib/docker/containers/988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca/988efb2679049c3b1ea591b049d6be23e3c040caff84af67c27c1cd9098927ca-json.log",
        "Name": "/boring_blackburn",
        "RestartCount": 0,
        "Driver": "btrfs",
        "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": "private",
            "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": null,
            "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": null,
            "Name": "btrfs"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "988efb267904",
            "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 hello;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": "980493ae95818c3356a6e8f00b9b3e81881eacf575ee2c50abdd3c43cec66cf8",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/980493ae9581",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "842c33105ad065a4b9cb6e14028586ac0f80764101d5c6986e93da5fb896db1a",
            "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": "2e62a3517f915ae7957898e76b21c2c3be3c078f2b03822f8155c2bbf77fa112",
                    "EndpointID": "842c33105ad065a4b9cb6e14028586ac0f80764101d5c6986e93da5fb896db1a",
                    "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
                }
            }
        }
    }
]

进入当前正在运行的容器

# 我们通常容器都是以后台方式运行的,需要进入容器,修改一些配置

# dcoker exec -it 容器ID bashshell

[root@fedora ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
988efb267904   centos    "/bin/bash -c 'while…"   19 minutes ago   Up 19 minutes             boring_blackburn

[root@fedora ~]# docker exec -it 988efb267904 /bin/bash
[root@988efb267904 /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 07:38 ?        00:00:00 /bin/bash -c while true;do echo hello;sleep 1;done
root        1236       0  0 07:59 pts/0    00:00:00 /bin/bash
root        1269       1  0 07:59 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root        1270    1236  0 07:59 pts/0    00:00:00 ps -ef
# 方式二
docker attach 容器id
#测试
docker attach 988efb267904
正在执行当前的代码...

区别
# docker exec   # 进入当前容器后开启一个新的终端,可以在里面操作。(常用)
# docker attach # 进入容器正在执行的终端

从容器内拷贝文件到主机

[root@fedora ~]# docker run -it centos  # 进入容器
[root@3593a6485284 /]# cd /home/        
[root@3593a6485284 home]# touch hello   # 在容器内新建一个文件
[root@3593a6485284 home]# ls  
hello
[root@3593a6485284 home]# exit # 退出容器
exit
[root@fedora ~]# docker ps -a # exit退出,容器被关闭。但不影响文件的拷贝
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                       PORTS     NAMES
3593a6485284   centos         "/bin/bash"              3 minutes ago    Exited (0) 8 seconds ago               sad_jepsen

[root@fedora ~]# docker cp 3593a6485284:/home/hello /root 
[root@fedora ~]# ls
anaconda-ks.cfg  hello

学习方式:将所有的命令敲一遍,自己记录笔记!

文章来源:https://www.cnblogs.com/simplerude/p/16396022.html

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复