root@jimo-VirtualBox:/home/jimo# docker run -it ubuntu root@4a12c1ec8971:/#
安装vim
1
root@4a12c1ec8971:/# apt-get install -y vim
查看运行的实例: 我们需要那个随机的名字: eager_austin
1 2 3
root@jimo-VirtualBox:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a12c1ec8971 ubuntu "/bin/bash" About a minute ago Up About a minute eager_austin
root@jimo-VirtualBox:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-with-vi latest 96f59c2ae190 50 seconds ago 179MB ubuntu latest 452a96d81c30 3 weeks ago 79.6MB
root@jimo-VirtualBox:~/workplace# pwd /root/workplace root@jimo-VirtualBox:~/workplace# ls Dockerfile root@jimo-VirtualBox:~/workplace# cat Dockerfile FROM ubuntu RUN apt-get update && apt-get install -y vim root@jimo-VirtualBox:~/workplace# docker build -t ubuntu-with-vi-dockerfile . Sending build context to Docker daemon 2.048kB Step 1/2 : FROM ubuntu ---> 452a96d81c30 Step 2/2 : RUN apt-get update && apt-get install -y vim ---> Running in 9f736146b482 ...... ...... Removing intermediate container 9f736146b482 ---> 98dc6c72d0ed Successfully built 98dc6c72d0ed Successfully tagged ubuntu-with-vi-dockerfile:latest
查看镜像:
1 2 3 4 5
root@jimo-VirtualBox:~/workplace# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-with-vi-dockerfile latest 98dc6c72d0ed 2 minutes ago 179MB ubuntu-with-vi latest 96f59c2ae190 14 minutes ago 179MB ubuntu latest 452a96d81c30 3 weeks ago 79.6MB
从
1 2
Step 1/2 : FROM ubuntu ---> 452a96d81c30
可以看出第一层是ubuntu层,在之上构建了vim.
history命令显示了层次:
1 2 3 4 5 6 7 8 9
root@jimo-VirtualBox:~/workplace# docker history ubuntu-with-vi-dockerfile IMAGE CREATED CREATED BY SIZE COMMENT 98dc6c72d0ed 5 minutes ago /bin/sh -c apt-get update && apt-get install… 99.7MB 452a96d81c30 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 3 weeks ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 3 weeks ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$… 2.76kB <missing> 3 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B <missing> 3 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:81813d6023adb66b8… 79.6MB
root@jimo-VirtualBox:~/workplace# ls Dockerfile testfile root@jimo-VirtualBox:~/workplace# cat Dockerfile FROM ubuntu RUN apt-get update && apt-get install -y vim COPY testfile /
root@jimo-VirtualBox:~/workplace# docker build -t ubuntu-with-vi-dockerfile-2 . Sending build context to Docker daemon 2.56kB Step 1/3 : FROM ubuntu ---> 452a96d81c30 Step 2/3 : RUN apt-get update && apt-get install -y vim ---> Using cache ---> 98dc6c72d0ed Step 3/3 : COPY testfile / ---> 3281ec1c6e9e Successfully built 3281ec1c6e9e Successfully tagged ubuntu-with-vi-dockerfile-2:latest
root@jimo-VirtualBox:~/workplace# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu-with-vi-dockerfile-2 latest 3281ec1c6e9e 12 seconds ago 179MB ubuntu-with-vi-dockerfile latest 98dc6c72d0ed 15 minutes ago 179MB ubuntu-with-vi latest 96f59c2ae190 27 minutes ago 179MB ubuntu latest 452a96d81c30 3 weeks ago 79.6MB
可看到第2步使用了缓存:
1 2 3
Step 2/3 : RUN apt-get update && apt-get install -y vim ---> Using cache ---> 98dc6c72d0ed