Essential Docker Commands
Managing Containers
Containers are the building blocks of Docker. Here are commands to manage container lifecycle:
Run a new container:
docker run -d -p 80:80 nginxRuns a container using the
nginximage in detached mode, mapping port 80 of the host to port 80 of the container.Start and stop a container:
docker start my_container docker stop my_containerStarts or stops an existing container named
my_container.Restart a container:
docker restart my_containerPause and unpause container processes:
docker pause my_container docker unpause my_containerTemporarily halts all processes inside a container and resumes them.
Kill a container:
docker kill my_containerImmediately stops a running container by sending a
SIGKILLsignal.Remove a container:
docker rm my_containerDeletes a container named
my_container.
Managing Images
Images serve as the blueprint for containers. Here’s how to work with them:
Build an image:
docker build -t my_image .Creates a Docker image named
my_imageusing theDockerfilein the current directory.Pull an image:
docker pull ubuntuDownloads the latest Ubuntu image from Docker Hub.
Push an image:
docker push my_user/my_imageUploads the image
my_imageto a repository under the usernamemy_user.List and remove images:
docker images docker rmi my_imageDisplays all locally available images or removes an image.
Managing Networks
Docker networks facilitate communication between containers.
Create and remove networks:
docker network create my_network docker network rm my_networkConnect or disconnect a container to/from a network:
docker network connect my_network my_container docker network disconnect my_network my_containerList all networks:
docker network ls
Volume Management
Volumes are used to persist data between container restarts.
Create a volume:
docker volume create my_volumeList and remove volumes:
docker volume ls docker volume rm my_volume
Information and Logs
These commands provide insights into containers and system state:
List running or all containers:
docker ps docker ps -aFetch logs and inspect details:
docker logs my_container docker inspect my_containerMonitor resource usage:
docker stats my_container
Docker Compose
For managing multi-container applications, Docker Compose is invaluable:
Start and stop all services in a Compose file:
docker-compose up docker-compose down
System Commands
Get system-wide information or Docker version details:
Display Docker info:
docker infoCheck Docker version:
docker version
Miscellaneous Commands
Log in and out of Docker Hub:
docker login -u my_user -p my_password docker logoutSearch for images:
docker search nginx
Sajit Khadka
Sajit Khadka is a software developer and tech enthusiast with a passion for exploring coding challenges and sharing insights from his development journey.