经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
docker 部署环境基本流程
来源:cnblogs  作者:zylyehuo  时间:2023/9/13 15:39:52  对本文有异议

博客地址:https://www.cnblogs.com/zylyehuo/

环境部署的问题,非常棘手,因此引入了容器技术

解决环境迁移的难题

1.利用虚拟机的模板克隆功能,将整个机器的环境复制一份,再丢给第二个机器去使用

2.最好是使用docker去部署环境

docker的生命周期概念

  • 镜像,是一个系统的只读模板,例如一个微型的centos系统镜像
  • 容器,容器进程,应用程序以后封装在容器中去运行,相互隔离
  • 仓库,存储镜像的一个仓库地址,便于和他人共享镜像文件的一个地方
  1. 基于镜像运行处容器
  2. 基于一个镜像,可以运行处N多个容器实例
  3. python的面向对象去理解的话
  4. docker的镜像---------理解为python class
  5. docker的容器---------理解为class的实例化对象
  6. class Stu():
  7. def __init__(self):
  8. self.age=18
  9. self.height=180
  10. self.weight=280
  11. 老王=Stu()
  12. 小李=Stu()
  13. 小张=Stu()
  14. 问,老王,小李,小张之间有什么相同性吗? 3个对象,都有相同的Init里面的实例化属性
  15. 基于如上的概念理解,基于同一个镜像文件,运行出的容器实例,其容器内的环境,也是一模一样的

安装docker

1.安装docker软件

  1. # 使用阿里云的yum源,可以直接安装docker软件,阿里云的docker软件版本可能较低,如果要下载新的,去docker官网找
  2. [root@localhost ~]# yum install docker -y

2.配置docker的镜像加速器

加速系统镜像的下载,默认是去国外下载,比较慢
能够加速下载你所需要的各种镜像,来自于如下提供的3个镜像站点
比如你想快速的使用tornado模块去开发一些东西

  • 编译安装python3
  • 安装tornado模块及依赖关系
  • 加上你的代码才能够运行

当你有了docker技术
docker search tornado # 直接搜索和tornado有关的镜像,是其他人制作好的镜像
docker pull tornado # 直接下载该镜像,和代码结合使用,docker解决了,省去了你配置环境的一些步骤

  1. [root@localhost ~]# vim /etc/docker/daemon.json # 修改docker的配置文件,修改docker镜像的下载地址,在国内下载比较快
  1. {
  2. "registry-mirrors": [
  3. "https://dockerhub.azk8s.cn",
  4. "https://hub-mirror.c.163.com",
  5. "https://pee6w651.mirror.aliyuncs.com"
  6. ]
  7. }

3.重启docker,运行docker

  1. [root@localhost ~]# systemctl restart docker

4.获取一个centos的基础镜像

docker的系统镜像,非常小,centos只有200M左右

  1. [root@localhost ~]# docker pull centos

使用docker容器、镜像的增删改查命令

