XK0-005 practice question 7 of 10

An operations engineer is planning to start a container running a PostgreSQL database. The engineer wants the container to start automatically at system startup, mount the /home/db directory as /var/lib/postgresql inside the container, and expose port 5432 to the OS. Which of the following commands should the engineer run to achieve this task?

Full set: 487 questions
Answer options
A: docker run -d --restart always -p 5432:5432 -v /home/db:/var/lib/postgresql postgresql:12 Correct answer
B: docker run -d --restart -p 5432:5432 --volume /var/lib/postgresql:/home/db postgresql:12
C: docker run -d --attach --platform 5432:5432 --volume /home/db:/var/lib/postgresql postgresql:12
D: docker run -d --init --restart --publish 5432:5432 --workdir /home/db:/var/lib/postgresql postgresql:12
Correct Answer:
A. docker run -d --restart always -p 5432:5432 -v /home/db:/var/lib/postgresql postgresql:12
Explanation

The docker run command initializes and starts a container from a specified image using provided configurations. The -d flag runs the container in detached mode in the background. The --restart always flag ensures the container restarts automatically upon system reboot or container exit. Port forwarding from the host OS to the container is specified via -p 5432:5432, binding host port 5432 to container port 5432. Volume mounting uses the syntax -v :, which correctly maps the host directory /home/db to /var/lib/postgresql inside the container. Combining these parameters allows the PostgreSQL service to run persistently, retain persistent database storage on the host, and accept network connections on host port 5432.

Why Incorrect

B. Option B reverses the host and container volume paths and lacks a valid restart policy argument required by the --restart flag.
C. Option C incorrectly uses --platform for port binding and combines --attach with -d, which conflict with detached operation.
D. Option D misuses --workdir instead of volume flags to bind directories and omits the mandatory restart policy for --restart.

References

Docker Documentation, "docker run reference", Section: "Restart policies (--restart)", "Bind mounts (-v, --mount)". https://docs.docker.com/engine/reference/commandline/run/

Red Hat Enterprise Linux 8, "Building, running, and managing containers", Section 3.2: Managing Container Lifecycle.

Scroll to Top