What does running a Docker container in detached mode mean?
Running a Docker container in detached mode means the container runs in the background, allowing you to continue using the terminal without being attached to the container's output.
Click to reveal answer
beginner
Which Docker command option runs a container in detached mode?
The -d option in the docker run command runs the container in detached mode.
Click to reveal answer
beginner
How can you check running containers after starting one in detached mode?
Use the command docker ps to list all running containers, including those started in detached mode.
Click to reveal answer
intermediate
What is the difference between running a container in attached vs detached mode?
Attached mode shows the container's output in the terminal and keeps the terminal busy. Detached mode runs the container in the background, freeing the terminal for other commands.
Click to reveal answer
beginner
How do you stop a container running in detached mode?
You can stop a detached container using docker stop <container_id_or_name>.
Click to reveal answer
Which command runs a Docker container in detached mode?
Adocker run --rm nginx
Bdocker run -it nginx
Cdocker run -d nginx
Ddocker run --name nginx
✗ Incorrect
The -d option runs the container in detached mode, so docker run -d nginx is correct.
What does the docker ps command show?
AAll running containers
BAll stopped containers
CDocker images
DDocker volumes
✗ Incorrect
docker ps lists all currently running containers.
If you want to see the logs of a detached container, which command do you use?
Adocker exec <container_id>
Bdocker attach <container_id>
Cdocker stop <container_id>
Ddocker logs <container_id>
✗ Incorrect
Use docker logs <container_id> to view output from a detached container.
What happens if you run a container without -d option?
AContainer runs attached to terminal
BContainer runs in background
CContainer stops immediately
DContainer runs with no network
✗ Incorrect
Without -d, the container runs attached to the terminal, showing output and blocking the terminal.
How do you stop a container running in detached mode?
Adocker start <container_id>
Bdocker stop <container_id>
Cdocker run -d <container_id>
Ddocker rm <container_id>
✗ Incorrect
Use docker stop <container_id> to stop a running container, including those in detached mode.
Explain what detached mode is in Docker and why you might want to use it.
Think about running a program and still being able to use your computer for other things.
You got /3 concepts.
Describe the steps to run a container in detached mode and then check if it is running.
Start with the command to run, then how to verify.
You got /2 concepts.
Practice
(1/5)
1. What does running a Docker container with the -d option do?
easy
A. Runs the container in detached mode, in the background
B. Runs the container interactively with a terminal
C. Removes the container after it stops
D. Builds a new Docker image from a Dockerfile
Solution
Step 1: Understand the -d option meaning
The -d flag tells Docker to run the container in detached mode, meaning it runs in the background.
Step 2: Compare with other options
Other options like interactive mode (-it) keep the container attached to the terminal, which is different from detached mode.
Final Answer:
Runs the container in detached mode, in the background -> Option A
Quick Check:
Detached mode = background run [OK]
Hint: Remember: -d means detach and run in background [OK]
Common Mistakes:
Confusing -d with -it for interactive mode
Thinking -d removes the container
Mixing up build and run commands
2. Which of the following is the correct syntax to run a container named myapp in detached mode using the image nginx?
easy
A. docker run nginx -d --name myapp
B. docker run -d --name myapp nginx
C. docker run --detach myapp nginx
D. docker run --name myapp -it nginx
Solution
Step 1: Identify correct order and flags
The correct syntax places options before the image name. The -d flag enables detached mode, and --name myapp names the container.
Step 2: Validate each option
docker run -d --name myapp nginx correctly uses docker run -d --name myapp nginx. docker run --detach myapp nginx misplaces arguments, docker run nginx -d --name myapp places flags after image, and docker run --name myapp -it nginx uses interactive mode instead of detached.
Final Answer:
docker run -d --name myapp nginx -> Option B
Quick Check:
Options before image, -d for detached [OK]
Hint: Put all flags before the image name in docker run [OK]
Common Mistakes:
Placing flags after the image name
Using -it instead of -d for detached mode
Misusing --detach instead of -d
3. What will be the output of the command docker ps after running docker run -d nginx?
medium
A. Shows the logs of the nginx container
B. Shows no containers because detached mode hides them
C. Shows an error because -d is not valid with nginx
D. Shows the running nginx container with its container ID and status
Solution
Step 1: Understand docker run -d nginx
This command runs the nginx container in detached mode, so it runs in the background.
Step 2: Understand docker ps output
The docker ps command lists all running containers, including those running detached. It will show the nginx container with its ID, image, and status.
Final Answer:
Shows the running nginx container with its container ID and status -> Option D
Quick Check:
docker ps lists running containers including detached [OK]
Hint: docker ps always lists running containers, detached or not [OK]
Common Mistakes:
Thinking detached containers are hidden from docker ps
Confusing docker ps with docker logs
Assuming -d causes errors with nginx image
4. You ran docker run -d nginx but the container stopped immediately. What is the most likely reason?
medium
A. The -d flag is invalid and caused the container to stop
B. You forgot to add the -it flag for interactive mode
C. The nginx container exited because it has no foreground process
D. Docker daemon is not running
Solution
Step 1: Understand container lifecycle with detached mode
Detached mode runs containers in background, but if the main process exits immediately, the container stops.
Step 2: Analyze nginx container behavior
Some nginx images may exit if no foreground process runs. This causes the container to stop right after starting.
Final Answer:
The nginx container exited because it has no foreground process -> Option C
Quick Check:
Container stops if main process exits immediately [OK]
Hint: Detached mode won't keep container alive if main process ends [OK]
Common Mistakes:
Assuming -d flag causes errors
Confusing interactive mode with container lifecycle
Not checking if Docker daemon is running
5. You want to run a container in detached mode and later check its logs. Which sequence of commands is correct?
hard
A. docker run -d --name myapp nginx; docker logs myapp
B. docker run --name myapp nginx -d; docker stop myapp
C. docker run -it --name myapp nginx; docker logs myapp
D. docker run -d nginx; docker attach myapp
Solution
Step 1: Run container in detached mode with a name
Use docker run -d --name myapp nginx to start the container in background with a name for easy reference.
Step 2: Check logs of the running container
Use docker logs myapp to view the logs of the named container without attaching to it.
Final Answer:
docker run -d --name myapp nginx; docker logs myapp -> Option A
Quick Check:
Detached run + named container + logs command [OK]
Hint: Name containers to easily check logs after detached run [OK]
Common Mistakes:
Using -it instead of -d for detached mode
Trying to attach instead of using logs to view output
Placing -d after image name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.