.dockerignore文件

从构建环境的上下文中排除不必要的文件,文件名以换行符分割,可以使用*?通配符

镜像层

创建新镜像的主要方法是通过Dockerfiledocker build命令
Dockerfile中每个指令执行后都会产生一个新的镜像层
一个新的镜像层的建立都是,用上一层的镜像启动容器
中间使用过的容器都会被删除

举个🌰

  1. FROM busybox:latest
  2. RUN echo "This should work"
  3. RUN /bin/bash/ -c echo "This won't"

执行结果

  1. dropann@dropann:~/tedo$ docker build -t echotest .
  2. Sending build context to Docker daemon 2.048kB
  3. Step 1/3 : FROM busybox:latest
  4. ---> 388056c9a683 临时容器
  5. Step 2/3 : RUN echo "This should work"
  6. ---> Using cache
  7. ---> f33e1bfb4a99 由上个容器创建的镜像
  8. Step 3/3 : RUN /bin/bash/ -c echo "This wont" 上面有一步没有显示出来把临时容器388056c9a683删除
  9. ---> Running in ccbe791d7cd6 容器
  10. /bin/sh: /bin/bash/: not found
  11. The command '/bin/sh -c /bin/bash/ -c echo "This wont"' returned a non-zero code: 127

仍然可以利用最后成功生成的镜像层创建一个镜像来执行它 使用镜像的ID(f33e1bfb4a99),而不是最后容器的ID(ccbe791d7cd6)

image.png

结论和之前报错一样,busybox镜像中没有包含bash shell

常用命令

  1. 查看镜像
  2. docker images
  3. docker images -a #查看所有镜像
  4. docker images php #查看名为php镜像
  5. 搜索镜像
  6. docker search redis
  7. 拉取镜像
  8. docker pull redis:3.2
  9. 删除镜像
  10. docker rmi 9b9cb95443b5
  11. docker rmi -f 镜像名A:tag 镜像名B:tag #删除多个镜像
  12. docker rmi `docker images -q` #删除所有镜像
  13. 导出镜像
  14. docker save #例如:docker image save centos > docker-centos7.4.tar.gz
  15. 导入镜像
  16. docker load #例如:docker image load -i docker-centos7.4.tar.gz
  17. 查找相关镜像
  18. docker build -t 账号名/镜像名:标签
  19. docker search xx #例如:docker search redis
  20. docker search -s 30 redis #查找start大于30的redis镜像
  21. docker pull name:标签 #从查找的镜像中下载下来,标签默认是latest 例如:docker pull redis 等价于 docker pull redis:latest

使用Dockerfile构建镜像

exec和shell格式的对比

一些指令(RUN CMD ENTRYPOINT) 能够接受exec和shell两种格式,exec需要用到一个JSON数组
RUN ["apt-get", "install", "-y", "nginx"],第一个元素是可执行文件,其他是执行时所使用参数。
shell使用是自由形式的字符串,字符串会传给/bin/sh -c执行
exec适用于规避shell对字符串做出的错误解析,或者不支持shell平台(镜像没有包含/bin/sh)

Dockerfileke可用指令

  1. # Version: 0.0.1
  2. FROM ubuntu:14.04
  3. MAINTAINER test_user "test@example.com"
  4. RUN apt-get update && apt-get install -y nginx
  5. RUN echo 'Hi, i am your container' \
  6. >/usr/share/nginx/html/index.html
  7. EXPOSE 80

默认情况下RUN指令会在shell里使用命令包装器/bin/sh -c来执行,如果是一个不支持shell的平台或者不希望在shell中运行(避免shell字符串篡改),也可以使用exec格式的RUN指令

  1. RUN ["apt-get", "install", "-y", "nginx"]

缓存

满足条件
上一个指令能够在缓存中找得到
缓存中存在一个镜像层,而他的指令与你的指令一摸一样,父层也完全相同

构建缓存

忽略Dockerfile的构建缓存 --no-cache
使用Dockerfile的构建缓存

  1. FROM ubuntu:14.04
  2. MAINTAINER test_user "test@example.com"
  3. ENV REFRESHED_AT 2021-04-03
  4. RUN apt-get -qq update

镜像分发

镜像库和镜像命名方式

cd identidock

  1. 构建镜像时

docker build -t "identidock:0.1" .

  1. docker tag

docker tag "identidock:0.1" "username/identidock:0.1"

把镜像推送到dockerhub

docker login
docker push username/identidock:0.1

自动构建

图片.png
报错了
image.png
好像是因为路径问题
https://developer.aliyun.com/article/693777
图片.png