(三)运行容器的最佳实践

(1)容器分类

​ 按用途容器大致可分为两类:服务类容器和工具类的容器。

​ 1.服务类容器以 daemon 的形式运行,对外提供服务。比如 web server,数据库等。通过 -d 以后台方式启动这类容器是非常合适的。如果要排查问题,可以通过 exec -it 进入容器。

​ 2.工具类容器通常给能我们提供一个临时的工作环境,通常以 run -it 方式运行,比如:

root@cuiyongchao:/dockerfile# docker run -it busybox
/ # ping www.baidu.com
PING www.baidu.com (180.101.49.11): 56 data bytes
64 bytes from 180.101.49.11: seq=0 ttl=127 time=33.820 ms
64 bytes from 180.101.49.11: seq=1 ttl=127 time=4.966 ms
64 bytes from 180.101.49.11: seq=2 ttl=127 time=4.794 ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 4.794/14.526/33.820 ms
/ # wget www.baidu.com
Connecting to www.baidu.com (180.101.49.11:80)
saving to 'index.html'
index.html           100% |**********************************************************************************|  2381  0:00:00 ETA
'index.html' saved
/ # 

运行 busybox,run -it 的作用是在容器启动后就直接进入。我们这里通过 wget 验证了在容器中访问 internet 的能力。执行 exit 退出终端,同时容器停止。

工具类容器多使用基础镜像,例如 busybox、debian、ubuntu 等。

(2)容器运行小结

容器运行相关的知识点:

  • 当 CMD 或 Entrypoint 或 docker run 命令行指定的命令运行结束时,容器停止。
  • 通过 -d 参数在后台启动容器。
  • 通过 exec -it 可进入容器并执行命令。

指定容器的三种方法:

  • 短ID。
  • 长ID。
  • 容器名称。 可通过 --name 为容器命名。重命名容器可执行docker rename

容器按用途可分为两类:

  • 服务类的容器。
  • 工具类的容器。
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/cuiyongchao007/p/13960090.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!