image.png

dockerfile:构建文件,源代码 dockerimages:通过dockerfile构建生成 docker容器:用来提供镜像运行的服务器

版本信息及帮助信息

  1. docker version
  2. docker info
  3. docker --help

镜像命令

docker images 查看所有本地主机的镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. 镜像的仓库源 标签 镜像ID 创建时间 大小
  4. # 扩展参数
  5. -a, --all #列出所有镜像
  6. -q, --quiet #只显示镜像的id

docker search 搜索镜像

  1. [root@localhost ~]# docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 10999 [OK]
  4. mariadb MariaDB Server is a high performing open sou 4165 [OK]
  5. # 扩展参数
  6. --filter=STARS=3000 #搜索出来的镜像都是STRATS大于3000的
  7. [root@localhost ~]# docker search mysql --filter=STARS=3000
  8. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  9. mysql MySQL is a widely used, open-source relation 10999 [OK]
  10. mariadb MariaDB Server is a high performing open sou 4165 [OK]
  11. [root@localhost ~]# docker search mysql --filter=STARS=5000
  12. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  13. mysql MySQL is a widely used, open-source relation 10999 [OK]

docker pull 下载镜像

  1. [root@localhost ~]# docker pull mysql
  2. Using default tag: latest #不写tag默认是latest最新版本
  3. latest: Pulling from library/mysql
  4. 69692152171a: Pull complete #分层下载 docker的核心就是联合文件系统
  5. 1651b0be3df3: Pull complete
  6. 951da7386bc8: Pull complete
  7. 0f86c95aa242: Pull complete
  8. 37ba2d8bd4fe: Pull complete
  9. 6d278bb05e94: Pull complete
  10. 497efbd93a3e: Pull complete
  11. f7fddf10c2c2: Pull complete
  12. 16415d159dfb: Pull complete
  13. 0e530ffc6b73: Pull complete
  14. b0a4a1a77178: Pull complete
  15. cd90f92aa9ef: Pull complete
  16. Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #签名
  17. Status: Downloaded newer image for mysql:latest
  18. docker.io/library/mysql:latest #真实地址
  19. docker pull mysql == docker pull docker.io/library/mysql:latest
  20. # 指定版本下载
  21. [root@localhost ~]# docker pull mysql:5.7
  22. 5.7: Pulling from library/mysql
  23. 69692152171a: Already exists
  24. 1651b0be3df3: Already exists
  25. 951da7386bc8: Already exists
  26. 0f86c95aa242: Already exists
  27. 37ba2d8bd4fe: Already exists
  28. 6d278bb05e94: Already exists
  29. 497efbd93a3e: Already exists
  30. a023ae82eef5: Pull complete
  31. e76c35f20ee7: Pull complete
  32. e887524d2ef9: Pull complete
  33. ccb65627e1c3: Pull complete
  34. Digest: sha256:a682e3c78fc5bd941e9db080b4796c75f69a28a8cad65677c23f7a9f18ba21fa
  35. Status: Downloaded newer image for mysql:5.7
  36. docker.io/library/mysql:5.7
  37. [root@localhost ~]# docker images
  38. REPOSITORY TAG IMAGE ID CREATED SIZE
  39. mysql 5.7 2c9028880e58 5 weeks ago 447MB
  40. mysql latest c0cdc95609f1 5 weeks ago 556MB

docker rmi 删除镜像

  1. docker rmi -f 容器id #删除指定容器 删除多个后边空格继续跟容器id
  2. docker rmi -f $(docker images -aq) #删除全部的容器

容器命令

有了镜像才能创建容器

运行容器

  1. docker run [可选参数] image
  2. # 扩展参数
  3. --name="name" 容器名字,例:tomcat1tomcat2用来区分容器
  4. -d 后台方式运行
  5. -it 使用交互方式运行,进入容器查看内容
  6. -p 指定容器的端口 -p 8080:8080
  7. -p ip:主机端口:容器端口
  8. -p 主机端口:容器端口(常用)
  9. -p 容器端口
  10. 容器端口
  11. -P 指定随机端口

列出所有的运行容器

  1. docker ps 命令 #列出当前正在运行的容器
  2. -a #列出当前正在运行的容器以及历史运行过的容器
  3. -n=? #显示最近创建的容器
  4. -q #只显示容器的编号

image.png

退出容器

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

