一、环境准备

(1)使用docker pull centos 下载一个centos的最新版本的镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. [root@localhost ~]# docker pull centos
  4. Using default tag: latest
  5. latest: Pulling from library/centos
  6. a1d0c7532777: Pull complete
  7. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  8. Status: Downloaded newer image for centos:latest
  9. docker.io/library/centos:latest
  10. [root@localhost ~]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. centos latest 5d0da3dc9764 3 weeks ago 231MB
  13. [root@localhost ~]#

二、docker操作常用的命令

2.1 docker run 命令

(1)docker run —help 帮助信息

  1. [root@localhost ~]# docker run --help
  2. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  3. Run a command in a new container
  4. Options:
  5. --add-host list Add a custom host-to-IP mapping (host:ip)
  6. -a, --attach list Attach to STDIN, STDOUT or STDERR
  7. --blkio-weight uint16 Block IO (relative weight), between 10 and
  8. 1000, or 0 to disable (default 0)
  9. --blkio-weight-device list Block IO weight (relative device weight)
  10. (default [])
  11. --cap-add list Add Linux capabilities
  12. --cap-drop list Drop Linux capabilities
  13. --cgroup-parent string Optional parent cgroup for the container
  14. --cgroupns string Cgroup namespace to use (host|private)
  15. 'host': Run the container in the Docker
  16. host's cgroup namespace
  17. 'private': Run the container in its own private
  18. cgroup namespace
  19. '': Use the cgroup namespace as
  20. configured by the
  21. default-cgroupns-mode option on the
  22. daemon (default)
  23. --cidfile string Write the container ID to the file
  24. --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
  25. --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
  26. --cpu-rt-period int Limit CPU real-time period in microseconds
  27. --cpu-rt-runtime int Limit CPU real-time runtime in microseconds
  28. -c, --cpu-shares int CPU shares (relative weight)
  29. --cpus decimal Number of CPUs
  30. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  31. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  32. -d, --detach Run container in background and print container ID
  33. --detach-keys string Override the key sequence for detaching a container
  34. --device list Add a host device to the container
  35. --device-cgroup-rule list Add a rule to the cgroup allowed devices list
  36. --device-read-bps list Limit read rate (bytes per second) from a
  37. device (default [])
  38. --device-read-iops list Limit read rate (IO per second) from a device
  39. (default [])
  40. --device-write-bps list Limit write rate (bytes per second) to a device
  41. (default [])
  42. --device-write-iops list Limit write rate (IO per second) to a device
  43. (default [])
  44. --disable-content-trust Skip image verification (default true)
  45. --dns list Set custom DNS servers
  46. --dns-option list Set DNS options
  47. --dns-search list Set custom DNS search domains
  48. --domainname string Container NIS domain name
  49. --entrypoint string Overwrite the default ENTRYPOINT of the image
  50. -e, --env list Set environment variables
  51. --env-file list Read in a file of environment variables
  52. --expose list Expose a port or a range of ports
  53. --gpus gpu-request GPU devices to add to the container ('all' to
  54. pass all GPUs)
  55. --group-add list Add additional groups to join
  56. --health-cmd string Command to run to check health
  57. --health-interval duration Time between running the check (ms|s|m|h)
  58. (default 0s)
  59. --health-retries int Consecutive failures needed to report unhealthy
  60. --health-start-period duration Start period for the container to initialize
  61. before starting health-retries countdown
  62. (ms|s|m|h) (default 0s)
  63. --health-timeout duration Maximum time to allow one check to run
  64. (ms|s|m|h) (default 0s)
  65. --help Print usage
  66. -h, --hostname string Container host name
  67. --init Run an init inside the container that forwards
  68. signals and reaps processes
  69. -i, --interactive Keep STDIN open even if not attached
  70. --ip string IPv4 address (e.g., 172.30.100.104)
  71. --ip6 string IPv6 address (e.g., 2001:db8::33)
  72. --ipc string IPC mode to use
  73. --isolation string Container isolation technology
  74. --kernel-memory bytes Kernel memory limit
  75. -l, --label list Set meta data on a container
  76. --label-file list Read in a line delimited file of labels
  77. --link list Add link to another container
  78. --link-local-ip list Container IPv4/IPv6 link-local addresses
  79. --log-driver string Logging driver for the container
  80. --log-opt list Log driver options
  81. --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
  82. -m, --memory bytes Memory limit
  83. --memory-reservation bytes Memory soft limit
  84. --memory-swap bytes Swap limit equal to memory plus swap: '-1' to
  85. enable unlimited swap
  86. --memory-swappiness int Tune container memory swappiness (0 to 100)
  87. (default -1)
  88. --mount mount Attach a filesystem mount to the container
  89. --name string Assign a name to the container
  90. --network network Connect a container to a network
  91. --network-alias list Add network-scoped alias for the container
  92. --no-healthcheck Disable any container-specified HEALTHCHECK
  93. --oom-kill-disable Disable OOM Killer
  94. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  95. --pid string PID namespace to use
  96. --pids-limit int Tune container pids limit (set -1 for unlimited)
  97. --platform string Set platform if server is multi-platform capable
  98. --privileged Give extended privileges to this container
  99. -p, --publish list Publish a container's port(s) to the host
  100. -P, --publish-all Publish all exposed ports to random ports
  101. --pull string Pull image before running
  102. ("always"|"missing"|"never") (default "missing")
  103. --read-only Mount the container's root filesystem as read only
  104. --restart string Restart policy to apply when a container exits
  105. (default "no")
  106. --rm Automatically remove the container when it exits
  107. --runtime string Runtime to use for this container
  108. --security-opt list Security Options
  109. --shm-size bytes Size of /dev/shm
  110. --sig-proxy Proxy received signals to the process (default true)
  111. --stop-signal string Signal to stop a container (default "SIGTERM")
  112. --stop-timeout int Timeout (in seconds) to stop a container
  113. --storage-opt list Storage driver options for the container
  114. --sysctl map Sysctl options (default map[])
  115. --tmpfs list Mount a tmpfs directory
  116. -t, --tty Allocate a pseudo-TTY
  117. --ulimit ulimit Ulimit options (default [])
  118. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  119. --userns string User namespace to use
  120. --uts string UTS namespace to use
  121. -v, --volume list Bind mount a volume
  122. --volume-driver string Optional volume driver for the container
  123. --volumes-from list Mount volumes from the specified container(s)
  124. -w, --workdir string Working directory inside the container
  125. [root@localhost ~]#

