📖 Scenario: You are managing a small web application running inside a Docker container. Sometimes the container needs to be restarted to apply updates or fix issues.
🎯 Goal: You will learn how to list running containers, identify a container by name, and restart it using Docker commands.
📋 What You'll Learn
List all running Docker containers
Identify a container by its name
Restart a specific Docker container by name
💡 Why This Matters
🌍 Real World
Restarting containers is a common task when updating applications or fixing issues without stopping the entire system.
💼 Career
DevOps engineers and system administrators often restart containers to deploy updates or recover from errors quickly.
Progress0 / 4 steps
1
List running Docker containers
Run the command docker ps to list all currently running Docker containers.
Docker
Hint
Use the docker ps command to see running containers.
2
Identify the container by name
Use the command docker ps --filter "name=webapp" to list running containers with the exact name webapp.
Docker
Hint
Use the --filter "name=webapp" option to find the container named webapp.
3
Restart the container named 'webapp'
Run the command docker restart webapp to restart the container named webapp.
Docker
Hint
Use docker restart webapp to restart the container.
4
Verify the container restarted
Run docker ps again to confirm the container webapp is running after the restart.
Docker
Hint
Run docker ps to see running containers and confirm webapp is listed.
Practice
(1/5)
1. What does the docker restart <container_name> command do?
easy
A. Only starts the specified container
B. Only stops the specified container
C. Deletes the specified container
D. Stops and then starts the specified container
Solution
Step 1: Understand the restart command
The docker restart command is designed to stop a running container and then start it again.
Step 2: Compare with other commands
Stopping only halts the container, starting only launches it, and deleting removes it. Restart does both stop and start.
Final Answer:
Stops and then starts the specified container -> Option D
Quick Check:
Restart = Stop + Start [OK]
Hint: Restart means stop then start container [OK]
Common Mistakes:
Thinking restart only stops or only starts
Confusing restart with remove or delete
Assuming restart creates a new container
2. Which of the following is the correct syntax to restart a container named webapp?
easy
A. docker restart -c webapp
B. docker restart webapp
C. docker restart --container webapp
D. docker restart container webapp
Solution
Step 1: Recall the basic restart syntax
The correct syntax is docker restart <container_name> without extra flags for container name.
Step 2: Evaluate the options
Options B, C, and D add invalid flags or keywords not recognized by Docker CLI for restart.
Hint: Use 'docker restart' plus container name only [OK]
Common Mistakes:
Adding unnecessary flags like -c or --container
Using extra keywords after restart
Confusing restart syntax with other commands
3. What will be the output of the command docker restart myapp if the container myapp is currently stopped?
medium
A. myapp
B. Error: Container is not running
C. Container myapp restarted successfully
D. No output
Solution
Step 1: Understand restart behavior on stopped container
If the container is stopped, docker restart will start it and print the container name.
Step 2: Check Docker CLI output format
The command outputs the container name on success, not a success message or error if stopped.
Final Answer:
myapp -> Option A
Quick Check:
Restart prints container name on success [OK]
Hint: Restart prints container name even if stopped [OK]
Common Mistakes:
Expecting an error if container is stopped
Expecting a success message instead of container name
Thinking restart does nothing if container stopped
4. You run docker restart 123abc but get an error: Error: No such container: 123abc. What is the likely cause?
medium
A. Docker daemon is not running
B. The container is already running
C. The container ID or name is incorrect or does not exist
D. You need to add --force flag to restart
Solution
Step 1: Analyze the error message
The error says no such container, meaning Docker cannot find a container with ID or name '123abc'.
Step 2: Check other options
Running container does not cause this error, Docker daemon not running causes different errors, and --force is not a valid restart flag.
Final Answer:
The container ID or name is incorrect or does not exist -> Option C
Quick Check:
No such container = wrong ID/name [OK]
Hint: Check container ID/name spelling if 'No such container' error [OK]
Common Mistakes:
Assuming container must be stopped to restart
Trying to force restart with invalid flags
Ignoring Docker daemon status
5. You want a container to automatically restart if it crashes or Docker restarts. Which command correctly starts a container named db with this behavior?
hard
A. docker run --restart=always db
B. docker run --restart=never db
C. docker run --restart=on-failure db
D. docker run --restart=unless-stopped db
Solution
Step 1: Understand restart policies
The --restart=always policy restarts the container whenever it stops, including crashes and Docker daemon restarts.
Step 2: Compare other policies
never disables restart, on-failure restarts only on errors, and unless-stopped restarts except when manually stopped.
Final Answer:
docker run --restart=always db -> Option A
Quick Check:
Always restart = auto restart on crash and daemon restart [OK]
Hint: Use --restart=always for auto restart on crash and reboot [OK]
Common Mistakes:
Using --restart=never which disables restart
Confusing on-failure with always
Not setting restart policy at container start
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.