Bird
Raised Fist0
Dockerdevops~5 mins

Starting and stopping containers in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to run software inside containers and later stop them when not needed. Starting and stopping containers lets you control when your apps are running without changing their setup.
When you want to launch a web server container to test your website locally.
When you need to stop a database container to save system resources.
When you want to restart a container to apply new environment settings.
When you want to pause work and stop containers without deleting them.
When you want to check which containers are running before starting new ones.
Commands
This command starts a new container named 'my-nginx' running the nginx web server in detached mode, mapping port 8080 on your computer to port 80 in the container.
Terminal
docker run -d --name my-nginx -p 8080:80 nginx:1.23.3
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
→
-d - Run container in background (detached mode)
→
--name - Assign a custom name to the container
→
-p - Map host port to container port
This command lists all currently running containers so you can see that 'my-nginx' is active.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.23.3 "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp my-nginx
This command stops the running container named 'my-nginx' safely, allowing it to finish current tasks before shutting down.
Terminal
docker stop my-nginx
Expected OutputExpected
my-nginx
Running this again shows no running containers, confirming 'my-nginx' has stopped.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
This command restarts the stopped container named 'my-nginx' so it runs again with the same settings.
Terminal
docker start my-nginx
Expected OutputExpected
my-nginx
Check again to confirm 'my-nginx' is running after the restart.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx:1.23.3 "/docker-entrypoint.…" 2 minutes ago Up 2 seconds 0.0.0.0:8080->80/tcp my-nginx
Key Concept

If you remember nothing else from this pattern, remember: use 'docker run' to start new containers, 'docker stop' to stop them safely, and 'docker start' to restart stopped containers.

Common Mistakes
Trying to start a container with 'docker run' when it already exists.
This creates a new container instead of restarting the existing one, causing duplicates and confusion.
Use 'docker start <container-name>' to restart an existing stopped container.
Using 'docker stop' without checking running containers first.
You might try to stop a container that is not running, causing an error.
Run 'docker ps' to list running containers before stopping.
Not using the '-d' flag with 'docker run' when you want the container to run in the background.
The container runs in the foreground and blocks your terminal until stopped.
Add '-d' to run the container detached in the background.
Summary
Use 'docker run -d --name my-nginx -p 8080:80 nginx:1.23.3' to start a new container in the background.
Check running containers with 'docker ps' to see active containers.
Stop containers safely with 'docker stop my-nginx' and restart them with 'docker start my-nginx'.

Practice

(1/5)
1. Which command is used to start a container that has been stopped in Docker?
easy
A. docker run <container_name>
B. docker stop <container_name>
C. docker start <container_name>
D. docker restart <container_name>

Solution

  1. Step 1: Understand the purpose of docker start

    This command resumes a container that was previously stopped without creating a new one.
  2. Step 2: Differentiate from other commands

    docker run creates a new container, docker stop stops a running container, and docker restart stops then starts a container.
  3. Final Answer:

    docker start <container_name> -> Option C
  4. Quick Check:

    Start stopped container = docker start [OK]
Hint: Use 'docker start' to resume stopped containers fast [OK]
Common Mistakes:
  • Confusing 'docker run' with 'docker start'
  • Using 'docker stop' to start containers
  • Thinking 'docker restart' only starts containers
2. Which of the following is the correct syntax to stop a running Docker container named webapp?
easy
A. docker terminate webapp
B. docker kill webapp
C. docker pause webapp
D. docker stop webapp

Solution

  1. Step 1: Identify the command to safely stop a container

    docker stop sends a signal to gracefully stop the container.
  2. Step 2: Check other options

    docker kill forcefully stops, docker pause suspends, and docker terminate is invalid.
  3. Final Answer:

    docker stop webapp -> Option D
  4. Quick Check:

    Stop container safely = docker stop [OK]
Hint: Use 'docker stop' to safely end container processes [OK]
Common Mistakes:
  • Using 'docker kill' instead of 'docker stop'
  • Confusing 'docker pause' with stopping
  • Using non-existent commands like 'docker terminate'
3. What will be the output of the following commands if the container db is currently stopped?
docker start db
docker stop db
medium
A. Container 'db' starts, then stops successfully
B. Error: container 'db' not found
C. Container 'db' remains stopped
D. Container 'db' restarts automatically

Solution

  1. Step 1: Analyze docker start db

    This command starts the stopped container named 'db'.
  2. Step 2: Analyze docker stop db

    This command stops the running container 'db' safely.
  3. Final Answer:

    Container 'db' starts, then stops successfully -> Option A
  4. Quick Check:

    Start then stop container = success [OK]
Hint: Start then stop a container runs commands without error [OK]
Common Mistakes:
  • Assuming 'docker start' creates a new container
  • Expecting errors if container exists
  • Thinking container restarts automatically without command
4. You run docker start myapp but get the error: Error response from daemon: No such container: myapp. What is the most likely cause?
medium
A. Docker daemon is not running
B. The container 'myapp' does not exist
C. You need to use docker run instead
D. The container 'myapp' is already running

Solution

  1. Step 1: Understand the error message

    The error says 'No such container', meaning 'myapp' is not found in Docker's container list.
  2. Step 2: Check other options

    If it was running, 'docker start' would not error; 'docker run' creates new containers but error is about missing container; daemon not running causes different errors.
  3. Final Answer:

    The container 'myapp' does not exist -> Option B
  4. Quick Check:

    No such container error = container missing [OK]
Hint: Check container existence before starting [OK]
Common Mistakes:
  • Assuming container is running when error says missing
  • Trying 'docker run' without checking container list
  • Ignoring Docker daemon status
5. You want to stop a container named api-server but ensure it has 10 seconds to finish its work before stopping. Which command should you use?
hard
A. docker stop -t 10 api-server
B. docker kill -t 10 api-server
C. docker pause api-server
D. docker stop api-server -timeout 10

Solution

  1. Step 1: Understand the timeout option in docker stop

    The -t or --time flag sets seconds to wait before killing the container.
  2. Step 2: Evaluate the options

    docker kill does not support -t, docker pause only suspends, and -timeout is invalid syntax.
  3. Final Answer:

    docker stop -t 10 api-server -> Option A
  4. Quick Check:

    Stop with timeout = docker stop -t [OK]
Hint: Use 'docker stop -t seconds' to allow graceful shutdown [OK]
Common Mistakes:
  • Using 'docker kill' with timeout flag
  • Using invalid flags like '-timeout'
  • Confusing 'pause' with stopping
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