目录

Docker容器基本命令

https://cloud.ccczg.site/202205271722860.png

mac@MacdeMacBook-Pro ~ % docker pull centos

新建容器并启动

docker run [可选参数] image

#参数说明
--name ="name" 容器名字 用来区分容器
-it 使用交互的方式运行 进入容器查看内容
-d 后台方式运行
-p
  -p ip:主机端口:容器端口
  -p 主机端口:容器端口
  -p 容器端口
  容器端口
 -P 随机指定端口
 
 # 测试,启动并进入容器
mac@MacdeMacBook-Pro ~ % docker run -it centos /bin/bash
[root@084a638abcc7 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

#从容器中返回主机
[root@084a638abcc7 /]# exit
exit

列出所有的运行的容器

# docker ps 命令
  列出当前正在运行的容器
-a #列出当前正在运行的容器和历史运行过的容器
-n=#显示最近创建的容器
-q #只显示容器的编号
mac@MacdeMacBook-Pro ~ % docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                          PORTS     NAMES
084a638abcc7   centos        "/bin/bash"   4 minutes ago    Exited (0) About a minute ago             hardcore_panini
0b4e3e7c0b35   hello-world   "/hello"      43 minutes ago   Exited (0) 43 minutes ago                 boring_tereshkova
mac@MacdeMacBook-Pro ~ % docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

退出容器

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

删除容器

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

启动和停止容器

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

常用其他命令

后台启动容器

#命令 docker -d 镜像名
mac@MacdeMacBook-Pro ~ % docker run -d centos

查看日志

docker logs  -ft --tail
#显示日志 
-tf #显示日志
--tail number #要显示的日志条数

查看容器中的进程信息

docker top 容器Id
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
9a5f11734277   centos    "/bin/bash"   11 seconds ago   Up 10 seconds             agitated_bartik
mac@MacdeMacBook-Pro ~ % docker top 9a5f11734277
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3403                3375                0                   20:45               ?                   00:00:00            /bin/bash

查看镜像元数据

# 命令 docker inspect 容器Id
mac@MacdeMacBook-Pro ~ % docker inspect 9a5f11734277
[
    {
        "Id": "9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a",
        "Created": "2021-05-22T20:45:40.317199885Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3403,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-05-22T20:45:40.706704339Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/hostname",
        "HostsPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/hosts",
        "LogPath": "/var/lib/docker/containers/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a/9a5f11734277b852ff8863949b81bfed74c9f3e8d4a74d3be2b1d4dffeae0e1a-json.log",
        "Name": "/agitated_bartik",
        "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/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b-init/diff:/var/lib/docker/overlay2/f065e606298dfdf6bb02a024818a4e4c062fe0b1c343e040293edc3bb04de86f/diff",
                "MergedDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/merged",
                "UpperDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/diff",
                "WorkDir": "/var/lib/docker/overlay2/77cac635569126a99c0af94eeda336c61ebf5c62885647a6a29bd2cef679886b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "9a5f11734277",
            "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": "20201204",
                "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": "eedb30699a235b7f08f3a3680ea2da8eeeb07333e6028fcf70b5c637215c4c62",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/eedb30699a23",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "7cb260cd88c48365ac016cea45c0fc7ee0111f94606e41628d18f2a3da1a6851",
            "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": "d24fab5689b36900802fec22ff27f250bf9cd480a498953715d4e6200e707081",
                    "EndpointID": "7cb260cd88c48365ac016cea45c0fc7ee0111f94606e41628d18f2a3da1a6851",
                    "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 9a5f11734277 /bin/bash  #进入容器侯开启一个新的终端
docker attach  9a5f11734277  #进入容器正在使用的终端,不会启动新的进程

从容器内拷贝文件到主机上

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

#进入docker容器内部
mac@MacdeMacBook-Pro ~ % docker exec -it 9a5f11734277 /bin/bash
[root@9a5f11734277 /]# ls  
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9a5f11734277 /]# cd /home/
[root@9a5f11734277 home]# ls
#创建文件
[root@9a5f11734277 home]# touch test.java
[root@9a5f11734277 home]# ls
test.java
[root@9a5f11734277 home]# exit
exit
#拷贝容器到主机上
mac@MacdeMacBook-Pro ~ % docker cp 9a5f11734277:/home/test.java  /Users/mac  
mac@MacdeMacBook-Pro ~ % ls /Users/mac 
Applications		Downloads		Postman			getting-started
BuildTools		Library			Projects		logs
Desktop			Movies			Public			temp
DevEcoStudioProjects	Music			Share			test.java
Documents		Pictures		dump.rdb