The topic

Say first conclusion

  1. Meet all changes with constancy

    • Make the best usecache, using a relatively fixedbuildThe environment
    • buildBase image of oneself
  2. A totally

    • keepcontextClean:.dockerignore
    • Clearing the Image Environment

The references you need to know

  • docker storage driver: Docs.docker.com/storage/sto…
  • dockerfile best practices: Docs.docker.com/develop/dev…

Why optimize mirroring

  • What are the benefits of a small image: faster distribution, less storage, faster loading.
  • What are the problems with mirror bloat: too much storage, slower distribution and more wasted bandwidth.

Composition of mirror image

  • Overlook mirror: a stripped down version of the operating system.
  • Side view mirrorMade up of layerslayerStack and become

So here’s the question

  1. Is a mirror with fewer layers a good mirror?
  2. In the enterprise application, how to plan and buildMirroring and building in CI ?
  3. How do you quickly distribute these images when the cluster is large enough and has enough nodes?

Take the Docker build as an example

  • Dockerfile v1
# v1
FROM nginx:1.15-alpine

RUN echo "hello"

RUN echo "demo best practise"

ENTRYPOINT [ "/bin/sh" ]
Copy the code
  • Dockerfile v2
# v2
FROM nginx:1.15-alpine

RUN echo "hello"

RUN echo "demo best practise 02"

ENTRYPOINT [ "/bin/sh" ]
Copy the code

1st build

A new building

# docker build-t demo:0.0.1Sending build context to Docker Daemon 2.048kB Step 1/4: FROM nginx:1.15-alpine --> 9a2868cac230 Step 2/4: RUNecho "hello"
 ---> Running in d301b4b3ed55
hello
Removing intermediate container d301b4b3ed55
 ---> 6dd2a7773bbc
Step 3/4 : RUN echo "demo best practise"
 ---> Running in e3084037668e
demo best practise
Removing intermediate container e3084037668e
 ---> 4588ecf9837a
Step 4/4 : ENTRYPOINT [ "/bin/sh" ]
 ---> Running ind63f460347ff Removing intermediate container d63f460347ff ---> 77b52d828f21 Successfully built 77b52d828f21 Successfully Tagged demo: 0.0.1Copy the code

2nd build

Dockerfile is identical to 1st build, command only changes build tag from 0.0.1 to 0.0.2

# docker build-t demo:0.0.2Sending build context to Docker Daemon 4.096kB Step 1/4: FROM nginx:1.15-alpine --> 9a2868cac230 Step 2/4: RUNecho "hello"
 ---> Using cache
 ---> 6dd2a7773bbc
Step 3/4 : RUN echo "demo best practise"
 ---> Using cache
 ---> 4588ecf9837a
Step 4/4 : ENTRYPOINT [ "/bin/sh"] --> Using cache --> 77b52D828f21 Successfully built 77b52D828f21 Successfully tagged demo:0.0.2Copy the code

As you can see,

  1. Each layer uses the cache ( ---> Using cache), has not been rebuilt.
  2. We can get throughdocker image ls |grep demoSee,Demo: 0.0.1Demo: hundredsThe Layer hash is the same. So basically, these two images are the same image, even though they are both built.

3rd build

RUN echo “demo best practise” RUN echo “demo best practise 02”

Docker build-t demo:0.0.3. Sending build context to Docker daemon 4.608kB Step 1/4: FROM nginx:1.15-alpine --> Step 2/4: RUNecho "hello"
 ---> Using cache
 ---> 6dd2a7773bbc
Step 3/4 : RUN echo "demo best practise 02"
 ---> Running in c55f94e217bd
demo best practise 02
Removing intermediate container c55f94e217bd
 ---> 46992ea04f49
Step 4/4 : ENTRYPOINT [ "/bin/sh" ]
 ---> Running inf176830cf445 Removing intermediate container f176830cf445 ---> 2e2043b7f3cb Successfully built 2e2043b7f3cb Successfully Tagged demo: 0.0.3Copy the code

As you can see,

  1. The second layer is still usedcache
  2. But the third layer has already generated a new hash
  3. Although the operations at the fourth layer have not changed, the fourth layer itself has changed because the image at the top has changed.

Note: each layer depends on the previous one when building –> Running in F176830CF445.

4th build

Fourth build, this time using –no-cache without caching, simulating a build on another computer.

# docker build-t demo:0.0.4 --no-cacheSending build context to Docker Daemon 5.632kB Step 1/4: FROM nginx:1.15-alpine --> 9a2868cac230 Step 2/4: RUNecho "hello"
 ---> Running in 7ecbed95c4cd
hello
Removing intermediate container 7ecbed95c4cd
 ---> a1c998781f2e
Step 3/4 : RUN echo "demo best practise 02"
 ---> Running in e90dae9440c2
demo best practise 02
Removing intermediate container e90dae9440c2
 ---> 09bf3b4238b8
Step 4/4 : ENTRYPOINT [ "/bin/sh" ]
 ---> Running in2ec19670cb14 Removing intermediate container 2ec19670cb14 ---> 9a552fa08f73 Successfully built 9a552fa08f73 Successfully Tagged demo: 0.0.4Copy the code

As you can see,

  1. Although and3rd buildThe use ofDockerfileSame, but since there is no cache, each layer is rebuilt.
  2. althoughDemo: 0.0.3Demo: 0.0.4It’s functionally consistent. butThey have different layers, and basically, they’re different mirrors.