docker常用的其他命令

 

1,后台启动容器

docker run -d 镜像名

 

[root@CentOs /]# docker run -d centos
e864279524525ed8f6fd7892b4c4708750c6b9edcbe5e3c670bd8a47b91e45f1
[root@CentOs /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@CentOs /]#

问题:我们发现docker ps 后,没有启动的容器

原因:docker 容器使用后台运行后,就必须有一个前台进程,如果发现没有提供服务,就会自动停止,就没有了程序。

 

2,查看日志

docker logs -f -t --tail 10 容器

  -tf:显示日志

  --tail number :显示日志条数

 

3,查看容器中的进程信息

docker top 容器id

[root@CentOs ~]# docker top d658d763ff47
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7801                7780                0                   19:30               pts/0               00:00:00            /bin/bash
(UID是用户id,PID是进程id,PPID是父进程id)

4,查看镜像元数据

docker inspect 容器id

 

测试:

[root@CentOs ~]# docker inspect 6958d537090a
[
    {
        "Id": "6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e",
        "Created": "2022-03-18T09:16:33.36559517Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2022-03-18T09:18:10.320875186Z",
            "FinishedAt": "2022-03-18T09:18:29.515547323Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e/hostname",
        "HostsPath": "/var/lib/docker/containers/6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e/hosts",
        "LogPath": "/var/lib/docker/containers/6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e/6958d537090ad05d8d5e6d887bb4f774dcca93062c7cbb7d37c5617e413a7f5e-json.log",
        "Name": "/wonderful_ride",
        "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,
            "CgroupnsMode": "host",
            "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/bbbf65ea42dea586fe35c75e21a6814a6bc04f6252f9fda53f2d0c4da805f838-init/diff:/var/lib/docker/overlay2/925622667cd55bf2260aa2fb7c50ec1fd4c50dc5cb685272afe7b951ddced930/diff",
                "MergedDir": "/var/lib/docker/overlay2/bbbf65ea42dea586fe35c75e21a6814a6bc04f6252f9fda53f2d0c4da805f838/merged",
                "UpperDir": "/var/lib/docker/overlay2/bbbf65ea42dea586fe35c75e21a6814a6bc04f6252f9fda53f2d0c4da805f838/diff",
                "WorkDir": "/var/lib/docker/overlay2/bbbf65ea42dea586fe35c75e21a6814a6bc04f6252f9fda53f2d0c4da805f838/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "6958d537090a",
            "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": "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": "02862665df003d3ea3518893bb7dcab1baf938924fb269512bdd91ede3975196",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/02862665df00",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "73208f28d3335735d2759427ddb4133b74adb783f038445ae2869b9681f64c69",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@CentOs ~]#

 

 

5,进入当前正在运行的容器(重要!!!)

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

 

方式一:

docker exec -it 容器id bash/shell

 

测试:

[root@CentOs ~]# docker exec -it d658d763ff47 /bin/bash
[root@d658d763ff47 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 11:30 pts/0    00:00:00 /bin/bash
root         16      0  0 11:45 pts/1    00:00:00 /bin/bash
root         30     16  0 11:45 pts/1    00:00:00 ps -ef
[root@d658d763ff47 /]#

方式二:

docker attach 容器id

 

测试:

[root@CentOs ~]# docker attach d658d763ff47 /bin/bash

 

二者的区别:

docker exec     进入容器开启一个新的终端,可以在里面操作(常用)

docker attach  进入容器正在执行的终端,不会启动新的终端

 

 

6,把容器内部的文件拷贝到主机下

docker cp 容器id:容器内路径  目的主机路径

 

测试:

查看当前主机目录下

[root@CentOs home]# ls
slime  Slime
[root@CentOs home]# touch wl.java
[root@CentOs home]# ls
slime  Slime  wl.java

 

进入docker容器内部
[root@CentOs home]# docker attach ddb33fd19107
[root@ddb33fd19107 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@ddb33fd19107 /]# cd /home/
[root@ddb33fd19107 home]# ls

 

在容器内新建一个文件
[root@ddb33fd19107 home]# touch test.java
[root@ddb33fd19107 home]# ls
test.java
[root@ddb33fd19107 home]# exit
exit
[root@CentOs home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@CentOs home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
ddb33fd19107         centos    "/bin/bash"   2 minutes ago   Exited (0) 17 seconds ago             blissful_booth

 

将新建的文件拷贝到主机上
[root@CentOs home]# docker cp ddb33fd19107:/home/test.java /home/
[root@CentOs home]# ls
slime  Slime  test.java  wl.java
[root@CentOs home]#

拷贝是个手动的过程,未来我们可以使用-v卷的技术,实现容器和主机互通

 

posted @ 2022-03-18 20:19  屯子里唯一的架构师  阅读(97)  评论(0编辑  收藏  举报