docker容器的常用命令

容器命令

首先我们下载一个镜像
docker pull centos

新建容器并启动

docker run [可选参数] iamges
#参数说明
--name="Name0" 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互方式运行 进入容器查看内容
-p 制定容器的端口  -p 8000:8000
	-p ip:主机端口:容器端口
	-p 主机端口:主机端口
	-p 容器端口
	容器端口
-P  瞬间制定端口

#启动并进入主机
[root@VM-0-16-centos ~]# docker run -it centos /bin/bash
[root@b07bc65a2489 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#从容器中退回主机
[root@b07bc65a2489 /]# exit
exit

列出所有的运行的容器

#docker ps 当前运行的容器
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#docker ps -a 当前运行的容器+历史运行过的容器
[root@VM-0-16-centos ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                       PORTS     NAMES
b07bc65a2489   centos         "/bin/bash"   5 minutes ago   Exited (130) 4 minutes ago             intelligent_kepler
cceed5521588   feb5d9fea6a5   "/hello"      26 hours ago    Exited (0) 26 hours ago   
#docker ps -a -n=?  只显示一个
[root@VM-0-16-centos ~]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                       PORTS     NAMES
b07bc65a2489   centos    "/bin/bash"   7 minutes ago   Exited (130) 6 minutes ago             intelligent_kepler
#docker ps -q 显示所有的编号
[root@VM-0-16-centos ~]# docker ps -aq
b07bc65a2489
cceed5521588

退出容器

exit #直接容器停止并退出
Ctrl + p + q # 只退出不停止容器

删除容器

#删除制定
docker rm 容器id
#删除所有
docker rm -f $(docker ps -aq)
#通过管道删除所有
docker ps -aq|xargs docker rm

启动和停止容器的操作

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

后台启动进程

# docker run -d 镜像名!
[root@VM-0-16-centos ~]# docker run -d centos


docker 日志

# docker logs 
docker logs -f -t --tail 行数 镜像id
[root@VM-0-16-centos ~]# docker logs -f -t --tail 10 ad9540c692ec
2021-10-18T16:23:42.616938007Z qiji
2021-10-18T16:23:43.618596120Z qiji

查看进程

查看容器id
[root@VM-0-16-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
ad9540c692ec   centos    "/bin/sh -c 'while t…"   2 minutes ago   Up 2 minutes             musing_borg
214376efadd9   centos    "/bin/bash"              7 minutes ago   Up 7 minutes             crazy_past
#命令 docker top 容器id
[root@VM-0-16-centos ~]# docker top ad9540c692ec
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2314833             2314815             0                   00:23               ?                   00:00:00            /bin/sh -c while true;do echo qiji;sleep 1;done
root                2315727             2314833             0                   00:26               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看容器元数据

# 命令
#docker inspect 容器id
[root@VM-0-16-centos ~]# docker inspect ad9540c692ec
[
    {
        "Id": "ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf",
        "Created": "2021-10-18T16:23:08.252492417Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo qiji;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 2314833,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-10-18T16:23:08.552746839Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf/hostname",
        "HostsPath": "/var/lib/docker/containers/ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf/hosts",
        "LogPath": "/var/lib/docker/containers/ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf/ad9540c692ec9702e5256c4d2b7ca70f66f7e905e2956cbab0dac271b5836fbf-json.log",
        "Name": "/musing_borg",
        "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/3010aa5d4d8fc838d011b511215682dc9ec570b0cecbbd5dcffdff0cd832d26e-init/diff:/var/lib/docker/overlay2/0bcd8bc766c2b91c8ba39a41c82f4b6916d9b900724168fef90c40acd7e4766c/diff",
                "MergedDir": "/var/lib/docker/overlay2/3010aa5d4d8fc838d011b511215682dc9ec570b0cecbbd5dcffdff0cd832d26e/merged",
                "UpperDir": "/var/lib/docker/overlay2/3010aa5d4d8fc838d011b511215682dc9ec570b0cecbbd5dcffdff0cd832d26e/diff",
                "WorkDir": "/var/lib/docker/overlay2/3010aa5d4d8fc838d011b511215682dc9ec570b0cecbbd5dcffdff0cd832d26e/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "ad9540c692ec",
            "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 qiji;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": "18372b02faea2b6f50e866c901877c9d8114e64fde38a0119381da7736c0a532",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/18372b02faea",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "8b56195d146a9e800751793b689e1f9ec891e8efaeb24debae781a737df614f4",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "22622a5550989fc609e3e34d710ddb2fc5876324961b4cd696fe3329724765a1",
                    "EndpointID": "8b56195d146a9e800751793b689e1f9ec891e8efaeb24debae781a737df614f4",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

循环
请先登录后发表评论
  • latest comments
  • 总共0条评论