Docker has revolutionized the way developers build, ship, and run applications. With its ability to encapsulate applications in lightweight, portable containers, Docker has become an essential tool in modern software development. To make the most of Docker, you need to be familiar with its core commands. In this post, we’ll explore the top 10 Docker commands every developer should know and how to use them effectively.
1. docker pull
The docker pull
command downloads images from Docker Hub or other container registries. This is often the first step in starting with Docker.
docker pull <image_name>
Example:
docker pull nginx
This command fetches the latest version of the Nginx image from Docker Hub.
2. docker run
The docker run
command creates and starts a container from a specified image. You can also pass options to run the container in detached mode or map ports.
docker run [OPTIONS] <image_name>
Example:
docker run -d -p 8080:80 nginx
This runs the Nginx container in detached mode and maps port 8080 of the host to port 80 of the container.
3. docker ps
To list all running containers, use the docker ps
command.
docker ps
Example:
docker ps
This displays details like container ID, image name, status, and ports in use.
4. docker stop
To stop a running container, use the docker stop
command followed by the container ID or name.
docker stop <container_id>
Example:
docker stop my-nginx
This stops the container named “my-nginx.”
5. docker rm
The docker rm
command removes stopped containers to free up system resources.
docker rm <container_id>
Example:
docker rm my-nginx
This removes the container named “my-nginx.”
6. docker rmi
To remove unused images from your system, use the docker rmi
command.
docker rmi <image_name>
Example:
docker rmi nginx
This deletes the Nginx image from your local Docker environment.
7. docker exec
The docker exec
command lets you execute commands inside a running container.
docker exec -it <container_id> <command>
Example:
docker exec -it my-nginx bash
This opens an interactive bash shell inside the “my-nginx” container.
8. docker logs
To view logs from a running or stopped container, use the docker logs
command.
docker logs <container_id>
Example:
docker logs my-nginx
This shows the logs generated by the “my-nginx” container.
9. docker build
The docker build
command creates a Docker image from a Dockerfile
.
docker build -t <image_name> .
Example:
docker build -t my-app .
This builds an image named “my-app” using the Dockerfile
in the current directory.
10. docker inspect
The docker inspect
command provides detailed information about a container or image.
docker inspect <container_id_or_image_name>
Example:
docker inspect my-nginx
This outputs JSON-formatted details about the “my-nginx” container.
Conclusion
Mastering these Docker commands will significantly enhance your productivity as a developer. Whether you’re debugging, deploying, or managing containers, these commands form the foundation of effective Docker usage.