安装Docker

连接阿里云服务器

  1. 重置实例密码
  2. 配置安全组
  3. 使用xshell连接

    环境查看

    1. root@iZbp16gi14kuk4bpt0kf69Z:/# cat /etc/os-release
    2. NAME="Ubuntu"
    3. VERSION="16.04.7 LTS (Xenial Xerus)"
    4. ID=ubuntu
    5. ID_LIKE=debian
    6. PRETTY_NAME="Ubuntu 16.04.7 LTS"
    7. VERSION_ID="16.04"
    8. HOME_URL="http://www.ubuntu.com/"
    9. SUPPORT_URL="http://help.ubuntu.com/"
    10. BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    11. VERSION_CODENAME=xenial
    12. UBUNTU_CODENAME=xenial

    安装

    卸载旧版本

    1. sudo apt-get remove docker docker-engine docker.io containerd runc

    更新需要的安装包

    ```bash sudo apt-get update

    sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg

  1. <a name="9NRTy"></a>
  2. #### 添加docker's official gpg key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg —dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

https://mzsb7ers.mirror.aliyuncs.com

sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-‘EOF’ { “registry-mirrors”: [“https://mzsb7ers.mirror.aliyuncs.com“] } EOF sudo systemctl daemon-reload sudo systemctl restart docker

echo \ “deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1734557/1614911057335-e8f83f57-7ea0-4379-ae3f-75dd7342a209.png#align=left&display=inline&height=459&margin=%5Bobject%20Object%5D&name=image.png&originHeight=459&originWidth=743&size=36899&status=done&style=none&width=743)
  2. /var/lib/docker docker的默认工作环境
  3. <a name="tsXeT"></a>
  4. ## 底层原理
  5. <a name="iiYTV"></a>
  6. ### docker是怎么工作的
  7. Docker是一个Client-Server结构的系统,Docker的守护运行在主机上,通过Socker从客户端访问<br />DockerServer接收到Docker-Client的指令,就会执行这个命令
  8. <a name="S2lll"></a>
  9. ### Docker为什么比VM快
  10. 1. Docker有着比虚拟机更少的抽象层
  11. ![](https://cdn.nlark.com/yuque/0/2021/png/1734557/1614912408398-2a3d2ab5-15a8-40e2-b541-d457e3e1cc92.png#align=left&display=inline&height=263&margin=%5Bobject%20Object%5D&originHeight=263&originWidth=529&size=0&status=done&style=none&width=529)
  12. 2. Docker利用的是宿主机的内核,VM需要的是guest OS
  13. 所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级
  14. <a name="7OAMT"></a>
  15. ## Docker的常用命令
  16. <a name="rPEf6"></a>
  17. ### 帮助命令

docker version # 显示docker的版本信息 docker info # 显示docker的系统命令,包括系统和镜像的数量 docker 命令 —help 万能命令

  1. 帮助文档的地址: [https://docs.docker.com/engine/reference/commandline/docker/](https://docs.docker.com/engine/reference/commandline/docker/)
  2. <a name="Tr7H6"></a>
  3. ### 镜像命令
  4. <a name="ltrgA"></a>
  5. #### docker images 查看所有本地的主机上的镜像
  6. ```bash
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. hello-world latest bf756fb1ae65 14 months ago 13.3kB
  9. # 解释
  10. Repository 镜像的仓库源
  11. tag 镜像的标签
  12. image id 镜像的id
  13. created 镜像的创建时间
  14. size 镜像的大小
  15. # 可选项
  16. -a, --all # 列出所有镜像
  17. -q, --quiet # 只显示镜像ID

docker search 搜索镜像

  1. docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 10575 [OK]
  4. mariadb MariaDB Server is a high performing open sou 3956 [OK]
  5. mysql/mysql-server Optimized MySQL Server Docker images. Create 775 [OK]
  6. percona Percona Server is a fork of the MySQL relati 527 [OK]
  7. centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
  8. mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr 79
  9. centurylink/mysql Image containing mysql. Optimized to be link 59 [OK]
  10. bitnami/mysql Bitnami MySQL Docker Image 48 [OK]
  11. deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r 41 [OK]
  12. databack/mysql-backup Back up mysql databases to... anywhere! 39
  13. prom/mysqld-exporter 37 [OK]
  14. tutum/mysql Base docker image to run a MySQL database se 35
  15. schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup 29 [OK]
  16. linuxserver/mysql A Mysql container, brought to you by LinuxSe 27
  17. # 可选项,通过搜藏来过滤
  18. --filter=STARS=3000 搜索出来的镜像就是大于3000
  19. root@iZbp16gi14kuk4bpt0kf69Z:/# docker search mysql --filter=stars=3000
  20. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  21. mysql MySQL is a widely used, open-source relation 10575 [OK]
  22. mariadb MariaDB Server is a high performing open sou 3956 [OK]
  23. root@iZbp16gi14kuk4bpt0kf69Z:/#

