Introduction
Are you a developer looking to work with Docker containers? The Docker CLI (Command Line Interface) is a powerful tool that allows you to interact with Docker and manage containers from the command line. In this article, we will explore 10 essential Docker CLI commands that every developer should know. These commands will help you work with Docker containers effectively and streamline your development workflows.
1. Docker run
The docker run
command is used to start a new container based on a Docker image. It pulls the image from the Docker registry if it is not already present locally and starts a new container based on that image. For example:
$ docker run -d -p 8080:80 nginx
This command starts a new container based on the nginx
image and maps port 8080 of the host machine to port 80 of the container.
2. Docker ps
The docker ps
command is used to list all running containers. It provides information such as the container ID, image used, and port mappings. For example:
$ docker ps
This command lists all the running containers on your system.
3. Docker stop
The docker stop
command is used to stop a running container. It gracefully stops the container by sending a SIGTERM signal. For example:
$ docker stop container_id
Replace container_id
with the actual ID of the container you want to stop.
4. Docker logs
The docker logs
command is used to view the logs of a container. It shows the output generated by the container, which is useful for debugging purposes. For example:
$ docker logs container_id
This command will display the logs of the specified container.
5. Docker exec
The docker exec
command is used to run a command inside a running container. It is helpful when you need to execute a one-time command inside a container. For example:
$ docker exec container_id ls
This command will list the contents of the root directory of the container.
6. Docker build
The docker build
command is used to build a Docker image from a Dockerfile. It reads the instructions defined in the Dockerfile and creates an image based on those instructions. For example:
$ docker build -t my_image .
This command builds a Docker image with the tag my_image
using the Dockerfile present in the current directory.
7. Docker pull
The docker pull
command is used to pull a Docker image from a Docker registry. It downloads the image to your local system so that it can be run as a container. For example:
$ docker pull ubuntu
This command downloads the ubuntu
image from the Docker registry.
8. Docker rm
The docker rm
command is used to remove a stopped or exited container. It permanently deletes the container and its associated resources. For example:
$ docker rm container_id
Replace container_id
with the ID of the container you want to remove.
9. Docker rmi
The docker rmi
command is used to remove a Docker image. It permanently deletes the image from your local system. For example:
$ docker rmi image_id
Replace image_id
with the ID of the image you want to remove.
10. Docker-compose up
The docker-compose up
command is used to start a Docker application defined in a docker-compose.yml
file. It will create and start all the services specified in the file. For example:
$ docker-compose up
This command starts all the services defined in the docker-compose.yml
file in the current directory.
Conclusion
In this article, we have explored 10 essential Docker CLI commands that every developer should know. These commands will help you work with Docker containers effectively and streamline your development workflows. By mastering these commands, you can become more productive and efficient in your Docker-based development projects.