Docker has revolutionized how developers manage and deploy applications. With its ability to package applications into containers, Docker simplifies the development process, providing a consistent environment across different stages of development and production. For beginners, understanding Docker commands is essential for getting the most out of the platform.
In this blog, we’ll explore the most commonly used Docker commands and explain how to use them effectively.
1. Getting Started with Docker
Before diving into the commands, make sure you have Docker installed on your machine. You can download it from the official Docker website.
After installation, verify that Docker is running by executing:
docker --version
This command will display the installed version of Docker.
2. Basic Docker Commands
2.1 docker --help
To view the full list of Docker commands and options, use:
docker --help
This will provide a summary of commands you can use.
2.2 docker info
If you want to get detailed information about your Docker setup, including storage, containers, and network information, use:
docker info
2.3 docker version
This command displays information about the Docker client and server version:
docker version
3. Docker Image Commands
Docker images are the blueprints from which containers are created. Here are the basic commands to manage images.
3.1 docker pull
To download an image from Docker Hub (the default registry), use the docker pull
command:
docker pull <image_name>
For example:
docker pull ubuntu
This command will pull the latest Ubuntu image.
3.2 docker build
To build an image from a Dockerfile
, use the docker build
command:
docker build -t <image_name>:<tag> .
Example:
docker build -t myapp:v1 .
This command will build an image named myapp
with the tag v1
.
3.3 docker images
To list all the images on your system:
docker images
This will show details such as the repository name, tag, image ID, and size.
3.4 docker rmi
To remove an image from your local system:
docker rmi <image_name>
Example:
docker rmi ubuntu
This will remove the Ubuntu image from your machine.
4. Docker Container Commands
Containers are instances of Docker images that run applications. Here are the most useful commands for managing containers.
4.1 docker run
To create and start a container from an image, use the docker run
command:
docker run <image_name>
For example:
docker run ubuntu
This will run a container from the ubuntu
image.
You can add additional flags to docker run
:
-d
: Run the container in detached mode (in the background).-p
: Map a port on your machine to the container’s port.--name
: Name the container.
Example:
docker run -d -p 80:80 --name webserver nginx
This command runs an Nginx container in detached mode, mapping port 80 on the host to port 80 on the container.
4.2 docker ps
To list all the running containers, use:
docker ps
This will show information like the container ID, names, status, and port mappings.
4.3 docker ps -a
To list all containers (running or stopped), use:
docker ps -a
4.4 docker stop
To stop a running container:
docker stop <container_name_or_id>
Example:
docker stop webserver
4.5 docker start
To start a stopped container:
docker start <container_name_or_id>
Example:
docker start webserver
4.6 docker restart
To restart a container:
docker restart <container_name_or_id>
4.7 docker rm
To remove a container (make sure it’s stopped first):
docker rm <container_name_or_id>
Example:
docker rm webserver
4.8 docker exec
To run a command in a running container, use docker exec
:
docker exec -it <container_name_or_id> <command>
Example:
docker exec -it webserver bash
This command opens an interactive terminal inside the running webserver
container.
5. Docker Logs and Monitoring
5.1 docker logs
To view the logs of a container:
docker logs <container_name_or_id>
This is helpful for debugging and monitoring your containers.
5.2 docker stats
To view real-time statistics (CPU, memory usage, etc.) of running containers:
docker stats
6. Networking and Volumes
Docker allows you to manage networking and persistent storage with containers.
6.1 docker network ls
To list all the Docker networks:
docker network ls
6.2 docker volume ls
To list all Docker volumes (used for persistent storage):
docker volume ls
7. Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. You define your services, networks, and volumes in a docker-compose.yml
file, then run them together.
7.1 docker-compose up
To start the services defined in the docker-compose.yml
file:
docker-compose up
Use the -d
flag to run containers in detached mode:
docker-compose up -d
7.2 docker-compose down
To stop and remove containers, networks, and volumes created by docker-compose up
:
docker-compose down
8. Cleaning Up Docker
Over time, your system may accumulate unused images, containers, and volumes. Docker provides commands to clean up unused resources.
8.1 docker system prune
To remove unused containers, networks, and images:
docker system prune
8.2 docker container prune
To remove stopped containers:
docker container prune
8.3 docker image prune
To remove unused images:
docker image prune
8.4 docker volume prune
To remove unused volumes:
docker volume prune
Conclusion
Docker commands are essential for managing your containers, images, networks, and volumes. Whether you’re just starting out or you’re an experienced user, mastering these commands will help you get the most out of Docker and streamline your development workflows.
As you become more familiar with Docker, you’ll find that many tasks can be automated using docker-compose
and the various command-line flags available. Happy containerizing!
Let me know if you need further details or explanations on any of the commands!