Docker的基本概述

89 阅读4分钟

Docker是一项不断增长的技术,并且像野火一样蔓延。是时候深入了解基础知识并将其应用于你的项目了

Docker的出现是为了提供一种高效、快速的方式在系统和机器之间移植应用程序。它是轻量级的,允许你快速包含应用程序,并在它们自己的安全环境中运行(通过Linux容器:LXC)。

"Docker是一个开发、运输和运行应用程序的开放平台。Docker的设计是为了更快地交付你的应用程序。有了Docker,你可以将你的应用程序与你的基础设施分开,并将你的基础设施当作一个可管理的应用程序。Docker帮助你更快地运送代码,更快地测试,更快地部署,并缩短编写代码和运行代码之间的周期。- docker.com

主要的Docker部分

  • docker守护程序用于管理它所运行的主机上的docker(LXC)容器。用户不直接与守护程序互动,而是通过Docker客户端。
  • docker CLI用于命令并与docker守护进程进行交流,其形式为docker 二进制。
  • docker图像索引是一个docker图像的存储库。

主要的Docker元素

Docker容器

使用docker移植应用程序的整个过程完全依赖于容器的运输。Docker容器基本上是一个目录,可以像其他目录一样被打包(tar-archived),然后在不同的机器和平台上共享和运行。

这里的容器是通过Linux容器(LXC)获得的。

Docker容器允许:应用程序的可移植性,隔离进程,防止与外界发生摩擦,管理资源消耗。

把容器想象成一个盒子里的进程。这个盒子包含了进程可能需要的一切。它有一个文件系统,有系统库,shell等等。

默认情况下,你从注册表中提取的容器将不会运行。你可以使用docker run [imagename] [command to run plus args] 来启动一个容器。容器执行完命令后,它将停止容器。

无论该命令做什么,都不会在镜像中被遗忘。如果你通过apt-get安装一些东西,它也不会被遗忘。然而,它还不会是持久性的。

要保存一个持久性的改变,你可以通过运行docker commit [id] [new/name] 来 "提交 "它。你通常只需要输入容器ID的前两到三个字符。要找到这个ID,请运行docker ps -l

提交将是一个新的 repo,并将返回一个新的镜像 ID。

要检查正在运行的容器,使用docker ps

要检查一个容器,请运行docker inspect [containerId]

Docker镜像

Docker镜像构成了docker容器的基础,一切都从它开始形成。它们与用于在服务器或桌面计算机上运行应用程序的操作系统磁盘镜像非常相似。

随着更多的层被添加到基础之上,新的镜像可以通过提交这些变化形成。当你使用一个容器时,联合文件系统将所有的层作为一个单一的实体聚集在一起。

Docker文件

Docker文件是包含一系列连续的指令、指示和命令的脚本,这些指令将被执行以形成一个新的docker镜像。

快速启动

# check docker commands
docker

# check docker version
docker version

# search for a docker image in Docker Hub Registry
docker search [name]

# downloading container images
# note that a repo might download other repo,
# making the original repo consist of many layers
docker pull [full/name/of/repository]

# start the container and make it run a command
# note that it will stop the container once it
# finishes executing the command
docker run [full/name/of/image] echo ‘hello world’

# install a program and make it persist
# note that whatever you do to the container will
# not be forgotten, but you need to run `commit` to
# persist the changes.
# here, we use -y argument to run the command
# in non-interactive mode
docker run [full/name/of/image] apt-get install ping -y

# get container ID
docker ps -l

# persist changes to the container
# you only need minimum of 3 or 4 characters here.
# note that this will return the new image ID.
docker commit [containerID] [new/repo/name]

# run command on new container ID
docker run [newContainerID] [run some command]

# inspect container
docker inspect [containerID]

# check local images
docker images

# push to registry (must be logged in)
docker push [full/repo/name]