How to Start, Stop, and Restart Docker Containers

Developer using a laptop while managing Docker containers from the command line

Docker containers are easy to control once you understand the difference between creating, starting, stopping, and restarting them. This guide shows the commands you need to manage a container safely from the terminal.

Before you begin

Make sure Docker is installed and the Docker daemon is running. Check the installation with:

docker version

To see containers that are currently running, use:

docker ps

To include stopped containers, add the -a option:

docker ps -a

How to start a Docker container

Use docker start when the container already exists but is stopped. Replace container_name with the container name or ID:

docker start container_name

For example:

docker start my-nginx

Start several existing containers in one command:

docker start web database

Docker starts the containers in the background by default. Add -a to attach to the container’s output:

docker start -a my-nginx

How to stop a Docker container

Use docker stop to ask a running container to shut down gracefully:

docker stop container_name

Docker normally waits 10 seconds for the main process to exit. You can choose a different timeout:

docker stop --time 30 my-nginx

Stop multiple containers together:

docker stop web database

If a container will not stop normally, use docker kill as a last resort. It terminates the process immediately:

docker kill container_name

A forced termination can interrupt application shutdown or unfinished writes, so use it only when docker stop does not work.

How to restart a Docker container

The docker restart command stops and starts an existing container:

docker restart container_name

You can also set the shutdown timeout:

docker restart --time 30 my-nginx

Restarting a container does not create a new container and does not remove its volumes. It is useful after changing an application setting that is stored inside the container or after a temporary service failure.

How to pause and resume a container

Pausing freezes the processes in a running container without stopping them:

docker pause container_name

Resume the frozen processes with:

docker unpause container_name

Pause and resume are useful for short maintenance tasks. They are different from stop and start: a paused process remains in memory, while a stopped container exits and can later be started again.

How to check the container state

After every state change, confirm the result:

docker ps
docker ps -a

For a more detailed status, inspect the container:

docker inspect --format '{{.State.Status}}' container_name

Common states include running, exited, created, and paused. To check the published ports, run:

docker port container_name

To view recent output when a service does not start correctly:

docker logs --tail 100 container_name

What to do if the container does not start

First, check whether the container exists:

docker ps -a --filter name=container_name

If the status is exited, read its logs and inspect the exit code:

docker logs container_name
docker inspect --format '{{.State.ExitCode}}' container_name

If Docker reports that no such container exists, you need to create it before you can start it. For example:

docker run -d --name my-nginx -p 8080:80 nginx

If the name is already in use, choose a different name or remove the old container only after confirming that it is no longer needed.

Start, stop, and restart with Docker Compose

For applications defined in a compose.yaml or docker-compose.yml file, use Docker Compose commands from the project directory:

docker compose up -d
docker compose stop
docker compose start
docker compose restart

docker compose stop keeps the containers and their configuration. Use docker compose down only when you want to remove the Compose-created containers and network. Named volumes are retained unless you explicitly add --volumes.

Quick command reference

GoalCommand
List running containersdocker ps
List all containersdocker ps -a
Start a stopped containerdocker start NAME
Stop gracefullydocker stop NAME
Restart a containerdocker restart NAME
Pause processesdocker pause NAME
Resume processesdocker unpause NAME
Force terminationdocker kill NAME
View logsdocker logs NAME

Conclusion

Use docker start for an existing stopped container, docker stop for a graceful shutdown, and docker restart to perform both actions in sequence. Always verify the state with docker ps -a and inspect the logs if the service does not behave as expected.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top