0
0
Dockerdevops~5 mins

Container states (created, running, paused, stopped) in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Containers are like small packages that hold your app and everything it needs. They can be in different states like created, running, paused, or stopped, which tells you what the container is doing right now.
When you want to start an app inside a container and check if it is running properly
When you need to pause a container temporarily without stopping it completely
When you want to stop a container to save resources but keep its data intact
When you create a container but want to start it later
When you want to see the current status of your containers to manage them
Commands
This command creates a container named 'example-container' from the nginx image but does not start it yet. It is in the 'created' state.
Terminal
docker create --name example-container nginx
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
--name - Assigns a custom name to the container for easy reference
This command starts the container named 'example-container', changing its state from 'created' to 'running'.
Terminal
docker start example-container
Expected OutputExpected
example-container
This command pauses the running container, freezing its processes temporarily without stopping it.
Terminal
docker pause example-container
Expected OutputExpected
No output (command runs silently)
This command resumes a paused container, returning it to the 'running' state.
Terminal
docker unpause example-container
Expected OutputExpected
No output (command runs silently)
This command stops the running container, changing its state to 'exited'. The container is not removed and can be started again.
Terminal
docker stop example-container
Expected OutputExpected
example-container
This command lists all containers including stopped ones, filtering by name 'example-container' and showing their current status.
Terminal
docker ps -a --filter "name=example-container" --format "{{.Names}} {{.Status}}"
Expected OutputExpected
example-container Exited (0) 5 seconds ago
-a - Show all containers, including stopped ones
--filter - Filter containers by name
--format - Customize the output format
Key Concept

If you remember nothing else from this pattern, remember: container states tell you exactly what your container is doing and help you control its lifecycle.

Common Mistakes
Trying to start a container that is already running
Docker will return an error because the container is already in the running state
Check the container state with 'docker ps' before starting it
Pausing a container that is not running
You can only pause containers that are running; otherwise, Docker will error
Ensure the container is running before pausing it
Stopping a container that is already stopped
Docker will return an error or no effect because the container is not running
Use 'docker ps -a' to check container status before stopping
Summary
Create a container with 'docker create' to prepare it without running.
Start the container with 'docker start' to run your app inside it.
Pause and unpause containers to temporarily freeze and resume processes.
Stop containers with 'docker stop' to end their running state but keep them for later use.
Use 'docker ps -a' with filters to check container states anytime.