Docker has become an essential tool in modern development, but with so many commands available, it’s easy to forget while work. While working on my recent project, I found myself repeatedly relying on a handful of commands that saved time, simplified debugging, and kept my containers under control. In this post, I’ll share the most useful Docker commands I used along with quick explanations and examples so you can keep them handy in your own workflow.
Inspecting images and containers
List all docker images: docker images
Displays all images available on your system. Great for checking what’s already downloaded.
Show Running Containers: docker ps
Lists all currently running containers. Perfect for a quick status check.
Show All Containers (Including Stopped): docker ps -a
Shows every container, even those that have exited. Useful when cleaning up old containers.
Cleaning Up
Remove an Image: docker rmi image_name:tag
Deletes an image from your system. Helps free up disk space.
Remove a Container: docker rm container_name/id
Removes a stopped container. Essential for keeping your environment tidy.
Tagging Images
Add a Tag to an Image: docker tag image-name:v1 image-name:v1
Creates a new tag for an existing image.
Example:
docker tag myapp:latest myapp:v1
This is handy when versioning your images or preparing them for a registry push.
Debugging & Logs
View Container Logs: docker logs container-name
Shows logs generated by a container. Useful for checking startup errors or application output.
Follow Logs in Real-Time: docker logs -f container-name
Keeps the log stream open so you can watch events as they happen.
💡Pro Tip: Use this while debugging a web app to see live error messages as requests come in.
Search keyword in logs: docker logs container-name | grep <keyword>
Search a specific keyword from the container logs
Working Inside Containers
Open a Shell Inside the Container: docker exec -it container-name bash
Starts an interactive shell session inside the container. Perfect for exploring the filesystem or running commands directly.
Check Environment Variables: docker exec -it container-name env
Prints all environment variables inside the container.
Search for a specific variable: docker exec -it container-name env | grep <keyword>
This is especially handy when debugging configuration issues.
Deep Dive
Inspect a Container: docker inspect container-name
Outputs JSON with everything from network settings to mounted volumes. Great for advanced troubleshooting.
Conclusion
These commands became my daily toolkit while working on my project. They helped me manage containers, debug issues, and keep my environment clean. If you’re starting with Docker, mastering these will make your life easier and save you countless hours.