Top 10 Docker Commands Every Developer Should Know
Maximize Efficiency and Productivity with These Docker Commands
Introduction
Docker has revolutionized the way developers build, ship, and run applications. Its lightweight, portable containers offer a consistent environment across different platforms, making development workflows smoother and more efficient.
However, harnessing the full power of Docker requires familiarity with a set of essential commands. This article covers those essential commands and being good developers we should know them.
🚀 Unlock Your Path to Success with the Ultimate Everything Bundle! 🚀
Introducing the Everything Bundle — your one-stop solution to mastering Java, Spring Boot, SQL, acing interviews, and securing certifications!
1. Docker Inspect
The docker inspect
command is used to display detailed information about Docker containers, images, volumes, or networks in JSON format. This information can include configuration details, state information, and more.
# To inspect a running container and get detailed information about its configuration and state:
docker inspect my_container
#To inspect a Docker image:
docker inspect my_image
#You can inspect multiple containers, images, volumes, or networks by providing their names or IDs:
docker inspect container1 container2
#To inspect a Docker volume:
docker inspect my_volume
2. Docker Commit
The docker commit
command is used to create a new image from the changes made to an existing container. This can be useful if you have made modifications to a container (e.g., installed software, or changed configuration files) and you want to save those changes as a new Docker image.
# Run a new container from the ubuntu image
docker run -it --name my_container ubuntu
# Inside the container, install nginx
apt-get update
apt-get install -y nginx
exit
# Commit the container's changes to a new image
docker commit -m "Installed nginx" -a "Your Name <your.email@example.com>" my_container my_new_image
# List Docker images to verify the new image is created
docker images
3. Docker Logs
The docker logs
command is used to fetch and display the logs of a running or stopped container. This command is essential for debugging and monitoring your containers.
# This will display all the logs from the container named my_container.
docker logs my_container
# This will display the logs and continue streaming new log entries.
docker logs -f my_container
# This will show logs with timestamps.
docker logs -t my_container
# This will show the last 10 lines of logs from the container.
docker logs --tail 10 my_container
# This will show logs from my_container since the specified date and time.
docker logs --since 2024-05-25T15:00:00 my_container
4. Docker Process Status (ps)
The docker ps
command is used to list the running containers in your Docker environment. This command provides information about the containers that are currently active.
# This will display a list of all running containers.
docker ps
# This will display all containers, including those that are stopped.
docker ps -a
# This will display only the IDs of running containers.
docker ps -q
# This will display containers that have exited.
docker ps -f "status=exited"
# This will display all exited containers that were created from the ubuntu image.
docker ps -a -f "status=exited" -f "ancestor=ubuntu"
# This will display the container ID, names, and status in a table format.
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
5. Docker Exec
The docker exec
command is used to execute a command inside a running Docker container. This command is helpful for tasks such as debugging, inspecting container environments, or running administrative commands within a container without the need to start a new shell session.
# This will start an interactive shell (bash) inside the my_container.
docker exec -it my_container bash
# This will execute the ls /app command inside the my_container.
docker exec my_container ls /app
# docker exec -d my_container python script.py
docker exec -d my_container python script.py
# This will execute the ls command inside my_container, with /app set as the working directory.
docker exec --workdir /app my_container ls
6. Docker Network
The docker network ls
command is used to list all the networks that are available in your Docker environment.
docker network ls
NETWORK ID NAME DRIVER SCOPE
9b1b2b7a1b2b bridge bridge local
9c7d54f84162 host host local
Docker creates several default networks when we install them. We can also create custom networks using the docker network create
.
To list networks with a specific driver, we can use a filter:
docker network ls --filter driver=bridge
7. Docker Remove
Remove Image
The docker rmi
command is used to remove one or more Docker images from your local machine. This command is useful for cleaning up unused or obsolete images to free up disk space.
# This will remove the image named my_image.
docker rmi my_image
# This will remove the image with the ID 7a86f8ffcb25.
docker rmi 7a86f8ffcb25
# This will force the removal of my_image, even if it is being used by stopped containers.
docker rmi -f my_image
# This will remove my_image1, my_image2, and my_image3.
docker rmi my_image1 my_image2 my_image3
Removing Dangling Images
Dangling images are layers that have no relationship to any tagged images and can be removed to free up space.
docker image prune
Remove Container
The docker rm
command is used to remove one or more Docker containers. This command is essential for cleaning up containers that are no longer needed.
# This will remove the container named my_container.
docker rm my_container
# This will remove the container with the ID 7a86f8ffcb25.
docker rm 7a86f8ffcb25
# This will force the removal of my_container, even if it is currently running.
docker rm -f my_container
# This will remove my_container1, my_container2, and my_container3.
docker rm my_container1 my_container2 my_container3
# This will remove my_container and any anonymous volumes associated with it.
docker rm -v my_container
# This will remove all stopped containers.
docker container prune
Usage in Cleanup Scripts
It is common to use the docker rm
command in scripts for cleaning up containers as part of a maintenance routine. For example:
# Stop and remove all running containers
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
# Remove all stopped containers
docker container prune -f
By using the docker rm
command effectively, you can manage and maintain your Docker environment, ensuring that unnecessary containers do not consume resources.
8. Docker Images
The docker images
command is used to list the Docker images that are stored locally on your machine. This command provides details about each image, including the repository name, tags, image ID, creation time, and size.
docker images
#This will display all images, including intermediate images that are usually hidden.
docker images -a
# This will display only the IDs of the images.
docker images -q
# This will display only the images in the ubuntu repository.
docker images --filter "reference=ubuntu"
# This will display only dangling images (images that are not tagged and not referenced by any container).
docker images --filter "dangling=true"
9. Docker Save
The docker save
command is used to save one or more Docker images to a tar archive file. This is useful for exporting Docker images from one environment and importing them into another, or for sharing images with others.
# This will save the my_image1 and my_image2 Docker images to a tar archive named my_images.tar.
docker save -o my_images.tar my_image1 my_image2
10. Docker Load
The docker load
command is used to load Docker images from a tar archive file. This command allows you to import Docker images that were previously saved using the docker save
command.
# This command will load Docker images from the my_images.tar tar archive file into the Docker daemon.
docker load -i my_images.tar
Conclusion
Mastering these Docker commands can significantly improve your development workflow. By efficiently managing containers, images, and networks, you can streamline your development process, improve collaboration, and ensure consistency across environments.
🚀 Docker & Kubernetes: The Practical Guide [2024 Edition] 🚀
Learn Docker, Docker Compose, Multi-Container Projects, Deployment, and all about Kubernetes from the ground up!
Bestseller | Rating: 4.7 out of 54.7(26,782 ratings) | 181,678 students