Common Docker Commands for Managing Containers
Docker is a powerful tool for containerizing applications, making it easier to build, deploy, and manage them. As a Docker expert and mentor, I'll guide you through the common Docker commands for managing containers.
Docker Container Lifecycle Commands
docker run
: This command is used to create and start a new container from a specified Docker image. For example, to run a Nginx web server container, you can use the following command:
docker run -d -p 80:80 --name my-nginx nginx:latest
This command will pull the latest Nginx image from the Docker Hub, create a new container, and start it in detached mode (-d
) while mapping port 80 on the host to port 80 in the container.
docker start
: This command is used to start an existing, stopped container. For example, to start themy-nginx
container:
docker start my-nginx
docker stop
: This command is used to stop a running container. For example, to stop themy-nginx
container:
docker stop my-nginx
docker restart
: This command is used to restart a container. For example, to restart themy-nginx
container:
docker restart my-nginx
docker rm
: This command is used to remove a container. For example, to remove themy-nginx
container:
docker rm my-nginx
docker pause
anddocker unpause
: These commands are used to pause and unpause a running container, respectively. For example, to pause themy-nginx
container:
docker pause my-nginx
And to unpause it:
docker unpause my-nginx
Docker Container Management Commands
docker ps
: This command is used to list all running containers. To list all containers, including stopped ones, you can use the-a
flag:
docker ps -a
docker inspect
: This command is used to inspect a container and retrieve detailed information about it. For example, to inspect themy-nginx
container:
docker inspect my-nginx
docker logs
: This command is used to view the logs of a container. For example, to view the logs of themy-nginx
container:
docker logs my-nginx
docker exec
: This command is used to execute a command inside a running container. For example, to open a shell inside themy-nginx
container:
docker exec -it my-nginx /bin/bash
docker stats
: This command is used to view real-time performance statistics for one or more containers. For example, to view the stats for all running containers:
docker stats
docker top
: This command is used to display the running processes of a container. For example, to view the running processes of themy-nginx
container:
docker top my-nginx
These are the most common Docker commands for managing containers. As a Docker expert, I hope this overview helps you understand the core concepts and commands for managing your containers effectively. Let me know if you have any further questions!