10 Essential Docker Commands Every Developer Should Know
Category: Software Install and Setup
Docker has revolutionized software development by making it easier to create, deploy, and manage applications in isolated environments. However, if you're new to Docker, mastering the right commands can be challenging. In this guide, we’ll cover 10 essential Docker commands that every developer should know.
1. Checking Docker Installation
Before using Docker, verify that it is installed correctly:
docker --version
If Docker is not installed, download it from the official Docker website.
2. Running a Docker Container
To create and run a container from an image, use:
docker run -d -p 8080:80 nginx
-d
: Runs the container in detached mode.-p 8080:80
: Maps port 8080 on the host to port 80 in the container.
3. Listing Running Containers
Check all running containers:
docker ps
To see all containers (including stopped ones):
docker ps -a
4. Stopping and Removing Containers
To stop a container, use its container ID:
docker stop container_id
To remove a stopped container:
docker rm container_id
5. Pulling Docker Images
Download images from Docker Hub:
docker pull ubuntu
List downloaded images:
docker images
6. Building a Docker Image
Create a Docker image from a Dockerfile:
docker build -t my-app .
-t my-app
: Assigns a tag to the image..
: Uses the current directory as the build context.
7. Running a Shell Inside a Container
To access a running container’s shell:
docker exec -it container_id /bin/bash
8. Viewing Logs of a Container
To see real-time logs:
docker logs -f container_id
9. Removing Docker Images
Delete an image:
docker rmi image_id
10. Cleaning Up Unused Resources
To free up space by removing unused containers, networks, and images:
docker system prune -a
Conclusion
These 10 essential Docker commands will help you manage containers efficiently. Whether you're pulling images, running containers, or cleaning up unused resources, mastering these commands will make you more productive. For more detailed commands, visit the official Docker CLI documentation.