DEV Community

Cover image for 🚀 Docker Commands You Must Know! 🐳
Shyamalendu Nayak
Shyamalendu Nayak

Posted on • Edited on

46 1 3 1

🚀 Docker Commands You Must Know! 🐳

Docker simplifies application deployment, making it easier to build, ship, and run applications anywhere. Whether you're a beginner or a seasoned developer, here are the key Docker commands you should have at your fingertips!

🚀 Why Do We Need Docker? 🐳

"It works on my machine" Problem 🛑
Developers often face issues where an application works on one system but fails on another due to different environments, dependencies, or OS configurations. Docker solves this by containerizing applications, ensuring consistency across different machines.

🔹Docker Basics

docker version        # Check Docker version  
docker info           # Get system-wide information  
docker help           # Get help on Docker commands  
Enter fullscreen mode Exit fullscreen mode

🔹Managing Containers

docker ps                      # List running containers  
docker ps -a                   # List all containers (including stopped ones)  
docker run <image>             # Run a container from an image  
docker start <container_id>    # Start a stopped container  
docker stop <container_id>     # Stop a running container  
docker restart <container_id>  # Restart a container  
docker rm <container_id>       # Remove a container  
docker logs <container_id>     # View container logs  

Enter fullscreen mode Exit fullscreen mode

🔹Working with Images

docker images             # List all images  
docker pull <image>       # Download an image from Docker Hub  
docker build -t <name> .  # Build an image from a Dockerfile  
docker rmi <image_id>     # Remove an image  

Enter fullscreen mode Exit fullscreen mode

🔹Executing Commands in Containers

docker exec -it <container_id> bash       # Access a running container  
docker inspect <container_id>             # Get detailed container info  
docker cp <container_id>:/path/to/file .  # Copy file from container  

Enter fullscreen mode Exit fullscreen mode

🔹Volumes & Storage

docker volume create <volume_name>  # Create a volume  
docker volume ls                    # List volumes  
docker volume rm <volume_name>      # Remove a volume  
docker run -v <volume_name>:/app <image> # Mount a volume to a container  

Enter fullscreen mode Exit fullscreen mode

🔹Networking in Docker

docker network ls                      # List networks  
docker network create <network_name>   # Create a custom network  
docker network connect <network_name> <container_id> # Connect a container to a network  
docker network inspect <network_name>  # Get details of a network  

Enter fullscreen mode Exit fullscreen mode

🔹Docker Compose

docker compose up -d  # Start services in the background  
docker compose down   # Stop and remove containers  
docker compose ps     # List services  
docker compose logs   # View logs of all services  

Enter fullscreen mode Exit fullscreen mode

🔹Clean Up Docker Resources

docker system prune -a          # Remove unused data, containers, and images  
docker volume prune             # Remove unused volumes  
docker network prune            # Remove unused networks  
docker rmi $(docker images -q)  # Remove all images  
docker rm $(docker ps -aq)      # Remove all containers  

Enter fullscreen mode Exit fullscreen mode

🔥 Mastering these commands will make your Docker experience much smoother! Which one do you use the most? Let me know in the comments! 👇

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (5)

Collapse
 
jake_killpack profile image
Jacob Killpack

Docker compose commands no longer use the - . Now they're docker compose ...

Collapse
 
shyam0118 profile image
Shyamalendu Nayak

Hello @jake_killpack , you are correct and I have updated the post, Thanks.

Collapse
 
bustedchain profile image
bustedchain

If you're using Docker Swarm and you don't mind setting up ssh key / ssh config locally for your worker nodes, and setup the authorized_keys for a docker user on the worker nodes (pretty easy), then knowing how to use the DOCKER_HOST=ssh://(node hostname here) variable to prefix docker commands and run them remotely is really powerful.

This includes being able to exec into a container on a worker node without anything other than ssh + docker. You definitely need to ssh to the node first to populate the known_hosts fully.

ssh isn't the only way of setting up, but it is the way I like.

Collapse
 
shitij_bhatnagar_b6d1be72 profile image
Shitij Bhatnagar

Thanks for sharing

Collapse
 
shitij_bhatnagar_b6d1be72 profile image
Shitij Bhatnagar

Good concise summary

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series