Containers provide a simple way of packaging applications so they are portable and easily reproducible. However, to fully leverage Docker containers, developers should be familiar with some essential Docker container commands. Luckily, there is no need to know all commands because a small subset of commands covers most situations. This article serves as a cheat sheet for developers who are already familiar with Docker.
List Running Containers
Run the following command to display all the containers that are running at the moment:
docker ps
If you want to see stopped containers as well, you may use the `-a` option:
docker ps -a
The above command should be the first one you use when trying to check whether your container was created successfully.
Create a Container
You can create a container using the `docker run` command:
docker run nginx
If you want to launch a container in detached mode, you should add `-d` option:
docker run -d nginx
In addition, you can specify port publishing to make your application accessible via your computer:
docker run -d -p 8080:80 nginx
In the above command, 8080 port on your computer communicates with port 80 inside the container.
Stop and Restart Containers
You may stop a container by running:
docker stop container_name
You can then start it back without recreating:
docker start container_name
Alternatively, you may stop and start the container right away by executing:
docker restart container_name
In all commands above, you can use the container ID instead of its name.
Check Container Logs
Logs are very helpful when troubleshooting unexpected application behavior, and they may be viewed like this:
docker logs container_name
If you want to view logs as they appear, you may use the following command:
docker logs -f container_name
Press `Ctrl+C` when you finish watching the log messages.
Execute Commands in Container
The `docker exec` command allows you to execute arbitrary commands within the container:
docker exec container_name ls
To start an interactive terminal within the container, you may use the following command:
docker exec -it container_name sh
However, sometimes the container provides a Bash shell, and in this case, you can use it instead of the `sh`.
Inspect a Container
The `docker inspect` command returns detailed information about the container:
docker inspect container_name
Here you can find out such information as network configuration, volumes mounted, environment variables, and other information.
Delete a Container
To delete a stopped container, you may execute:
docker rm container_name
These essential Docker container commands provide a solid foundation for working with Docker containers. As you build and troubleshoot more containerized applications, they will quickly become part of your regular development workflow.
Learn More – Docker Foundations Professional Certificate
Ready to keep building your Docker skills? The Docker Foundations Professional Certificate on LinkedIn Learning provides a structured learning path designed to help you understand Docker concepts and apply them through hands-on practice. My course, Docker: Your First Project, is the second course in the certificate learning path and walks you through creating your first containerized application. It is a great next step for developers who want to move beyond individual commands and start using Docker in a real project.
