Docker基础入门
2018-08-01 22:54
621 查看
初识Docker
虚拟化技术发展的如火如荼,特别的,在云时代,最为看重的是拼接虚拟化技术所构建的集群处理能力。不同于传统的虚拟化,容器化充分利用了操作系统本身已有的机制和特性,可以实现远超传统虚拟化的轻量级虚拟化。Docker则是被广泛应用。什么是Docker
Docker是基于GO语言开发的开源容器项目。现有主流的Linux操作系统都已经支持Docker。Docker的构想是要实现“Build,Ship And Run Any APP,Anywhere”,即通过对应用的封装(Packing)、分发(Distribution)、部署(Deployment)、运行(Runtime)生命周期进行管理,达到应用组件“一次封装,到处运行的目的”。为什么要使用Docker
docker容器虚拟化的好处
Docker使应用脱离了底层物理硬件的限制,同时也使得应用快速分发和部署Docker可以更快的交付和部署。使用Docker,开发人员可以使用镜像来快速构建一套标准的开发环境;开发完成后,测试和运维人员可以直接使用完全相同环境部署代码。Docker可以快速创建和删除容器,实现快速迭代,大量节约开发、测试、部署的时间。
Docker可以更高效的资源利用。Docker是内核级的虚拟机化,可以实现更高的性能,同时对资源的额外需求很低。
Docker可以更轻松的迁移和扩展。Docker容器几乎可以在任意的平台上运行,包括物理机、虚拟机、公有云等,
Docker可以更简单的更新管理。使用Dock而file,只需要小小的配置修改,就可以替代大量的更新工作。并且所有修改都以增量的方式被分发和更新,从而实现自动化并且高效的容器管理。
Docker和虚拟机比较
Docker容器很快,启动和停止可以在秒级实现。虚拟机方式需要数分钟Docker容器对系统资源需求少,一台主机上可以同时运行数千个Docker容器。
Docker可以类似Git设计理念的操作来方便用户获取、分发和更新应用镜像,存储复用、增量更新
Docker通过Dockerfile支持灵活创建和部署机制,提高工作效率
Docker核心概念
Docker有3大核心概念——镜像、容器和仓库##Docker镜像
Docker镜像类似于虚拟机镜像,可以将镜像理解为一个基本的操作系统环境,里面仅仅安装了一个应用程序,例如Apache应用程序,那么它就可以被称为Apache镜像。
镜像是创建Docker容器的基础。通过版本管理和增量的文件系统,Docker提供了一套简单的机制来创建和更新现有的镜像。
##Docker容器
Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用。容器是镜像创建的应用运行实例。可以将其启动、开始、停止、删除,而这些容器都是彼此相互分离的。
##Docker仓库
Docker仓库类似于代码仓库,它是Docker集中存放镜像文件的场所。有公有仓库和私有仓库之分。
#安装Docker
centOS 64位操作系统,内核版本至少为3.10
推荐使用CentOS 7
首先添加yum源
# tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseurl=http://yum.dockerproject.org/repo/main/centos/$releasever/ enable=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF
之后更新yum源缓存以及安装docker-engine
# yum update # yum install -y docker-engine
Windows7 74位及以上版本
1、开启CPU虚拟化特性。
2、安装Docker for Windows Installer。安装完之后如下图所示
3、此时点击Docker Quickstart即可运行
#使用Docker镜像
镜像(image)是Docker三大核心概念中最为重要的。
Docker命令一览
Usage: docker COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "C:\\Users\\Hui\\.docker") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "C:\\Users\\Hui\\.docker\\machine\\machines\\default\\ca.pem") --tlscert string Path to TLS certificate file (default "C:\\Users\\Hui\\.docker\\machine\\machines\\default\\cert.pem") --tlskey string Path to TLS key file (default "C:\\Users\\Hui\\.docker\\machine\\machines\\default\\key.pem") --tlsverify Use TLS and verify the remote (default true) -v, --version Print version information and quit Management Commands: config Manage Docker configs container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes
获取镜像
镜像是运行容器的前提,可以使用docker pull命令直接从Docker Hub镜像源来下载镜像。命令格式docker pull NAME[:TAG] NAME是镜像仓库的名称 TAG是镜像的标签(版本),例如 docker pull ubuntu:14.04 不指定TAG,默认选择latest标签(不稳定) 严格来讲,镜像的仓库名还需要加上仓库地址(registry,也即注册服务器)作为前缀,不添加则默认使用Docker Hub,例如 docker pull hub.c.163.com/public/ubuntu:14.04.表示指定地址下载镜像 pull子命令支持的参数 | 参数名 | 值类型 | 说明 | | ------------ | ------------ | ------------ | | -a | --all-tags=true|false | 是否获取仓库中所有的镜像,默认false |
获取镜像示例:
查看镜像信息
使用images命令列出镜像
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest c82521676580 8 days ago 109MB ubuntu 14.04 971bb384a50a 2 weeks ago 188MB hub.c.163.com/public/ubuntu 16.04 70b70c987e8f 2 years ago 224MB
字段解释
来源于哪个仓库,比如ubuntu仓库用来保存ubuntu系列的基础镜像
镜像标签,比如14.04、latest用来标注不同的版本信息
镜像ID(唯一标识镜像)
创建时间
镜像大小
images子命令主要参数
Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show numeric IDs
使用tag命令添加镜像标签
为了方便,可以使用docker tag命令来为本地镜像添加新的标签。使用inspect命令查看详细信息
这里返回一个json格式的消息,如果我们只要其中一项,可以使用-f来指定。
搜寻镜像
使用docker search命令可以搜索远端仓库中共享的镜像,默认搜索官方仓库镜像,OPTIONS如下:Options: -f, --filter filter Filter output based on conditions provided --format string Pretty-print search using a Go template --limit int Max number of search results (default 25) --no-trunc Don't truncate output
###删除镜像
使用标签删除镜像
当拥有同一个镜像多个标签时,docker rmi命令只是删除该镜像多个标签中的指定标签,并不影响镜像文件。格式为:
docker rmi IMAGE[:TAG]
使用镜像ID删除镜像
当使用docker rmi命令,后面跟上镜像ID,会尝试删除所有指向该镜像的标签,然后删除镜像本身
创建镜像
1、基于已有镜像的容器创建2、基于本地模板导入
3、基于Dockerfile创建
基于已有镜像的容器创建
该方法主要使用docker commit命令,命令格式:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]],主要选项包括: Options: -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") -c, --change list Apply Dockerfile instruction to the created image -m, --message string Commit message -p, --pause Pause container during commit (default true)
示例如下:
导出和载入
用户可以使用docker save和docker load命令来导出和载入镜像
1、镜像列表 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx test ca34a59e7ca1 6 minutes ago 109MB nginx latest c82521676580 8 days ago 109MB ubuntu 14.04 971bb384a50a 2 weeks ago 188MB hub.c.163.com/public/ubuntu 16.04 70b70c987e8f 2 years ago 224MB 2、保存镜像 $ docker save -o nginx_test.tar nginx:test open .docker_temp_707894571: Access is denied. 3、载入镜像 $ docker load --input nginx_test.tar open nginx_test.tar: The system cannot find the file specified.
Dockerfile创建的方法单独放一章节介绍
上传镜像
可以使用docker push命令上传镜像到仓库,命令格式如下:docker push NAME[:TAG] | REGISTRY_HOST[REGISTRY_PORT/]NAME[:TAG]
相关文章推荐
- Docker系列一之基础快速入门企业实战
- Docker自建虚拟机实验环境及基础入门
- 一、Docker 知识框架以及入门基础
- docker基础入门之一
- Docker入门基础(一)
- Docker 入门基础
- Docker基础入门索引
- Spring基础:快速入门spring boot(6):用docker进行Build/Ship/Run
- Docker从入门到飞升:基础配置安装(Ⅰ)
- 零基础入门 Docker
- Docker安装和基础用法 Docker入门教程第二篇
- Docker:入门基础
- Docker 基础入门与实践
- Docker 基础命令 简要入门
- 只要一小时,零基础入门 Docker
- Docker:入门基础