Challenge your understanding of Dockerfile essentials and best practices in building custom container images. This quiz covers syntax, instruction order, and key concepts for effective Dockerfile creation and optimization.
In a Dockerfile, which instruction is used to specify the base image for your custom image, for example, using 'FROM ubuntu:20.04'?
Explanation: The FROM instruction sets the base image for all subsequent instructions in a Dockerfile, such as FROM ubuntu:20.04. BASE and IMAGE are not valid Dockerfile instructions, even though they may sound similar. START is also incorrect, as it does not exist in Dockerfile syntax. Using the correct FROM keyword ensures your custom image builds on the desired foundation.
Which approach helps minimize the number of layers and image size when installing packages and cleaning up files in a Dockerfile?
Explanation: Joining commands with 'u0026u0026' in a single RUN instruction reduces the number of image layers, helping to keep the final image size smaller and cleaner. Writing each command in a separate RUN instruction creates additional unnecessary layers. Multiple ENTRYPOINT instructions are not related to layer management and only the last one takes effect. Adding cleaning commands in a new FROM instruction would reset the build context, not clean up layers.
Which Dockerfile instruction should be used to move files from your build context host directory into the image’s filesystem, as in placing 'app.py' into '/app'?
Explanation: COPY is the correct instruction for transferring files and directories from the build context into the image. EXPORT and PUSH are invalid in this context and do not serve this functionality. TRANSPOSE is not a recognized Dockerfile instruction, and doesn't affect the filesystem. The use of COPY allows precise control over which local files are included in the built image.
If your application listens on port 8000, which Dockerfile instruction is used to indicate this to the container runtime?
Explanation: EXPOSE is the valid instruction to signal which ports should be made externally accessible, such as EXPOSE 8000. OPEN and PORT are not recognized Dockerfile instructions and would result in build errors. LISTEN is a typical application-level directive but has no effect in a Dockerfile. EXPOSE documents the intended communication port, assisting with container orchestration and linking.
Why should you place instructions that change less frequently (like installing system packages) near the top of your Dockerfile, and more frequently changed steps (like copying source code) later?
Explanation: Placing infrequently-changed instructions first allows Docker to use cached layers for those steps, making subsequent rebuilds faster if only later layers, like source code, change. This order does not reduce environment variables, which are set separately. Multiple FROM instructions are used for multi-stage builds, but not influenced by this order. Exposing more ports is unrelated to instruction placement, as port exposure requires explicit directives.