Dockerfile Basics: Building Custom Images Quiz Quiz

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.

  1. Choosing the Correct Base Image

    In a Dockerfile, which instruction is used to specify the base image for your custom image, for example, using 'FROM ubuntu:20.04'?

    1. FROM
    2. START
    3. IMAGE
    4. BASE

    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.

  2. Maintaining Layer Efficiency

    Which approach helps minimize the number of layers and image size when installing packages and cleaning up files in a Dockerfile?

    1. Using a single RUN instruction joined with 'u0026u0026'
    2. Writing each command in a separate RUN instruction
    3. Using multiple ENTRYPOINT instructions
    4. Adding cleaning commands in a new FROM instruction

    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.

  3. Copying Files into the Image

    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'?

    1. PUSH
    2. TRANSPOSE
    3. EXPORT
    4. COPY

    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.

  4. Exposing Network Ports

    If your application listens on port 8000, which Dockerfile instruction is used to indicate this to the container runtime?

    1. EXPOSE 8000
    2. OPEN 8000
    3. PORT 8000
    4. LISTEN 8000

    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.

  5. Order of Instructions for Optimal Caching

    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?

    1. To allow multiple FROM instructions in sequence
    2. To make the most of Docker’s build cache and speed up rebuilds
    3. To reduce the number of required environment variables
    4. To expose more ports automatically

    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.