Image Building
https://docs.docker.com/get-started/09_image_best/
Security scanning
When you have built an image, it is a good practice to scan it for security vulnerabilities using the docker scan
command. Docker has partnered with Snyk to provide the vulnerability scanning service.
Note
You must be logged in to Docker Hub to scan your images. Run the command
docker scan --login
, and then scan your images usingdocker scan <image-name>
.
For example, to scan the getting-started
image you created earlier in the tutorial, you can just type
The scan uses a constantly updated database of vulnerabilities, so the output you see will vary as new vulnerabilities are discovered, but it might look something like this:
The output lists the type of vulnerability, a URL to learn more, and importantly which version of the relevant library fixes the vulnerability.
There are several other options, which you can read about in the docker scan documentation.
As well as scanning your newly built image on the command line, you can also configure Docker Hub to scan all newly pushed images automatically, and you can then see the results in both Docker Hub and Docker Desktop.
Image layering
Did you know that you can look at what makes up an image? Using the docker image history
command, you can see the command that was used to create each layer within an image.
Use the
docker image history
command to see the layers in thegetting-started
image you created earlier in the tutorial.You should get output that looks something like this (dates/IDs may be different).
Each of the lines represents a layer in the image. The display here shows the base at the bottom with the newest layer at the top. Using this, you can also quickly see the size of each layer, helping diagnose large images.
You’ll notice that several of the lines are truncated. If you add the
--no-trunc
flag, you’ll get the full output (yes... funny how you use a truncated flag to get untruncated output, huh?)
Layer caching
Now that you’ve seen the layering in action, there’s an important lesson to learn to help decrease build times for your container images.
Once a layer changes, all downstream layers have to be recreated as well
Let’s look at the Dockerfile we were using one more time...
Going back to the image history output, we see that each command in the Dockerfile becomes a new layer in the image. You might remember that when we made a change to the image, the yarn dependencies had to be reinstalled. Is there a way to fix this? It doesn’t make much sense to ship around the same dependencies every time we build, right?
To fix this, we need to restructure our Dockerfile to help support the caching of the dependencies. For Node-based applications, those dependencies are defined in the package.json
file. So, what if we copied only that file in first, install the dependencies, and then copy in everything else? Then, we only recreate the yarn dependencies if there was a change to the package.json
. Make sense?
Update the Dockerfile to copy in the
package.json
first, install dependencies, and then copy everything else in.Create a file named
.dockerignore
in the same folder as the Dockerfile with the following contents..dockerignore
files are an easy way to selectively copy only image relevant files. You can read more about this here. In this case, thenode_modules
folder should be omitted in the secondCOPY
step because otherwise, it would possibly overwrite files which were created by the command in theRUN
step. For further details on why this is recommended for Node.js applications and other best practices, have a look at their guide on Dockerizing a Node.js web app.Build a new image using
docker build
.You should see output like this...
You’ll see that all layers were rebuilt. Perfectly fine since we changed the Dockerfile quite a bit.
Now, make a change to the
src/static/index.html
file (like change the<title>
to say “The Awesome Todo App”).Build the Docker image now using
docker build -t getting-started .
again. This time, your output should look a little different.First off, you should notice that the build was MUCH faster! And, you’ll see that steps 1-4 all have
Using cache
. So, hooray! We’re using the build cache. Pushing and pulling this image and updates to it will be much faster as well. Hooray!
Multi-stage builds
While we’re not going to dive into it too much in this tutorial, multi-stage builds are an incredibly powerful tool to help use multiple stages to create an image. There are several advantages for them:
Separate build-time dependencies from runtime dependencies
Reduce overall image size by shipping only what your app needs to run
Maven/Tomcat example
When building Java-based applications, a JDK is needed to compile the source code to Java bytecode. However, that JDK isn’t needed in production. Also, you might be using tools like Maven or Gradle to help build the app. Those also aren’t needed in our final image. Multi-stage builds help.
In this example, we use one stage (called build
) to perform the actual Java build using Maven. In the second stage (starting at FROM tomcat
), we copy in files from the build
stage. The final image is only the last stage being created (which can be overridden using the --target
flag).
React example
When building React applications, we need a Node environment to compile the JS code (typically JSX), SASS stylesheets, and more into static HTML, JS, and CSS. If we aren’t doing server-side rendering, we don’t even need a Node environment for our production build. Why not ship the static resources in a static nginx container?
Here, we are using a node:12
image to perform the build (maximizing layer caching) and then copying the output into an nginx container. Cool, huh?
Last updated