删除容器

  1. docker rm 容器id #删除指定容器,该容器如果处于运行状态用rm -f删除
  2. docker rm -f $(docker ps -aq) #删除所有的容器
  3. docker ps -aq|xargs docker rm #删除所有的容器

启动和停止容器

  1. docker start 容器id #启动容器
  2. docker restart 容器id #重启容器
  3. docker stop 容器id #停止当前容器
  4. docker kill 容器id #强制终止当前容器

其他命令

后台启动容器

  1. docker run -d 镜像名
  2. # docker后台运行,必须要有一个前台程序,否则会自动停止
  3. # 例如nginx 容器启动后 发现没有提供服务 就会立即停止 就是没有程序了

查看日志

  1. docker logs -tf --tail 容器
  2. 例:
  3. [root@localhost ~]# docker run -it centos
  4. [root@a8721bbc495c /]# /bin/bash -c "while true;do echo hello;sleep 1;done"
  5. ctrl+p+q
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. a8721bbc495c centos "/bin/bash" 3 minutes ago Up 3 minutes keen_wilson
  9. 191a77d995f9 centos "/bin/bash -c 'while…" 4 minutes ago Up 4 minutes optimistic_wozniak
  10. #显示日志:
  11. -tf #显示日志
  12. --tail number #显示日志条数
  13. [root@localhost ~]# docker logs -tf --tail 10 a8721bbc495c

查看容器中进程信息

  1. [root@localhost ~]# docker top a8721bbc495c
  2. UID PID PPID C STIME TTY TIME CMD
  3. root 8781 8760 0 10:22 pts/0 00:00:00 /bin/bash
  4. root 8952 8781 0 10:23 pts/0 00:00:00 /bin/bash -c while true;do echo hello;sleep 1;done
  5. root 10191 8952 0 10:31 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像的源数据

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