docker pull docker下载

  1. # 下载镜像docker pull 镜像名[:tag]
  2. root@iZbp16gi14kuk4bpt0kf69Z:/# docker pull mysql
  3. Using default tag: latest # 如果不加tag默认就是latest,默认下载最新
  4. latest: Pulling from library/mysql
  5. a076a628af6f: Pull complete # 分层下载,docker image的核心 联合文件系统
  6. f6c208f3f991: Pull complete
  7. 88a9455a9165: Pull complete
  8. 406c9b8427c6: Pull complete
  9. 7c88599c0b25: Pull complete
  10. 25b5c6debdaf: Pull complete
  11. 43a5816f1617: Pull complete
  12. 1a8c919e89bf: Pull complete
  13. 9f3cf4bd1a07: Pull complete
  14. 80539cea118d: Pull complete
  15. 201b3cad54ce: Pull complete
  16. 944ba37e1c06: Pull complete
  17. Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c # 签名
  18. Status: Downloaded newer image for mysql:latest
  19. docker.io/library/mysql:latest # 真实地址
  20. # 指定版本下载
  21. root@iZbp16gi14kuk4bpt0kf69Z:/# docker pull mysql:5.7
  22. 5.7: Pulling from library/mysql
  23. a076a628af6f: Already exists
  24. f6c208f3f991: Already exists
  25. 88a9455a9165: Already exists
  26. 406c9b8427c6: Already exists
  27. 7c88599c0b25: Already exists
  28. 25b5c6debdaf: Already exists
  29. 43a5816f1617: Already exists
  30. 1831ac1245f4: Pull complete
  31. 37677b8c1f79: Pull complete
  32. 27e4ac3b0f6e: Pull complete
  33. 7227baa8c445: Pull complete
  34. Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
  35. Status: Downloaded newer image for mysql:5.7
  36. docker.io/library/mysql:5.7

docker rmi 删除镜像

  1. root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f 容器id
  2. root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f 容器id 容器id 容器id 容器id # 删除多个容器
  3. root@iZbp16gi14kuk4bpt0kf69Z:/# docker rmi -f $(docker images -aq) # 删除全部的容器

容器命令

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

  1. docker pull centos

新建容器并启动 docker run

  1. docker run [可选参数] image
  2. # 参数说明
  3. --name="Name" 容器名字 tomcat01,tomcat用来区分容器
  4. -d 后台运行方式
  5. -it 使用交互方式运行,进入容器查看内容
  6. -p 指定容器的端口 -p 8080:8080
  7. -p ip:主机端口:容器端口
  8. -p 主机端口:容器端口
  9. -p 容器端口
  10. 容器端口
  11. -p 随机指定端口
  12. # 测试,启动并进入容器
  13. # docker run it centos /bin/bash
  14. # ls # 查看容器内的centos,基础版本,很多命令都是不完善的
  15. # 从容器中退回主机
  16. # exit

查看正在运行的容器 docker ps -a

  1. docker ps
  2. # 参数
  3. #列出当前正在运行的容器
  4. -a #列出当前正在运行的容器+带出历史运行过的容器
  5. -n=? # 显示最近创建的容器
  6. -q # 显示编号
  7. docker ps -aq

退出容器

  1. exit # 直接停止容器并退出
  2. ctrl + p + q # 容器不停止退出

删除容器

  1. docker rm 容器id # 删除指定的容器,不能删除正在运行的容器,如果要强制删除,rm -rf
  2. docker rm -rf $(docker ps -aq) # 删除所有的容器

启动和停止容器

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

常用其他命令

后台启动容器

  1. # 命令docker run -d 镜像名
  2. docker run -d centos
  3. # 问题:docker ps ,发现centos停止了
  4. # 常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有前台进程,就会自动停止
  5. # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

  1. docker logs -t -f --tail 容器
  2. # 显示日志
  3. -tf # 显示日志,时间戳
  4. --tail number # 要显示的日志条数
  5. docker logs -tf --tail 10 容器id

查看容器中的进程信息

  1. # docker top 容器id
  2. docker top 0a37b92836f4
  3. UID PID PPID C STIME TTY TIME CMD
  4. root 28883 28860 0 21:10 pts/0 00:00:00 /bin/bash

查看镜像的源数据

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

进入当前正在运行的容器

  1. # 通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
  2. # 命令
  3. docker exec -it 容器id bash shell
  4. # 方式二
  5. docker attach 容器id
  6. # docker exec # 进入容器后开启一个新的终端,可以再里面操作(常用)
  7. # docker attach # 进入容器正在执行的终端,不会启动新的进程

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

  1. docker cp 容器id:容器内路径 主机路径

作业

Docker 安装nginx

  1. 搜索镜像
  2. 下载镜像
    ```shell root@iZbp16gi14kuk4bpt0kf69Z:~# docker run -d —name nginx01 -p 3344:80 nginx addf33b67b93bb4ac5a4de9f144319d8fd05f26e4dc0167e43f084ab0f0d057e root@iZbp16gi14kuk4bpt0kf69Z:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES addf33b67b93 nginx “/docker-entrypoint.…” 9 seconds ago Up 7 seconds 0.0.0.0:3344->80/tcp nginx01 5d26034a3e93 nginx “/docker-entrypoint.…” About a minute ago Up About a minute 80/tcp practical_kare root@iZbp16gi14kuk4bpt0kf69Z:~# docker kill 5d26034a3e93 5d26034a3e93 root@iZbp16gi14kuk4bpt0kf69Z:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES addf33b67b93 nginx “/docker-entrypoint.…” 31 seconds ago Up 30 seconds 0.0.0.0:3344->80/tcp nginx01 root@iZbp16gi14kuk4bpt0kf69Z:~# curl localhost:3344 <!DOCTYPE html>

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

```