Explore key concepts in Docker Compose related to managing volumes and persistent storage. This quiz helps assess your understanding of data persistence, configuration, and common practices for storage in orchestrated container environments.
Which statement best describes how to define a named volume in a Docker Compose file for persisting a database’s data directory across container restarts?
Explanation: The correct way is to define the volume in the root-level 'volumes' section and reference it under the service to ensure data persists outside the container lifecycle. Mounting host directories directly works but is not the same as a named volume and can introduce portability issues. Placing the volume under the 'environment' key is incorrect; that section is for environment variables. Adding the volume after the image name is not valid syntax for specifying volumes.
Imagine you want changes made in a local project directory to reflect immediately within your running container. Which type of volume should you use in your Docker Compose configuration?
Explanation: Bind mounts synchronize local files with the container, so changes in your directory appear instantly inside the container, which is especially useful during development. Named volumes do not track changes in the source directory—they are managed by the orchestrator and independent of your file system. Network alias is for networking, not storage, and a config map is not a recognized compose storage type.
If you define a named volume called 'user-data' and mount it to '/data' in your service, what happens to the files in '/data' after the container is removed and recreated?
Explanation: Named volumes preserve their contents even after the associated container is removed or recreated, ensuring persistent storage. Volumes are not deleted unless explicitly removed, so the files are not automatically erased. There is no default behavior to move files to a backup folder, and contents are not replaced with files from the initial image unless the volume is brand new.
Which of the following volume entries in a Docker Compose 'volumes' list is syntactically incorrect and would cause an error?
Explanation: The correct syntax places the source (host path or volume name) before the colon, followed by the target path in the container and optional flags. '/container/data:data-volume' reverses this order, leading to an error. The other options all follow valid patterns: host path to container path, named volume to container path, or relative path to container path.
For storing and sharing persistent log data generated by multiple containers in a compose project, which strategy is most appropriate?
Explanation: A named volume ensures that multiple containers accessing the same path share persistent data, ideal for logs or shared files. Assigning unique bind mounts isolates data rather than sharing it. Adjusting environment variables only changes configuration, not where data is physically stored. Temporary storage drivers do not persist data, making them unsuitable for persistent log sharing.