进入当前正在运行的容器(exec 新建一个终端操作,常用)

  1. docker exec -it 容器id bashshell
  2. #例子:
  3. [root@localhost ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. a8721bbc495c centos "/bin/bash" 15 minutes ago Up 15 minutes keen_wilson
  6. 191a77d995f9 centos "/bin/bash -c 'while…" 16 minutes ago Up 16 minutes optimistic_wozniak
  7. [root@localhost ~]# docker exec -it a8721bbc495c /bin/bash
  8. [root@a8721bbc495c /]# ls
  9. bin etc lib lost+found mnt proc run srv tmp var
  10. dev home lib64 media opt root sbin sys usr
  11. [root@a8721bbc495c /]# ps -ef
  12. UID PID PPID C STIME TTY TIME CMD
  13. root 1 0 0 02:22 pts/0 00:00:00 /bin/bash
  14. root 16 1 0 02:23 pts/0 00:00:00 /bin/bash -c while true;do echo hello;sl
  15. root 903 0 0 02:38 pts/1 00:00:00 /bin/bash
  16. root 929 16 0 02:38 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-sheb
  17. root 930 903 0 02:38 pts/1 00:00:00 ps -ef

进入当前正在运行的容器(attach 进入原来的终端操作,不会新建)

  1. docker attach 容器id
  2. #例子:
  3. [root@localhost ~]# docker attach 191a77d995f9
  4. hello
  5. hello
  6. hello
  7. hello

CMD和ENTRYPOINT的区别

CMD # 指定容器启动时运行的命令,只有最后一个会生效,可替代 ENTRYPOINT # 指定容器启动时运行的命令,可以追加命令

从容器内拷贝文件到主机

  1. docker cp 容器id:容器内路径 目的主机路径
  2. #例子:
  3. [root@localhost ~]# ls
  4. anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
  5. [root@localhost ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. a8721bbc495c centos "/bin/bash" 24 minutes ago Up 24 minutes keen_wilson
  8. 191a77d995f9 centos "/bin/bash -c 'while…" 24 minutes ago Up 24 minutes optimistic_wozniak
  9. [root@localhost ~]# docker exec a8721bbc495c /bin/bash
  10. [root@localhost ~]# docker exec -it a8721bbc495c /bin/bash
  11. [root@a8721bbc495c /]# cd /home
  12. [root@a8721bbc495c home]# ls
  13. [root@a8721bbc495c home]# touch 123.txt
  14. [root@a8721bbc495c home]# exit
  15. exit
  16. [root@localhost ~]# docker cp a8721bbc495c:/home/123.txt /home
  17. [root@localhost ~]# cd /home
  18. [root@localhost home]# ls
  19. 123.txt cao

总结

image.png

docker容器数据卷

将容器内的目录挂载到本地 数据的持久化 容器之间可以通过—volumes-from来实现数据共享

直接用命令来挂载 -v

  1. docker run -it -v 主机目录:容器内目录
  2. #测试
  3. docker run -it -v /home/ceshi:/home centos /bin/bash
  4. #起来以后可以通过docker inspect 容器id

image.png

数据卷容器数据共享

  1. #mysql数据共享,生命周期持续到没有容器使用为止,一旦持久化到本地
  2. docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
  3. docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7

image.png

实战MySQL数据持久

  1. #pull拉取MySQL镜像
  2. docker pull mysql:5.7
  3. #官方运行案例:
  4. docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:tag
  5. #运行容器
  6. docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7

具名和匿名挂载

-v 容器内路径 #匿名挂载 -v 卷名:容器内路径 #具名挂载 -v /宿主机路径:容器内路径 #指定路径挂载

docker run -d -P —name nginx01 -v juming-nginx:/etc/nginx:ro nginx

ro 限制仅可读

rw 限制仅可读写

  • 具名 ```javascript [root@localhost ~]# docker run -d -P —name nginx01 -v juming-nginx:/etc/nginx nginx WARNING: IPv4 forwarding is disabled. Networking will not work. fcec760cb0fce1c5d5f618718e1afc1e045c9878b8ea8acf9d6020bbb7359711

    查看所有volume的情况

    [root@localhost ~]# docker volume ls DRIVER VOLUME NAME local juming-nginx [root@localhost ~]# docker volume inspect juming-nginx [ {
    1. "CreatedAt": "2021-07-19T16:49:23+08:00",
    2. "Driver": "local",
    3. "Labels": null,
    4. "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
    5. "Name": "juming-nginx",
    6. "Options": null,
    7. "Scope": "local"
    } ]

所有的docker容器中的卷在没有指定目录的情况下都是挂载到 /var/lib/docker/volumes/xxxx/_data

具名挂载也是常用的挂载方式

  1. - **匿名**
  2. ```javascript
  3. [root@localhost ~]# docker run -P -d --name nginx00 -v /etc/nginx nginx
  4. WARNING: IPv4 forwarding is disabled. Networking will not work.
  5. b6d370c96836e42ee0d54f0daff04212fb00ac13ffd135f4285a75e7f593dd7b
  6. [root@localhost ~]# docker volume ls
  7. DRIVER VOLUME NAME
  8. local 1c09a343b680b1c76a9a256f29ec2ba14084d484eacf10620aef126472b8a3f4
  9. local juming-nginx

dockerfile

用来构建docker镜像的构造文件,实际就是命令脚本 步骤: 1、写脚本文件 2、build构建镜像 3、run运行镜像 4、push发布镜像(dockerhub、阿里云镜像仓库)

  1. #脚本中的指定都是大写,官方案例
  2. FROM scratch
  3. ADD centos-7-x86_64-docker.tar.xz /
  4. LABEL \
  5. org.label-schema.schema-version="1.0" \
  6. org.label-schema.name="CentOS Base Image" \
  7. org.label-schema.vendor="CentOS" \
  8. org.label-schema.license="GPLv2" \
  9. org.label-schema.build-date="20201113" \
  10. org.opencontainers.image.title="CentOS Base Image" \
  11. org.opencontainers.image.vendor="CentOS" \
  12. org.opencontainers.image.licenses="GPL-2.0-only" \
  13. org.opencontainers.image.created="2020-11-13 00:00:00+00:00"
  14. CMD ["/bin/bash"]

命令参数

  1. FROM # 基础镜像,最底层的镜像
  2. MAINTAINER # 镜像是谁写的,标准格式:姓名+邮箱
  3. RUN # 镜像构建的时候序言运行的命令
  4. ADD # 添加内容
  5. WORKDIR # 镜像的工作目录
  6. VOLUME # 镜像的挂载目录
  7. EXPOSE # 暴漏端口配置
  8. CMD # 指定容器启动的时候要运行的命令,只有最后一个会生效,可被替换
  9. ENTRYPOINT # 指定容器启动的时候要运行的命令,可以追加命令
  10. ONBUILD # 当构建一个被继承的 DockerFile 这个时候就会运行 Onbuild 指令,触发指令
  11. COPY # 类似ADD,将我们的文件拷贝到镜像
  12. ENV # 构建的时候设置的环境变量