Docker CLI Commands Essentials Quiz Quiz

Challenge your understanding of essential Docker CLI commands with these practical, real-world scenarios. This quiz is designed to help users strengthen their knowledge of image management, container operations, and networking fundamentals using Docker’s command-line interface.

  1. Listing Docker Containers

    Which Docker CLI command shows all containers, including those that are stopped, along with details like container ID and status?

    1. docker list all
    2. docker containers
    3. docker ps -l
    4. docker ps -a

    Explanation: The correct answer is 'docker ps -a', which lists all containers, both running and stopped, with comprehensive details. 'docker ps -l' only shows the most recently created container. 'docker containers' is not a valid Docker command, and 'docker list all' is an incorrect command syntax. Using the '-a' flag is essential for seeing stopped containers—which is useful for management and troubleshooting.

  2. Removing Docker Images

    Suppose you want to permanently delete an unused Docker image called 'myapp:v1' from your local system. Which command would you use?

    1. docker image remove myapp:v1
    2. docker rm myapp:v1
    3. docker del myapp:v1
    4. docker rmi myapp:v1

    Explanation: 'docker rmi myapp:v1' is used to remove a Docker image from the local system. 'docker rm' is meant for removing containers, not images. 'docker image remove myapp:v1' is close but not the standard syntax—'docker image rm' works but is less common than 'docker rmi'. 'docker del' is not a recognized Docker command. Removing unused images helps free up disk space.

  3. Running a Container in Detached Mode

    If you want to start an Nginx container in the background so your terminal remains available, which command correctly achieves this?

    1. docker run -d nginx
    2. docker up -d nginx
    3. docker run --background nginx
    4. docker start nginx -b

    Explanation: The correct answer is 'docker run -d nginx', where '-d' stands for detached mode, allowing the container to run in the background. 'docker start nginx -b' is invalid as the '-b' flag does not exist for this purpose. 'docker run --background nginx' is not a recognized option. 'docker up -d nginx' is an incorrect command as 'up' is not used with 'docker run'. Running containers in detached mode is helpful for persistent services.

  4. Viewing Container Logs

    You need to check the output and error logs for a container named 'web_server'. Which Docker CLI command displays these logs?

    1. docker logs web_server
    2. docker show logs web_server
    3. docker container-logs web_server
    4. docker get-logs web_server

    Explanation: 'docker logs web_server' retrieves and displays the logs for the specified container, making it easy to troubleshoot issues. 'docker get-logs web_server', 'docker show logs web_server', and 'docker container-logs web_server' are not valid Docker commands and will not provide the logs as intended. Knowing the correct logging command is crucial for diagnosing container problems.

  5. Attaching a Terminal to a Running Container

    If you want to open an interactive bash shell inside a running container called 'app', which Docker command should you use?

    1. docker exec -it app bash
    2. docker shell app -it
    3. docker attach app bash
    4. docker connect app bash

    Explanation: The correct syntax is 'docker exec -it app bash', where '-it' provides interactive access and 'bash' opens the shell inside the container. 'docker connect app bash' and 'docker shell app -it' are not valid Docker commands. Using 'docker attach app bash' is incorrect; 'attach' connects to the main process of the container but does not start a new shell. Using 'exec' with '-it' is required for accessing a new shell interactively.