zhaoyu@home:~$

docker-镜像

将镜像保存为文件

docker pull busybox:latest
docker save -o myfile.tar busybox:latest

删除镜像

docker rmi busybox

从文件中加载镜像

docker load –i myfile.tar

从Dockerfile安装镜像

Dockerfile是描述如何构建一个新image的脚本文件。ch3_dockerfile是一个包含Dockerfile的项目。构建镜像命令如下:

docker build -t dia_ch3/dockerfile:latest ch3_dockerfile

-t选项是你安装镜像的repository。

alpine

alpine的意思是“高山的”,是一套基于Musl libc 和busybox的完整的linux系统。最小的docker镜像只有5MB。

创建一个镜像

  1. 创建一个容器,并在容器中增加一个HelloWorld的文件。 shell script docker container run --name hw_container ubuntu:latest touch /HelloWorld
  2. 用容器hw_container,创建一个新镜像hw_image。 shell script docker container commit -a "zhaoyu" -m "hello world image" hw_container hw_image -a添加作者信息。
  3. 删除改变的容器。 shell script docker container rm -vf hw_container
  4. 使用新镜像创建容器,查看是否有 HelloWorld文件。 shell script docker container run --rm hw_image ls -l /HelloWorld

设置镜像的属性

当使用docker container commit创建向一个image创建一个新的layer时,如下内容会被包含在层中。

  • 所有的环境变量
  • 工作目录
  • 暴露的端口集合。
  • 卷的定义信息。
  • 容器的entrypoint。
  • 命令和参数。

定义容器的环境变量如下:

  1. 创建新容器,并设置环境变量。 shell script docker container run --name rich-image-example -e ENV_EXAMPLE1=Rich -e ENV_EXAMPLE2=Example busybox:latest
  2. 创建新镜像 rie shell script docker container commit rich-image-example rie
  3. 查看rie是否有设置的环境变量。 shell script docker container run --rm rie /bin/sh -c "echo \$ENV_EXAMPLE1 \$ENV_EXAMPLE2"

定义容器的entrypoint如下:

  1. 设置entrypoint,并设置默认的命令 shell script docker container run --name rich-image-example-2 --entrypoint "/bin/sh" rie -c "echo \$ENV_EXAMPLE1 \$ENV_EXAMPLE2"
  2. 更新镜像 rie shell script docker container commit rich-image-example-2 rie
  3. 使用镜像rie创建容器 shell script docker container run --rm rie

设置镜像的标签

创建rich-image-example容器
docker container run --name rich-image-example -e ENV_EXAMPLE1=Rich -e ENV_EXAMPLE2=Example busybox:latest
提交1.1版本镜像
docker container commit rich-image-example image-example:1.1
将1.1版本标记为latest版本
docker image tag image-example:1.1 image-example:latest
查看镜像版本
docker image ls

查看镜像的历史,可以看到在busybox:latest 018c9d7b792b 上面添加了一层layer d1ff73f209f9。 ```shell script docker image history image-example:latest

IMAGE CREATED CREATED BY SIZE COMMENT d1ff73f209f9 4 minutes ago sh 0B 018c9d7b792b 13 hours ago /bin/sh -c #(nop) CMD [“sh”] 0B

13 hours ago /bin/sh -c #(nop) ADD file:a7c0b75d086cc605f… 1.22MB ``` ### 删除名称或标签为none的镜像 ``` docker rmi -f `docker images | grep '' | awk '{print $3}'` 或 docker images|grep none|awk '{print $3}'|xargs docker rmi ```