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.
Which Docker CLI command shows all containers, including those that are stopped, along with details like container ID and status?
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.
Suppose you want to permanently delete an unused Docker image called 'myapp:v1' from your local system. Which command would you use?
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.
If you want to start an Nginx container in the background so your terminal remains available, which command correctly achieves this?
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.
You need to check the output and error logs for a container named 'web_server'. Which Docker CLI command displays these logs?
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.
If you want to open an interactive bash shell inside a running container called 'app', which Docker command should you use?
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.