This short article lists the most important commands you need to know for managing the lifecycle of a Docker container.
Starting a Docker container
This command is used to start a new container from a single image:
docker run
For example, the following will run a Node.js container in the foreground, and tell it to run the Bash shell:
docker run -it --name my_example node bash
The purpose of the –name flag is to assign a memorable name to the container.
Another example with port option:
docker container run -d --name web_latest -p 8080:8080 web:latest
Pausing a container
The command below will pause all processes within a container:
docker pause <container-id or container-name>
Restarting a container
You can restart a Docker container by executing the following:
docker restart <container-id or container-name>
Stopping a container
You can stop a running Docker container with this command:
docker stop <container-id or container-name>
Deleting a container
To remove a stopped (exited) container, run:
docker rm <container-id or container-name>
If you want to wipe out all running and stopped containers on your system at once without passing IDs or names, use the following command:
docker container rm $(docker container ls -aq) -f
It’s worth noting that the preceding command is dangerous and you should be very careful when typing it.
Final Words
We’ve walked through the most-used commands for working with Docker containers. If you’d like to explore more interesting stuff about this awesome technology, take a look at the following articles:
- Docker: How to Name or Rename a Container
- How to Delete Docker Images on your System
- How to Get the Size of a Docker Image
- Docker: How to List Running and Stopped Containers
You can also check out our Docker topic page for the latest tutorials, examples, tips, and tricks.