CompTIA Linux+ (XK0-006)Services and User ManagementMedium
A system administrator is deploying a containerized application that requires persistent storage for its database. The database files should be stored in a host directory `/var/lib/myapp/db` to ensure data survives container recreation and can be easily backed up. The container's internal database path is `/data/db`. Which `docker run` command option correctly configures this persistent storage?
- A--mount type=volume,source=myapp-db,target=/data/db
- B-p /var/lib/myapp/db:/data/db
- C-v /var/lib/myapp/db:/data/db
- D--volume /data/db:/var/lib/myapp/db
Show answer & explanationAnswer & explanation
Correct answer: C. -v /var/lib/myapp/db:/data/db
The `-v` (or `--volume`) option in `docker run` is used to bind mount a host directory or a named volume into a container. The format for a bind mount is `host_path:container_path`. Therefore, `-v /var/lib/myapp/db:/data/db` correctly maps the host directory `/var/lib/myapp/db` to the container's `/data/db`.
Why the other options are wrong
- A. This syntax (`--mount type=volume,source=myapp-db,target=/data/db`) is for a named volume, not a bind mount to a specific host directory. `source=myapp-db` refers to a Docker-managed named volume.
- B. The `-p` option is used for port mapping (e.g., `host_port:container_port`), not for persistent storage.
- D. The order of `host_path:container_path` is reversed; it should be host path first, then container path.
Docker Bind Mounts
Docker bind mounts allow you to share a file or directory from the host machine directly into a container, providing persistent storage and host-side access.
- Uses `-v host_path:container_path` or `--mount type=bind,source=host_path,target=container_path`.
- Data persists on the host even if the container is removed.
- Host path must exist, or Docker will create a file if not a directory.
Memory trick: VOLUME Mounts: Host to Container - H:C!