Linux+ Container Operations with Docker Questions and Answers 1 — Questions and Answers
Question 1: A Linux administrator is performing maintenance on a Docker host and needs to reclaim disk space. Which command should they use to remove all unused images, including tagged images that are not associated with any container?
- docker system prune
- docker image prune -a (Correct answer)
- docker rmi $(docker images -q)
- docker image prune
Correct answer: docker image prune -a
The `docker image prune` command is used to remove unused images. By default, it only removes dangling images (those without a tag). The `-a` (or `--all`) flag extends this to remove all images that are not associated with at least one container, which is what the scenario requires.
Question 2: Which of the following commands will start a new container from the `ubuntu:latest` image, name it `my-test-container`, and ensure it runs in the background as a daemon?
- docker run -it --name my-test-container ubuntu:latest
- docker start my-test-container
- docker run -d --name my-test-container ubuntu:latest (Correct answer)
- docker create --name my-test-container ubuntu:latest
Correct answer: docker run -d --name my-test-container ubuntu:latest
The `docker run` command creates and starts a new container. The `-d` flag runs the container in detached mode (in the background), and the `--name` flag assigns a user-friendly name to it.
Question 3: A developer needs to deploy a database in a Docker container. It is critical that the database's data persists even if the container is removed. Which command correctly maps a Docker-managed named volume called `db_data` to the `/var/lib/mysql` directory inside the container?
- docker run -d --mount type=volume,source=db_data,target=/var/lib/mysql mysql (Correct answer)
- docker run -d --mount type=bind,source=/data,target=/var/lib/mysql mysql
- docker run -d --volume /data:/var/lib/mysql mysql
- docker run -d --tmpfs /var/lib/mysql mysql
Correct answer: docker run -d --mount type=volume,source=db_data,target=/var/lib/mysql mysql
To persist data using a Docker-managed named volume, the `--mount` flag is recommended for its clarity. The syntax `type=volume,source=db_data,target=/var/lib/mysql` explicitly creates (if not existing) a named volume called `db_data` and maps it to the `/var/lib/mysql` directory inside the container. This ensures data persists independently of the container's lifecycle.
Question 4: An administrator is troubleshooting a network connectivity issue with a running container named `api-gateway`. Which command would they use to view detailed configuration information about the container, including its assigned IP address, mounts, and port mappings?
- docker logs api-gateway
- docker ps --filter name=api-gateway
- docker stats api-gateway
- docker inspect api-gateway (Correct answer)
Correct answer: docker inspect api-gateway
The `docker inspect` command provides low-level, detailed information on Docker objects. When run against a container, it returns a JSON array containing its complete configuration, including network settings like the IP address, volume mounts, and environment variables.
Question 5: A developer has created a web application that listens on port 3000 inside its container. To make this application accessible on port 80 on the host machine, which `docker run` flag and argument should be used?
- -p 3000:80
- --expose 80
- -p 80:3000 (Correct answer)
- --net=host
Correct answer: -p 80:3000
The `-p` or `--publish` flag is used to map a host port to a container port. The format is `HOST_PORT:CONTAINER_PORT`. Therefore, to map port 80 on the host to port 3000 inside the container, the correct syntax is `-p 80:3000`.
Question 6: When building a Docker image with a Dockerfile, which instruction specifies the default command to be executed when a container is started, and is designed to be easily overridden by providing arguments to the `docker run` command?
- RUN
- CMD (Correct answer)
- ENTRYPOINT
- EXEC
Correct answer: CMD
The `CMD` instruction sets the default command and/or parameters for a container. These can be overridden simply by specifying a different command after the image name in a `docker run` command. `RUN` executes commands during the image build, and `ENTRYPOINT` configures the container to run as an executable, making it harder to override.
A Linux administrator is performing maintenance on a Docker host and needs to reclaim disk space.
Which command should they use to remove all unused images, including tagged images that are not associated with any container?