(2)docker run [可选参数] 镜像名称
—name 设置docker容器的名称
-d 在后台运行docker
-it 使用交互的方式运行docker
-p 执行容器的端口,如 -p 80:80
-P 随机指定端口

如下为以交互的方式启动并进入docker容器

  1. [root@localhost ~]# docker run -it centos /bin/bash
  2. [root@3e394ae7eda7 /]#

(3)docker run -d 镜像名称 后台启动docker
如下为后台启动centos容器,这里需要注意的是,使用-d后台启动docker镜像后,如果docker镜像中没有前台进程,则docker容器停止,即当docker发现没有前台进程时就会自动停止

  1. [root@localhost ~]# docker run -d centos
  2. 3cab830ec97c4f964b62a42726e3e541f689f906e00ee2a24289f2ce44a2b94e
  3. [root@localhost ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. [root@localhost ~]# docker ps -a
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 3cab830ec97c centos "/bin/bash" About a minute ago Exited (0) About a minute ago inspiring_benz
  8. [root@localhost ~]#

(4)docker run -d 镜像名称 /bin/bash -C “xxx” 后台启动一个docker容器,并且执行xxx的shell脚本
如下为后台启动一个docker,并且docker容器中循环打印hello_docker,此时可以发现docker处于运行状态

  1. [root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello_docker;sleep 1;done"
  2. 9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33
  3. [root@localhost ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 9f427c9ff601 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds nice_almeida
  6. [root@localhost ~]#

2.2 退出容器

(1)使用exit 命令,注意如果使用exit命令进入退出后,docker就停止了

  1. [root@3e394ae7eda7 /]# exit
  2. exit
  3. [root@localhost ~]#

(2)使用快捷键 Ctrl+p Ctrl + q 快捷键可以做到退出docker容器,同时docker容器不会停止

2.3 docker ps 命令

(1)docker ps 帮助信息

  1. [root@localhost ~]# docker ps --help
  2. Usage: docker ps [OPTIONS]
  3. List containers
  4. Options:
  5. -a, --all Show all containers (default shows just running)
  6. -f, --filter filter Filter output based on conditions provided
  7. --format string Pretty-print containers using a Go template
  8. -n, --last int Show n last created containers (includes all states) (default -1)
  9. -l, --latest Show the latest created container (includes all states)
  10. --no-trunc Don't truncate output
  11. -q, --quiet Only display container IDs
  12. -s, --size Display total file sizes
  13. [root@localhost ~]#

(2)docker ps 列出正在运行的容器
如下,表示当前没有docker在运行

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. [root@localhost ~]#

(3)docker ps -a 列出正在运行和历史运行过的docker容器

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3e394ae7eda7 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago unruffled_lederberg
  4. c2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse
  5. 0150f4c51f21 feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago optimistic_albattani
  6. [root@localhost ~]#

(4)docker ps -n 个数 列出最近创建的容器
如下表示最近创建的1个容器

  1. [root@localhost ~]# docker ps -n 1
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3e394ae7eda7 centos "/bin/bash" 7 minutes ago Exited (0) 5 minutes ago unruffled_lederberg
  4. [root@localhost ~]#

(5)docker ps -aq 显示所有运行过的容器的id

  1. [root@localhost ~]# docker ps -aq
  2. 3e394ae7eda7
  3. c2f1f112cbbe
  4. 0150f4c51f21
  5. [root@localhost ~]#

2.4 docker rm -f 容器id 命令

(1)docker rm -f 容器id 删除容器

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3e394ae7eda7 centos "/bin/bash" 14 minutes ago Up 3 minutes unruffled_lederberg
  4. c2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse
  5. 0150f4c51f21 feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago optimistic_albattani
  6. [root@localhost ~]# docker rm -f 0150f4c51f21
  7. 0150f4c51f21
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 3e394ae7eda7 centos "/bin/bash" 14 minutes ago Up 3 minutes unruffled_lederberg
  11. c2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse
  12. [root@localhost ~]#

(2)docker rm -f $(docker ps -aq) 删除所有docker容器

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3e394ae7eda7 centos "/bin/bash" 17 minutes ago Up 6 minutes unruffled_lederberg
  4. c2f1f112cbbe feb5d9fea6a5 "/hello" 10 hours ago Exited (0) 10 hours ago loving_morse
  5. [root@localhost ~]# docker rm -f $(docker ps -aq)
  6. 3e394ae7eda7
  7. c2f1f112cbbe
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. [root@localhost ~]#

(3)docker ps -aq|xargs docker rm -f 也可以删除所有的容器

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 2fe6ca22d978 centos "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago centos02
  4. 40dbe1daff99 centos "/bin/bash" 12 seconds ago Exited (0) 12 seconds ago centos01
  5. [root@localhost ~]# docker ps -aq|xargs docker rm -f
  6. 2fe6ca22d978
  7. 40dbe1daff99
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. [root@localhost ~]#

2.5 启动和停止容器

(1)docker start 容器id 启动容器

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. [root@localhost ~]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. aa2976a9dbb1 centos "/bin/bash" 19 seconds ago Exited (0) 15 seconds ago blissful_kirch
  6. [root@localhost ~]# docker start aa2976a9dbb1
  7. aa2976a9dbb1
  8. [root@localhost ~]# docker ps
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. aa2976a9dbb1 centos "/bin/bash" 33 seconds ago Up 2 seconds blissful_kirch
  11. [root@localhost ~]#

(2)docker restart 容器id 重启重启

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. aa2976a9dbb1 centos "/bin/bash" 7 minutes ago Up 6 minutes blissful_kirch
  4. [root@localhost ~]# docker restart aa2976a9dbb1
  5. aa2976a9dbb1
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. aa2976a9dbb1 centos "/bin/bash" 7 minutes ago Up 1 second blissful_kirch
  9. [root@localhost ~]#

(3)docker stop 容器id 停止容器

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. aa2976a9dbb1 centos "/bin/bash" 8 minutes ago Up 30 seconds blissful_kirch
  4. [root@localhost ~]# docker stop aa2976a9dbb1
  5. aa2976a9dbb1
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. aa2976a9dbb1 centos "/bin/bash" 8 minutes ago Exited (0) 4 seconds ago blissful_kirch
  11. [root@localhost ~]#

(4)docker kill 容器id 停止容器(当使用docker stop 无法停止时,可以使用docker kill 直接杀掉)

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. aa2976a9dbb1 centos "/bin/bash" 9 minutes ago Up 9 seconds blissful_kirch
  4. [root@localhost ~]# docker kill aa2976a9dbb1
  5. aa2976a9dbb1
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. [root@localhost ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. aa2976a9dbb1 centos "/bin/bash" 9 minutes ago Exited (137) 5 seconds ago blissful_kirch
  11. [root@localhost ~]#

2.6 docker logs 查看日志命令

(1)docker logs —help 查看docker logs 的帮助信息

  1. [root@localhost ~]# docker logs --help
  2. Usage: docker logs [OPTIONS] CONTAINER
  3. Fetch the logs of a container
  4. Options:
  5. --details Show extra details provided to logs
  6. -f, --follow Follow log output
  7. --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or
  8. relative (e.g. 42m for 42 minutes)
  9. -n, --tail string Number of lines to show from the end of the logs (default "all")
  10. -t, --timestamps Show timestamps
  11. --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or
  12. relative (e.g. 42m for 42 minutes)
  13. [root@localhost ~]#

(2)docker logs -tf —tail num 容器id 可以查看启动容器的最新num行的日志
如下查看10行日志,并且会一直更新。类似linux中的tail -f 的效果

  1. [root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello_docker;sleep 1;done"
  2. 9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33
  3. [root@localhost ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 9f427c9ff601 centos "/bin/sh -c 'while t…" 3 seconds ago Up 2 seconds nice_almeida
  6. [root@localhost ~]# docker logs -tf --tail 10 9f427c9ff601
  7. 2021-10-10T15:27:02.215242337Z hello_docker
  8. 2021-10-10T15:27:03.218393417Z hello_docker
  9. 2021-10-10T15:27:04.220812877Z hello_docker
  10. 2021-10-10T15:27:05.223520640Z hello_docker
  11. 2021-10-10T15:27:06.226058326Z hello_docker
  12. 2021-10-10T15:27:07.229459499Z hello_docker
  13. 2021-10-10T15:27:08.232441670Z hello_docker
  14. 2021-10-10T15:27:09.235056718Z hello_docker
  15. 2021-10-10T15:27:10.238007922Z hello_docker
  16. 2021-10-10T15:27:11.240768549Z hello_docker

2.7 docker top 查看容器的进程信息

(1)docker top 容器id 查看容器中的进程信息

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9f427c9ff601 centos "/bin/sh -c 'while t…" 8 minutes ago Up 8 minutes nice_almeida
  4. [root@localhost ~]# docker top 9f427c9ff601
  5. UID PID PPID C STIME TTY TIME CMD
  6. root 68199 68179 0 23:24 ? 00:00:00 /bin/sh -c while true;do echo hello_docker;sleep 1;done
  7. root 70194 68199 0 23:32 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  8. [root@localhost ~]#

2.8 docker inspect 查看容器内部信息

(1)docker inspect 容器id 查看容器的内部信息

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9f427c9ff601 centos "/bin/sh -c 'while t…" 11 minutes ago Up 11 minutes nice_almeida
  4. [root@localhost ~]# docker inspect 9f427c9ff601
  5. [
  6. {
  7. "Id": "9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33",
  8. "Created": "2021-10-10T15:24:06.297442771Z",
  9. "Path": "/bin/sh",
  10. "Args": [
  11. "-c",
  12. "while true;do echo hello_docker;sleep 1;done"
  13. ],
  14. "State": {
  15. "Status": "running",
  16. "Running": true,
  17. "Paused": false,
  18. "Restarting": false,
  19. "OOMKilled": false,
  20. "Dead": false,
  21. "Pid": 68199,
  22. "ExitCode": 0,
  23. "Error": "",
  24. "StartedAt": "2021-10-10T15:24:06.590095928Z",
  25. "FinishedAt": "0001-01-01T00:00:00Z"
  26. },
  27. "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
  28. "ResolvConfPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/resolv.conf",
  29. "HostnamePath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/hostname",
  30. "HostsPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/hosts",
  31. "LogPath": "/var/lib/docker/containers/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33/9f427c9ff601d31891fc947932a4e11f8e8b696dfd4a93dceea5bbfa6f465a33-json.log",
  32. "Name": "/nice_almeida",
  33. "RestartCount": 0,
  34. "Driver": "overlay2",
  35. "Platform": "linux",
  36. "MountLabel": "",
  37. "ProcessLabel": "",
  38. "AppArmorProfile": "",
  39. "ExecIDs": null,
  40. "HostConfig": {
  41. "Binds": null,
  42. "ContainerIDFile": "",
  43. "LogConfig": {
  44. "Type": "json-file",
  45. "Config": {}
  46. },
  47. "NetworkMode": "default",
  48. "PortBindings": {},
  49. "RestartPolicy": {
  50. "Name": "no",
  51. "MaximumRetryCount": 0
  52. },
  53. "AutoRemove": false,
  54. "VolumeDriver": "",
  55. "VolumesFrom": null,
  56. "CapAdd": null,
  57. "CapDrop": null,
  58. "CgroupnsMode": "host",
  59. "Dns": [],
  60. "DnsOptions": [],
  61. "DnsSearch": [],
  62. "ExtraHosts": null,
  63. "GroupAdd": null,
  64. "IpcMode": "private",
  65. "Cgroup": "",
  66. "Links": null,
  67. "OomScoreAdj": 0,
  68. "PidMode": "",
  69. "Privileged": false,
  70. "PublishAllPorts": false,
  71. "ReadonlyRootfs": false,
  72. "SecurityOpt": null,
  73. "UTSMode": "",
  74. "UsernsMode": "",
  75. "ShmSize": 67108864,
  76. "Runtime": "runc",
  77. "ConsoleSize": [
  78. 0,
  79. 0
  80. ],
  81. "Isolation": "",
  82. "CpuShares": 0,
  83. "Memory": 0,
  84. "NanoCpus": 0,
  85. "CgroupParent": "",
  86. "BlkioWeight": 0,
  87. "BlkioWeightDevice": [],
  88. "BlkioDeviceReadBps": null,
  89. "BlkioDeviceWriteBps": null,
  90. "BlkioDeviceReadIOps": null,
  91. "BlkioDeviceWriteIOps": null,
  92. "CpuPeriod": 0,
  93. "CpuQuota": 0,
  94. "CpuRealtimePeriod": 0,
  95. "CpuRealtimeRuntime": 0,
  96. "CpusetCpus": "",
  97. "CpusetMems": "",
  98. "Devices": [],
  99. "DeviceCgroupRules": null,
  100. "DeviceRequests": null,
  101. "KernelMemory": 0,
  102. "KernelMemoryTCP": 0,
  103. "MemoryReservation": 0,
  104. "MemorySwap": 0,
  105. "MemorySwappiness": null,
  106. "OomKillDisable": false,
  107. "PidsLimit": null,
  108. "Ulimits": null,
  109. "CpuCount": 0,
  110. "CpuPercent": 0,
  111. "IOMaximumIOps": 0,
  112. "IOMaximumBandwidth": 0,
  113. "MaskedPaths": [
  114. "/proc/asound",
  115. "/proc/acpi",
  116. "/proc/kcore",
  117. "/proc/keys",
  118. "/proc/latency_stats",
  119. "/proc/timer_list",
  120. "/proc/timer_stats",
  121. "/proc/sched_debug",
  122. "/proc/scsi",
  123. "/sys/firmware"
  124. ],
  125. "ReadonlyPaths": [
  126. "/proc/bus",
  127. "/proc/fs",
  128. "/proc/irq",
  129. "/proc/sys",
  130. "/proc/sysrq-trigger"
  131. ]
  132. },
  133. "GraphDriver": {
  134. "Data": {
  135. "LowerDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c-init/diff:/var/lib/docker/overlay2/70d40b13bd8c041fcbd2dcffcde4522bc11106bd5e21dc5c647aa94764caa269/diff",
  136. "MergedDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/merged",
  137. "UpperDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/diff",
  138. "WorkDir": "/var/lib/docker/overlay2/12f4200467ee3b6ec7fff8878dae6acd648b8967f4b1d32601d4b8adb8549c4c/work"
  139. },
  140. "Name": "overlay2"
  141. },
  142. "Mounts": [],
  143. "Config": {
  144. "Hostname": "9f427c9ff601",
  145. "Domainname": "",
  146. "User": "",
  147. "AttachStdin": false,
  148. "AttachStdout": false,
  149. "AttachStderr": false,
  150. "Tty": false,
  151. "OpenStdin": false,
  152. "StdinOnce": false,
  153. "Env": [
  154. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  155. ],
  156. "Cmd": [
  157. "/bin/sh",
  158. "-c",
  159. "while true;do echo hello_docker;sleep 1;done"
  160. ],
  161. "Image": "centos",
  162. "Volumes": null,
  163. "WorkingDir": "",
  164. "Entrypoint": null,
  165. "OnBuild": null,
  166. "Labels": {
  167. "org.label-schema.build-date": "20210915",
  168. "org.label-schema.license": "GPLv2",
  169. "org.label-schema.name": "CentOS Base Image",
  170. "org.label-schema.schema-version": "1.0",
  171. "org.label-schema.vendor": "CentOS"
  172. }
  173. },
  174. "NetworkSettings": {
  175. "Bridge": "",
  176. "SandboxID": "a86ecda3eaf8cf0aa94ff0c783890015394d6103e3054ac57746c9f08c9eb5d3",
  177. "HairpinMode": false,
  178. "LinkLocalIPv6Address": "",
  179. "LinkLocalIPv6PrefixLen": 0,
  180. "Ports": {},
  181. "SandboxKey": "/var/run/docker/netns/a86ecda3eaf8",
  182. "SecondaryIPAddresses": null,
  183. "SecondaryIPv6Addresses": null,
  184. "EndpointID": "7ac083dc3fc1b560462577a3c50c8435a2382afc3f763876b26c9b733a9d021e",
  185. "Gateway": "172.17.0.1",
  186. "GlobalIPv6Address": "",
  187. "GlobalIPv6PrefixLen": 0,
  188. "IPAddress": "172.17.0.2",
  189. "IPPrefixLen": 16,
  190. "IPv6Gateway": "",
  191. "MacAddress": "02:42:ac:11:00:02",
  192. "Networks": {
  193. "bridge": {
  194. "IPAMConfig": null,
  195. "Links": null,
  196. "Aliases": null,
  197. "NetworkID": "863db47971b47481718e154eb2b43ab2d76f8e09b32924a48074808084fe9f10",
  198. "EndpointID": "7ac083dc3fc1b560462577a3c50c8435a2382afc3f763876b26c9b733a9d021e",
  199. "Gateway": "172.17.0.1",
  200. "IPAddress": "172.17.0.2",
  201. "IPPrefixLen": 16,
  202. "IPv6Gateway": "",
  203. "GlobalIPv6Address": "",
  204. "GlobalIPv6PrefixLen": 0,
  205. "MacAddress": "02:42:ac:11:00:02",
  206. "DriverOpts": null
  207. }
  208. }
  209. }
  210. }
  211. ]
  212. [root@localhost ~]#

2.9 进入docker容器的命令

(1)docker exec -it 容器id bash 重新打开一个终端的方式进入docker
如下为进入docker后重新打开一个终端,这种方式可以通过exit方式退出docker

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9f427c9ff601 centos "/bin/sh -c 'while t…" 17 minutes ago Up 17 minutes nice_almeida
  4. [root@localhost ~]# docker exec -it 9f427c9ff601 bash
  5. [root@9f427c9ff601 /]#

(2)docker attach
进入容器中正在运行的终端

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 9f427c9ff601 centos "/bin/sh -c 'while t…" 19 minutes ago Up 19 minutes nice_almeida
  4. [root@localhost ~]# docker attach 9f427c9ff601
  5. hello_docker
  6. hello_docker
  7. hello_docker
  8. hello_docker
  9. hello_docker
  10. hello_docker
  11. hello_docker
  12. hello_docker
  13. hello_docker
  14. hello_docke

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

(1)docker cp 容器id:容器文件路径 本地文件路径

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d1df9f4c810d centos "/bin/sh -c 'while t…" 16 minutes ago Up 16 minutes confident_franklin
  4. [root@localhost ~]# docker exec -it d1df9f4c810d bash
  5. [root@d1df9f4c810d /]# ls
  6. bin etc lib lost+found mnt proc run srv tmp var
  7. dev home lib64 media opt root sbin sys usr
  8. [root@d1df9f4c810d /]# cd /opt
  9. [root@d1df9f4c810d opt]# ls
  10. [root@d1df9f4c810d opt]# echo 'hello docker' >> test.txt
  11. [root@d1df9f4c810d opt]# ls
  12. test.txt
  13. [root@d1df9f4c810d opt]# exit
  14. exit
  15. [root@localhost ~]# docker cp d1df9f4c810d:/opt/test.txt /opt/test.txt
  16. [root@localhost ~]# cat /opt/test.txt
  17. hello docker
  18. [root@localhost ~]#