Bird
Raised Fist0
Dockerdevops~20 mins

Restarting containers in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Container Restart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of restarting a stopped container?
You have a Docker container named webapp that is currently stopped. You run the command docker restart webapp. What will be the output?
Docker
docker restart webapp
Awebapp restarted
BError: No such container: webapp
CContainer webapp restarted successfully
Dwebapp
Attempts:
2 left
💡 Hint
The restart command returns the container name if successful.
❓ Troubleshoot
intermediate
2:00remaining
Why does docker restart fail with 'No such container'?
You run docker restart mycontainer but get the error Error: No such container: mycontainer. What is the most likely cause?
Docker
docker restart mycontainer
AThe container name or ID is incorrect or does not exist
BDocker daemon is not running
CThe container is paused
DThe container is already running
Attempts:
2 left
💡 Hint
Check if the container name matches exactly.
❓ Configuration
advanced
2:00remaining
How to configure automatic restart of a container on failure?
You want a container to restart automatically only if it crashes (exits with error). Which Docker run option achieves this?
Docker
docker run --restart=? myimage
A--restart=unless-stopped
B--restart=on-failure
C--restart=no
D--restart=always
Attempts:
2 left
💡 Hint
This option restarts only on non-zero exit codes.
🔀 Workflow
advanced
2:00remaining
What is the correct sequence to restart all running containers?
You want to restart all currently running Docker containers with a single command sequence. Which option correctly does this?
Adocker restart $(docker ps -q)
Bdocker ps -q | xargs docker restart
Cdocker ps -a | grep Up | awk '{print $1}' | xargs docker restart
Ddocker restart $(docker ps -a -q)
Attempts:
2 left
💡 Hint
Use only running containers' IDs.
✅ Best Practice
expert
2:00remaining
What is the best practice to ensure a container restarts after Docker daemon reboot?
You want your container to restart automatically after the Docker service or host machine restarts. Which restart policy is best suited for this?
A--restart=on-failure
B--restart=unless-stopped
C--restart=always
D--restart=no
Attempts:
2 left
💡 Hint
This policy restarts containers except when explicitly stopped.

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

  1. Step 1: Understand the restart command

    The docker restart command is designed to stop a running container and then start it again.
  2. 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.
  3. Final Answer:

    Stops and then starts the specified container -> Option D
  4. 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

  1. Step 1: Recall the basic restart syntax

    The correct syntax is docker restart <container_name> without extra flags for container name.
  2. Step 2: Evaluate the options

    Options B, C, and D add invalid flags or keywords not recognized by Docker CLI for restart.
  3. Final Answer:

    docker restart webapp -> Option B
  4. Quick Check:

    Simple restart command = docker restart container_name [OK]
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

  1. Step 1: Understand restart behavior on stopped container

    If the container is stopped, docker restart will start it and print the container name.
  2. Step 2: Check Docker CLI output format

    The command outputs the container name on success, not a success message or error if stopped.
  3. Final Answer:

    myapp -> Option A
  4. 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

  1. Step 1: Analyze the error message

    The error says no such container, meaning Docker cannot find a container with ID or name '123abc'.
  2. 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.
  3. Final Answer:

    The container ID or name is incorrect or does not exist -> Option C
  4. 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

  1. Step 1: Understand restart policies

    The --restart=always policy restarts the container whenever it stops, including crashes and Docker daemon restarts.
  2. Step 2: Compare other policies

    never disables restart, on-failure restarts only on errors, and unless-stopped restarts except when manually stopped.
  3. Final Answer:

    docker run --restart=always db -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard