Docker学习笔记一
安装
使用Docker镜像
获取镜像
docker pull [镜像名:TAG]
如果不指定TAG,会自动获取最近的版本即latest,如下载CentOS镜像
这里的docker pull centos
相当于docker pull registry.hub.docker.com/centos:latest
如果从非官方仓库下载,镜像名前应加上服务器地址
后面还可以加以下命令
-a, --all-tags=true|false 是否获取仓库中的所有镜像,默认为否
--disable-content-trust 取消镜像的内容校验,默认为真
查看镜像信息
使用docker images
或docker image ls
可以列出主机上已有镜像的基本信息
REPOSITORY: 镜像来自于哪个仓库
TAG:镜像的标签,可以理解为版本
IMAGE ID:镜像的唯一标识,如果两个镜像的ID相同,说明他们实际上指向同一个镜像,只是具有不同的标签名
CREATED:创建时间
SIZE:镜像大小
后面还可以加以下命令
-a, --all=true|false 列出所有(包括临时文件)镜像文件,默认为否
--digests=true|false 列出镜像的数字摘要值,默认为否
-f, --filter=[] 过滤列出的镜像,如 dangling=true 只显示没有被使用的镜像,也可以指定带有特定标注的镜像等
--format="TEMPLATE" 控制输出格式,如.ID代表ID信息,.Repository代表仓库信息等
--no-trunc=true|false 对输出结果太长的部分是否进行截断,如镜像的ID信息,默认为是
-q, --quiet=true|false 仅输出ID信息,默认为否
使用tag命令添加镜像标签
docker tag 已有镜像名:已有标签 新镜像名:新标签
使用inspect命令查看详细信息
docker [image] inspect 镜像名:标签名
返回的是JSON格式的消息
可以使用-f
来指定查看某一部分的消息,例如:
使用history查看镜像历史
docker history 镜像名:标签名
搜寻镜像
使用docker search
命令可以搜索Docker Hub官方仓库中的镜像
docker search [option] keyword
Options:
-f, --filter filter Filter output based on conditions provided
--help Print usage
--limit int Max number of search results (default 25)
--no-index Don't truncate output
--no-trunc Don't truncate output
例如查询官方nginx镜像
删除和清理镜像
使用标签删除镜像
docker rmi 镜像名:标签名
docker image rm 镜像名:标签名
Options:
-f, --force Force removal of the image
--help Print usage
--no-prune Do not delete untagged parents
如果标签名为空,会默认为latest
使用标签删除镜像时,只是删除该镜像的多个标签中的一个指定标签,但是如果该镜像只剩下最后一个标签,那么再次删除会将该镜像彻底删除
使用镜像id删除镜像
使用dockr rmi 镜像id
可以删除与该ID相同的所有镜像,但是如果该镜像正在容器中运行,则无法删除,可以用-f
强行删除,但是除此之外有更好的方法,可以用docker pa -a
查找正在运行的容器,然后用docker rm 容器id
删除正在运行该镜像的容器,之后再删除镜像,例如:
清理镜像
使用Docker一段时间后, 系统中可能会遗留一些临时的镜像文件,以及一些没有被使用的镜像,可以通过docker image prune
命令来进行清理。
docker image Usage: docker image prune [OPTIONS]
Remove unused images
Options:
-a, --all Remove all unused images, not just dangling ones
-f, --force Do not prompt for confirmation
--help Print usage
创建镜像
创建镜像的方法主要有三种:基于已有镜像的容器创建、基于本地模板导入、基于Dockerfile创建。
基于已有容器创建
docker [container] commit [options] CONTAINER [镜像名:标签名]
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
如上图所示,可以先运行一个docker镜像,左侧用户名后是容器的ID,然后就可以根据这个容器ID创建一个新的镜像。
基于本地模板导入
docker import
指令:
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Set commit message for imported image
基于Dockerfile创建
这种方式是最常见的方式,Dockerfile 是一个文本文件,利用给定的指令描 述基于某个父镜像创建新镜像的过程。 创建镜像的过程可以用docker [image] build
命令。
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--build-arg list Set build-time variables (default [])
--cache-from stringSlice Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--help Print usage
--isolation string Container isolation technology
--label list Set metadata for an image (default [])
-m, --memory string Memory limit
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--security-opt stringSlice Security options
--shm-size string Size of /dev/shm, default value is 64MB
-t, --tag list Name and optionally a tag in the 'name:tag' format (default [])
--ulimit ulimit Ulimit options (default [])
-v, --volume list Set build-time bind mounts (default [])
具体使用方法之后会介绍
存出和载入镜像
存出镜像
命令:docker [image] save
,可以导出镜像到本地文件
Usage: docker image save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
--help Print usage
-o, --output string Write to a file, instead of STDOUT
载入镜像
命令:docker [image] load
,将导出的镜像文件再导入本地镜像库
Usage: docker image load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
--help Print usage
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
上传镜像
命令:docker [image] push
,将镜像上传到仓库,默认是Docker Hub官方仓库。
上传镜像之前 可以先用docker login
命令登录一下docker账户
Usage: docker image push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image verification (default true)
--help Print usage