对于后端开发的程序员,只需要掌握Docker的容器,镜像,仓库的增删改查命令即可

  1. # 1.从dockerhub 仓库中获取docker的镜像,从github获取代码一个道理
  2. docker pull centos # 去docker仓库中寻找centos系统镜像
  3. docker pull ubuntu # 获取ubuntu镜像
  4. # 2.获取一个hello-world进程
  5. [root@localhost ~]# docker pull hello-world
  6. # 3.获取一个ubuntu镜像
  7. [root@localhost ~]# docker pull ubuntu
  8. [root@localhost ~]# docker images
  9. REPOSITORY TAG IMAGE ID CREATED SIZE
  10. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  11. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  12. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  13. [root@localhost ~]# docker run -it 9c7 /bin/bash
  14. /usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:290: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory".
  15. [root@localhost ~]# docker run -it ba6 /bin/bash
  16. root@afe83ed4db1b:/# cat /etc/os-release
  17. NAME="Ubuntu"
  18. VERSION="20.04.3 LTS (Focal Fossa)"
  19. ID=ubuntu
  20. ID_LIKE=debian
  21. PRETTY_NAME="Ubuntu 20.04.3 LTS"
  22. VERSION_ID="20.04"
  23. HOME_URL="https://www.ubuntu.com/"
  24. SUPPORT_URL="https://help.ubuntu.com/"
  25. BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
  26. PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
  27. VERSION_CODENAME=focal
  28. UBUNTU_CODENAME=focal
  29. # 4.搜索相关的镜像
  30. [root@localhost ~]# docker search python3
  31. INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  32. docker.io docker.io/faucet/python3 Python3 docker image for amd64 7
  33. docker.io docker.io/openwhisk/python3action Apache OpenWhisk runtime for Python 3 Actions 6
  34. # 比如想用nginx,又不想修改宿主机的一个软件环境,直接用docker安装
  35. [root@localhost ~]# docker search nginx
  36. [root@localhost ~]# docker pull nginx
  37. [root@localhost ~]# docker run nginx # nginx服务器就能够运行在容器中,然后和宿主机有一个端口映射,就可以访问了

  1. # 1.删除本地镜像文件
  2. docker rmi 镜像id
  3. [root@localhost ~]# docker rmi 要删除镜像id的前3 # 删除镜像id的前3位即可,必须要先删除有相关依赖的容器进程记录
  4. # 2.删除容器记录的命令
  5. [root@localhost ~]# docker rm 容器id3
  6. # 3.批量清空无用的docker容器记录,容器记录非常容易创建docker run
  7. # 批量删除挂掉的容器记录
  8. [root@localhost ~]# docker rm `docker ps -aq` # 把docker容器记录的id号,保存在反引号中,丢给docker rm实现批量删除
  9. # 4.批量删除镜像
  10. [root@localhost ~]# docker rmi `docker iamges -aq`
  11. # 5.批量停止容器
  12. [root@localhost ~]# docker stop `docker ps -aq`
  13. [root@localhost ~]# docker start 容器id # 启动暂停的容器
  14. [root@localhost ~]# docker stop 容器id # 暂停一个容器
  15. [root@localhost ~]# docker restart 容器id # 重启容器

  1. # 1.运行第一个docker的容器实例,运行镜像文件,产生容器进程
  2. docker run 镜像文件的名字即可
  3. [root@localhost ~]# docker run centos # 运行centos基础镜像,如果docker容器中没有在后台运行的进程,容器会直接挂掉
  4. # 如果你发现你的容器没有启动成功,说明容器内部出错了,程序没有运行
  5. # 2.运行一个hello world容器进程
  6. [root@localhost ~]# docker run hello-world
  7. Hello from Docker!
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10. 1. The Docker client contacted the Docker daemon.
  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  12. (amd64)
  13. 3. The Docker daemon created a new container from that image which runs the
  14. executable that produces the output you are currently reading.
  15. 4. The Docker daemon streamed that output to the Docker client, which sent it
  16. to your terminal.
  17. To try something more ambitious, you can run an Ubuntu container with:
  18. $ docker run -it ubuntu bash
  19. Share images, automate workflows, and more with a free Docker ID:
  20. https://hub.docker.com/
  21. For more examples and ideas, visit:
  22. https://docs.docker.com/get-started/
  23. # 3.docker run指令还有一个功能是,当镜像不存在的时候,会自动去下载该进程
  24. [root@localhost ~]# docker run hello-world # 有2个功能,下载镜像,执行镜像
  25. # 4.交互式的运行一个存活的docker容器,centos
  26. # -it参数 -i 是交互式的命令操作 -t 是开启一个终端 /bin/bash 指定shell解释器
  27. # 容器空间内,有自己的文件系统
  28. # docker run -it centos /bin/bash # 运行centos镜像,且进入容器内,容器空间内是以容器id命名的
  29. [root@localhost ~]# docker run -it centos /bin/bash
  30. [root@e7b96652ac88 /]# cat /etc/os-release
  31. NAME="CentOS Linux"
  32. VERSION="8"
  33. ID="centos"
  34. ID_LIKE="rhel fedora"
  35. VERSION_ID="8"
  36. PLATFORM_ID="platform:el8"
  37. PRETTY_NAME="CentOS Linux 8"
  38. ANSI_COLOR="0;31"
  39. CPE_NAME="cpe:/o:centos:centos:8"
  40. HOME_URL="https://centos.org/"
  41. BUG_REPORT_URL="https://bugs.centos.org/"
  42. CENTOS_MANTISBT_PROJECT="CentOS-8"
  43. CENTOS_MANTISBT_PROJECT_VERSION="8"
  44. [root@e7b96652ac88 /]# exit
  45. exit
  46. # 5.运行出一个活着的容器,在后台不断执行程序的容器
  47. # docker run 运行镜像文件
  48. # -d 是让容器后台运行
  49. # -c 指定一段shell代码
  50. # 运行centos镜像,生成容器实例,且有一段shell代码,在后台不断运行,死循环打印一句话,每秒钟打印一次
  51. [root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo 辛苦学习linux; sleep 1;done"
  52. ae3c01990d86f42f11775bbfbfaa384bb6b3428f0e2eef16b289e2f16dd69a5c
  53. [root@localhost ~]# docker ps
  54. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  55. ae3c01990d86 centos "/bin/sh -c 'while..." 16 seconds ago Up 15 seconds angry_wing
  56. # 6.运行docker容器,且指定名字,便于管理
  57. # docker run --name "指定容器的运行名字“ -d centos /bin/sh -c "while true;do echo 辛苦学习linux; sleep 1;done"
  58. [root@localhost ~]# docker run --name "test" -d centos /bin/sh -c "while true;do echo 辛苦学习linux; sleep 1;done"
  59. d0beb2b2340bcd14e578938812e9394065974d5cf2af787a66630bf2b76422e1
  60. [root@localhost ~]# docker ps
  61. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  62. d0beb2b2340b centos "/bin/sh -c 'while..." 26 seconds ago Up 25 seconds test
  63. e3db5a209d69 centos "/bin/sh -c 'while..." 40 seconds ago Up 39 seconds stoic_edison
  64. ae3c01990d86 centos "/bin/sh -c 'while..." 4 minutes ago Up 4 minutes angry_wing
  65. # 7.进入一个正在运行的容器空间,进入一个线上正在运行的容器程序,修改其内部的资料
  66. # docker exec -it 容器id /bin/bash
  67. [root@localhost ~]# docker ps
  68. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  69. d0beb2b2340b centos "/bin/sh -c 'while..." 4 minutes ago Up 4 minutes test
  70. e3db5a209d69 centos "/bin/sh -c 'while..." 5 minutes ago Up 5 minutes stoic_edison
  71. ae3c01990d86 centos "/bin/sh -c 'while..." 8 minutes ago Up 8 minutes angry_wing
  72. [root@localhost ~]# docker exec -it ae3 /bin/bash
  73. [root@localhost ~]# docker exec -it ae3 /bin/bash
  74. [root@ae3c01990d86 /]# ps -ef
  75. UID PID PPID C STIME TTY TIME CMD
  76. root 1 0 0 07:26 ? 00:00:00 /bin/sh -c while true;do echo ????????????linux; sleep 1;done
  77. root 595 0 0 07:35 ? 00:00:00 /bin/bash
  78. root 627 1 0 07:36 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
  79. root 628 595 0 07:36 ? 00:00:00 ps -ef
  80. # 8.如何进入容器空间内,修改容器内的环境,以及代码等内容,修改软件等操作,且提交镜像,发送给其他人
  81. # --8.1 进入容器空间内,安装一个vim或是python3等步骤
  82. [root@localhost ~]# docker run -it centos /bin/bash
  83. [root@300d9440c4d1 /]# vi /etc/resolv.conf
  84. # Generated by NetworkManager
  85. search localdomain
  86. nameserver 10.0.0.2
  87. nameserver 223.5.5.5
  88. nameserver 119.29.29.29
  89. [root@300d9440c4d1 /]# yum install vim -y
  90. # 报错参考下方地址
  91. # https://blog.csdn.net/weixin_43252521/article/details/124409151
  92. # --8.2 安装好vim后,退出容器空间
  93. [root@localhost ~]# exit
  94. exit
  95. # --8.3 提交该容器,生成新的镜像文件
  96. [root@localhost ~]# docker ps -a
  97. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  98. 89efe007cff2 centos "/bin/bash" About a minute ago Exited (127) 2 seconds ago modest_wright
  99. [root@localhost ~]# docker commit 89efe007cff2 s25-centos-vim
  100. sha256:c73dfef4b276d4fb84e8778f672e6280952e1907eb2d0f21e713f638f6d40896
  101. [root@localhost ~]# docker images
  102. REPOSITORY TAG IMAGE ID CREATED SIZE
  103. s25-centos-vim latest c73dfef4b276 29 seconds ago 231 MB
  104. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  105. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  106. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  107. # 9.导出你的docker镜像,可以发送给同事,或是其他人使用
  108. # docker save 镜像id > 镜像的压缩文件
  109. # 官方文档解释的是,docker save用的是tar命令压缩,应该是没有其他压缩格式的
  110. [root@localhost ~]# docker save c73dfef4b276 > /opt/s25-centos-vim.tar.gz
  111. [root@localhost ~]# ll -h /opt/
  112. total 253M
  113. drwxr-xr-x. 4 root root 68 Sep 4 22:57 mysite
  114. drwxr-xr-x. 6 root root 56 Sep 4 21:06 python369
  115. drwxr-xr-x. 18 501 501 4.0K Sep 4 21:05 Python-3.6.9
  116. -rw-r--r--. 1 root root 22M Jul 3 2019 Python-3.6.9.tgz
  117. drwxr-xr-x. 3 root root 18 Sep 10 14:11 redis
  118. -rw-r--r--. 1 root root 228M Sep 11 16:35 s25-centos-vim.tar.gz
  119. drwxrwxr-x. 14 root root 4.0K Sep 6 00:04 tengine-2.3.2
  120. -rw-r--r--. 1 root root 2.8M Jul 21 16:33 tengine-2.3.2.tar.gz
  121. drwxr-xr-x. 9 root root 141 Sep 5 11:06 tf_crm
  122. drwxr-xr-x. 11 root root 151 Sep 6 00:14 tngx232
  123. drwxr-xr-x. 4 root root 64 Sep 4 23:29 venv1
  124. drwxr-xr-x. 3 root root 60 Sep 4 23:40 venv1_dj119
  125. drwxr-xr-x. 4 root root 64 Sep 4 23:45 venv2
  126. drwxr-xr-x. 3 root root 60 Sep 4 23:52 venv2_dj201
  127. # 你可以删掉本地的镜像,然后重新导入该压缩文件,模拟发送给同事的操作
  128. [root@localhost ~]# docker rmi c73
  129. Untagged: s25-centos-vim:latest
  130. Deleted: sha256:c73dfef4b276d4fb84e8778f672e6280952e1907eb2d0f21e713f638f6d40896
  131. Deleted: sha256:45322746ea754e77d102f1a90fbbf061d7a71b432a14646dd3730923badad9e8
  132. # 10.如何进行docker镜像导入
  133. # 比如小李运维同志,他收到了该docker镜像压缩文件,在他的机器上导入该镜像
  134. [root@localhost ~]# docker images
  135. REPOSITORY TAG IMAGE ID CREATED SIZE
  136. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  137. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  138. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  139. [root@localhost ~]# docker load < /opt/s25-centos-vim.tar.gz
  140. 390029cde3d2: Loading layer [==================================================>] 4.096 kB/4.096 kB
  141. Loaded image ID: sha256:c73dfef4b276d4fb84e8778f672e6280952e1907eb2d0f21e713f638f6d40896
  142. [root@localhost ~]# docker images
  143. REPOSITORY TAG IMAGE ID CREATED SIZE
  144. <none> <none> c73dfef4b276 5 minutes ago 231 MB
  145. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  146. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  147. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  148. # 首次导入该进项的时候,发现丢失了镜像tag标签,重新赋予一个即可
  149. [root@localhost ~]# docker tag c73dfef4b276 s25-new-centos-vim
  150. [root@localhost ~]# docker images
  151. REPOSITORY TAG IMAGE ID CREATED SIZE
  152. s25-new-centos-vim latest c73dfef4b276 7 minutes ago 231 MB
  153. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  154. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  155. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  156. # 11.如何在docker内,运行一个python web的程序,需要用到端口映射知识
  157. # -d 后台运行
  158. # -P 大写的P参数,作用是随机的端口映射
  159. # training/webapp 是镜像的名字,默认没有会去在线下载
  160. # python app.py 代表启动容器后,让容器执行的命令是它
  161. # 因此这个命令作用是,启动一个webapp镜像,且在容器中执行 python app.py
  162. # -p 6000:5000 访问宿主机的6000,就是访问容器的5000了
  163. [root@localhost ~]# docker run --name "s25webdocker" -d -p 6000:5000 training/webapp python app.py
  164. Unable to find image 'training/webapp:latest' locally
  165. Trying to pull repository docker.io/training/webapp ...
  166. latest: Pulling from docker.io/training/webapp
  167. e190868d63f8: Downloading [==================================================>] 65.77 MB/65.77 MB
  168. 909cd34c6fd7: Downloading [==================================================>] 71.48 kB/71.48 kB
  169. 0b9bfabab7c1: Downloading [==================================================>] 682 B/682 B
  170. a3ed95caeb02: Download complete
  171. 10bbbc0fc0ff: Download complete
  172. fca59b508e9f: Download complete
  173. e7ae2541b15b: Download complete
  174. 9dd97ef58ce9: Download complete
  175. a4c1b0cb7af7: Download complete
  176. latest: Pulling from docker.io/training/webapp
  177. e190868d63f8: Pull complete
  178. 909cd34c6fd7: Pull complete
  179. 0b9bfabab7c1: Pull complete
  180. a3ed95caeb02: Pull complete
  181. 10bbbc0fc0ff: Pull complete
  182. fca59b508e9f: Pull complete
  183. e7ae2541b15b: Pull complete
  184. 9dd97ef58ce9: Pull complete
  185. a4c1b0cb7af7: Pull complete
  186. Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
  187. Status: Downloaded newer image for docker.io/training/webapp:latest
  188. 4a18674efa8e4b28639695bd0b144a07644e05fbe4f0e6725a02f311d18d034d
  189. [root@localhost ~]# docker ps
  190. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  191. 4a18674efa8e training/webapp "python app.py" 38 seconds ago Up 37 seconds 0.0.0.0:6000->5000/tcp s25webdocker
  192. [root@localhost ~]# docker port 4a1
  193. 5000/tcp -> 0.0.0.0:6000
  194. [root@localhost ~]# curl 127.0.0.1:6000
  195. Hello world![root@localhost ~]#
  196. # 12.进入该webapp的容器,查看里面的内容
  197. # docker exec -it 容器id /bin/bash # 进入容器内,可以进行相应的修改操作
  198. # docker restart 容器id # 改动代码后需要重启该容器,重新读取代码,方可生效
  199. [root@localhost ~]# docker exec -it 4a18674efa8e /bin/bash
  200. root@4a18674efa8e:/opt/webapp# ls
  201. Procfile app.py requirements.txt tests.py
  202. root@4a18674efa8e:/opt/webapp# cat requirements.txt
  203. Flask
  204. Jinja2
  205. Werkzeug
  206. distribute
  207. wsgiref
  208. root@4a18674efa8e:/opt/webapp# vi app.py
  209. root@4a18674efa8e:/opt/webapp# exit
  210. exit
  211. [root@localhost ~]# curl 127.0.0.1:6000
  212. Hello world![root@localhost ~]# docker restart 4a18674efa8e
  213. 4a18674efa8e
  214. [root@localhost ~]# docker ps
  215. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  216. 4a18674efa8e training/webapp "python app.py" 17 minutes ago Up 6 seconds 0.0.0.0:6000->5000/tcp s25webdocker
  217. [root@localhost ~]# curl 127.0.0.1:6000
  218. Hello zylyehuo world![root@localhost ~]#

  1. # 1.查看本地机器,所有的镜像文件内容
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  5. # 2.查看docker正在运行的进程
  6. [root@localhost ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. # 3.查看所有运行,以及挂掉的容器进程
  9. [root@localhost ~]# docker ps -a
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. 494ae785d793 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago stoic_dubinsky
  12. # 4.查看容器内的运行日志
  13. # docker logs 容器id
  14. # docker logs -f 容器id # 实时刷新容器内的日志,例如检测nginx等日志信息
  15. [root@localhost ~]# docker ps
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. d0beb2b2340b centos "/bin/sh -c 'while..." 26 seconds ago Up 25 seconds test
  18. e3db5a209d69 centos "/bin/sh -c 'while..." 40 seconds ago Up 39 seconds stoic_edison
  19. ae3c01990d86 centos "/bin/sh -c 'while..." 4 minutes ago Up 4 minutes angry_wing
  20. [root@localhost ~]# docker logs ae3
  21. 辛苦学习linux
  22. 辛苦学习linux
  23. 辛苦学习linux
  24. 辛苦学习linux
  25. 辛苦学习linux
  26. 辛苦学习linux
  27. [root@localhost ~]# docker logs -f ae3
  28. 辛苦学习linux
  29. 辛苦学习linux
  30. # 5.查看容器内的端口转发情况
  31. docker port 容器id # 查看容器的端口转发
  32. [root@localhost ~]# docker port 4a1
  33. 5000/tcp -> 0.0.0.0:6000

dockerfile

手写一个dockerfile,运行出python的应用

dockerfile常用指令学习

FROM 指令表示,告诉该dockerfile以哪个镜像为基础

比如你的技术老大,要求你们程序运行在ubuntu中

  1. # FROM ubuntu
  2. # FROM centos
  3. FROM scratch # 制作base image 基础镜像,尽量使用官方的image作为base image
  4. FROM centos # 使用base image
  5. FROM ubuntu:14.04 # 带有tag的base image

LABEL标签,定义变量,定义作者信息等

  1. LABEL version=“1.0 # 容器元信息,帮助信息,Metadata,类似于代码注释
  2. LABEL maintainer=“yc_uuu@163.com"

RUN是一个完成指令,你可以用它在docker中执行任意的命令

RUN就是告诉容器要做哪些配置

用RUN指令告诉dockerfile他该去做什么事

  1. RUN mkdir /s25牛批
  2. RUN cd /s25牛批
  3. RUN cd
  4. RUN pwd # 会输出什么? 因此在容器中会输出用户家目录
  1. # 对于复杂的RUN命令,避免无用的分层,多条命令用反斜线换行,合成一条命令!
  2. # 要修改centos基础镜像的环境问题
  3. RUN yum update && yum install -y vim Python-dev # 反斜线换行
  4. RUN /bin/bash -c "source $HOME/.bashrc;echo $HOME”

WORKDIR,相当于linux的cd命令

  1. WORKDIR /root # 相当于linux的cd命令,改变目录,尽量使用绝对路径,不要用RUN cd
  2. WORKDIR /test # 如果没有就自动创建
  3. WORKDIR demo # 再进入demo文件夹
  4. RUN pwd # 打印结果应该是/test/demo
  1. # 案例
  2. WORKDIR /s25很棒
  3. WORKDIR 我们要说goodbay
  4. RUN pwd # 会输出什么? /s25很棒/我们要说goodbay了 此时进入了2层目录

ADD指令,用于添加宿主机的文件,放入到容器空间内

  1. # 宿主机有自己的文件系统,文件夹,文件,目录等
  2. # 容器内也有一套自己的文件系统,独立的文件信息
  3. # 把宿主机的代码,拷贝到容器内
  4. # ADD还有解压缩的功能,这是一个坑,需要注意
  5. ADD hello.txt /opt # 把宿主机的hello.txt拷贝到容器内的/opt目录下
  6. ADD test.tar.gz /opt /opt/test
  7. RUN tar -zxvf test.tar.gz # 直接报错,文件不存在,因为上一步,ADD指令已经对tar.gz压缩包解压缩了

COPY

  1. WORKDIR /root
  2. ADD hello test/ # 进入/root/ 添加hello可执行命令到test目录下,也就是/root/test/hello 一个绝对路径
  3. COPY hello test/ # 等同于上述ADD效果
  1. # dockerfile,用于从宿主机拷贝文件到容器内有2个指令一个ADD,一个COPY,COPY仅仅就是拷贝,尽量用COPY
  2. ADDCOPY
  3. - 优先使用COPY命令
  4. -ADD除了COPY功能还有解压功能
  5. # 添加远程文件/目录使用curl或wget

ENV,设置环境变量

  1. # ENV用于设置环境变量,使用ENV能够增加可维护性
  2. ENV MYSQL_VERSION 8.0
  3. RUN yum install -y mysql-server=“${MYSQL_VERSION}”

dockfile实战,写一个flask容器脚本

构建镜像的步骤

1.准备好一个flask代码,检查需要哪些依赖步骤

  1. [root@localhost ~]# mkdir /s25docker
  2. [root@localhost ~]# cd /s25docker/
  3. [root@localhost s25docker]# vim s25_flask.py
  4. from flask import Flask
  5. app=Flask(__name__)
  6. @app.route('/')
  7. def hello():
  8. return "linux即将结束"
  9. if __name__=="__main__":
  10. app.run(host='0.0.0.0',port=8080)

2.在宿主机环境检查如何能够运行该脚本

  1. # 需要安装flask模块
  2. pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
  1. # 检查运行
  2. [root@localhost s25docker]# python3 s25_flask.py
  3. * Serving Flask app 's25_flask' (lazy loading)
  4. * Environment: production
  5. WARNING: This is a development server. Do not use it in a production deployment.
  6. Use a production WSGI server instead.
  7. * Debug mode: off
  8. * Running on all addresses.
  9. WARNING: This is a development server. Do not use it in a production deployment.
  10. * Running on http://10.0.0.129:8080/ (Press CTRL+C to quit)
  1. # 浏览器访问 10.0.0.129:8080

3.编写dockerfile脚本,注意名字必须是 大写Dockerfile

  1. [root@localhost s25docker]# vim Dockerfile
  2. # 写入如下的内容

注释版

  1. FROM python # 指定镜像,dockerhub提供好的python镜像,已经安装好了python3,很好用
  2. RUN pip3 install -i https://pypi.douban.com/simple flask # 在容器内安装flask模块
  3. ADD s25_flask.py /opt # 把宿主机的代码,拷贝到容器的/opt目录下
  4. WORKDIR /opt # 容器内进行目录切换
  5. EXPOSE 8080 # 打开容器的8080端口,用于和宿主机进行映射
  6. CMD ["python3","s25_flask.py"] # 在容器启动后,内部自动执行的命令是什么

无注释版

  1. FROM python
  2. RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
  3. ADD s25_flask.py /opt
  4. WORKDIR /opt
  5. EXPOSE 8080
  6. CMD ["python3","s25_flask.py"]

4.检查准备的脚本代码,以及Dockerfile文件

  1. [root@localhost s25docker]# ls
  2. Dockerfile s25_flask.py

5.构建该dockerfile,生成镜像

  1. [root@localhost s25docker]# docker build .
  2. ...
  3. Successfully built 0b8358ccc202
  4. ...

6.检查docker的镜像,是否生成docker images

  1. [root@localhost s25docker]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. <none> <none> 0b8358ccc202 59 seconds ago 928 MB
  4. s25-new-centos-vim latest c73dfef4b276 About an hour ago 231 MB
  5. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  6. docker.io/python latest a5d7930b60cc 20 months ago 917 MB
  7. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  8. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  9. docker.io/training/webapp latest 6fae60ef3446 8 years ago 349 MB
  1. # 可以修改一下镜像的标签
  2. [root@localhost s25docker]# docker tag 0b8 s25-flask
  3. [root@localhost s25docker]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. s25-flask latest 0b8358ccc202 2 minutes ago 928 MB
  6. s25-new-centos-vim latest c73dfef4b276 About an hour ago 231 MB
  7. docker.io/hello-world latest 9c7a54a9a43c 4 months ago 13.3 kB
  8. docker.io/python latest a5d7930b60cc 20 months ago 917 MB
  9. docker.io/ubuntu latest ba6acccedd29 23 months ago 72.8 MB
  10. docker.io/centos latest 5d0da3dc9764 24 months ago 231 MB
  11. docker.io/training/webapp latest 6fae60ef3446 8 years ago 349 MB

7.运行该镜像文件,查看是否能够运行容器内的flask

  1. [root@localhost s25docker]# docker run -d -p 8000:8080 0b8
  2. 888a9aab9fd44544706846345a43cf37a10cfd42456d6d54f5dcb33bdaf6ba71
  3. [root@localhost s25docker]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 888a9aab9fd4 0b8 "python3 s25_flask.py" 21 seconds ago Up 21 seconds 0.0.0.0:8000->8080/tcp condescending_boyd
  6. 4a18674efa8e training/webapp "python app.py" About an hour ago Up About an hour 0.0.0.0:6000->5000/tcp s25webdocker

8.访问宿主机端口,查看容器内的应用

  1. # 访问方式一
  2. [root@localhost s25docker]# curl 127.0.0.1:8000
  3. linux即将结束[root@localhost s25docker]#
  4. # 访问方式二
  5. # 浏览器访问 10.0.0.129:8000

9.可以修改容器内的代码,重启容器

  1. [root@localhost s25docker]# docker exec -it 888 /bin/bash
  2. root@888a9aab9fd4:/opt#
  3. # 修改容器内的代码
  4. root@888a9aab9fd4:/opt# sed -i "s/linux即将结束/linux结束/" s25_flask.py
  5. root@888a9aab9fd4:/opt# cat s25_flask.py
  6. from flask import Flask
  7. app=Flask(__name__)
  8. @app.route('/')
  9. def hello():
  10. return "linux结束"
  11. if __name__=="__main__":
  12. app.run(host='0.0.0.0',port=8080)

10.重启容器

  1. root@888a9aab9fd4:/opt# exit
  2. exit
  3. [root@localhost s25docker]# docker restart 888

11.再次访问容器内应用,查看更新的代码内容

  1. # 访问方式一
  2. [root@localhost s25docker]# curl 127.0.0.1:8000
  3. linux结束[root@localhost s25docker]#
  4. # 访问方式二
  5. # 浏览器访问 10.0.0.129:8000

dockerhub仓库

dockerhub仓库就是和github一样的概念

github---托管程序员的代码

dockerhub----托管程序员编写的docker镜像

  1. 1.docker提供了一个类似于github的仓库dockerhub,
  2. 网址 https://hub.docker.com/ 需要注册使用
  3. 2.注册docker id后,在linux中登录dockerhub,会提示让你输入账号密码,正确登录之后,本台机器就和dockerhub绑定账号了,你的镜像推送就能够推送到该账户的dockerhub
  4. docker login
  5. 3.准备镜像推送
  6. 注意要保证imagetagdockerhub账户名,如果镜像名字不对,需要改一下tag
  7. docker tag 镜像id dockerhub的账号/centos-vim
  8. 语法是: docker tag 仓库名 yuchao163/仓库名
  9. 4.推送docker imagedockerhub,好比你准备git push推送代码一样
  10. docker push dockerhub账号/centos-vim
  11. 5.dockerhub中检查镜像,查看个人账户中的镜像文件
  12. https://hub.docker.com/
  13. 6.删除本地镜像,测试下载pull 镜像文件
  14. docker pull yuchao163/centos-vim

yaml配置文件

不同的配置文件,遵循的语法也不一样

  • json
  • Conf-----nginx.conf ,my.cnf
  • ini -----uwsgi.ini
  • xml------xml格式的配置文件
  • yaml-----新式配置文件,用在docker、salt、k8s等配置文件中,遵循python的缩进语法
  1. yaml语法规则
  2. 大小写敏感
  3. 使用缩进表示层级关系
  4. 缩进时禁止tab键,只能空格
  5. 缩进的空格数不重要,相同层级的元素左侧对其即可
  6. # 表示注释行
  7. yaml支持的数据结构
  8. 对象: 键值对,也称作映射 mapping 哈希hashes 字典 dict 冒号表示 key: value key冒号后必须有
  9. 数组: 一组按次序排列的值,又称为序列sequence 列表list 短横线 - list1
  10. 纯量: 单个不可再分的值
  11. 对象:键值对

python的字典套字典,数据结构表示如下

  1. {
  2. "s25":{
  3. "男同学":["宝元","太白","马jj"],
  4. "女同学":["景女神","alex"]
  5. }
  6. }

用yaml表示上述数据结构

在线yaml解析
https://www.bejson.com/validators/yaml_editor/

  1. "s25":
  2. "男同学":
  3. - "宝元"
  4. - "太白"
  5. - "马jj"
  6. "女同学":
  7. - "景女神"
  8. - "alex"

原文链接:https://www.cnblogs.com/zylyehuo/p/17693359.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号