Essential Docker Commands Every Beginner Should Learn


As in my previous article, I wrote steps for Docker installation and had a healthy discussion about sudo docker
vs just docker
. (Here I will write commands that start with docker
)
What is the Problem?
Imagine you're working on multiple projects:
Project 1 needs Node.js version 18.x
Project 2 needs Node.js v20.x
Some projects need an older MongoDB version
Others need the current MongoDB version
What would you do? Install and reinstall versions one by one? This approach is difficult because:
Every version must be configured accordingly
Installed versions may conflict with each other
Managing dependencies becomes a nightmare
What is the Solution?
Virtual machines are one solution, but they're resource-heavy and difficult to manage. You'd need to:
Spin VMs on and off
Connect services between different VMs
Connect VM services with your local machine
Docker changes everything! It can:
Spin environments up and down in seconds
Run services instantly
Connect all services with your local machine out of the box
Simply map ports to connect services
Docker Terminology
Docker Image: A blueprint or template containing the code and dependencies needed to run a service. Think of it as a program stored on your machine, not executing but ready to run.
Docker Container: A running instance of a Docker image. It's like a process - your program in execution. When you run a container, Docker creates a unique instance with its own environment.
Each container is unique by default through a container ID, even if you don't pass any variables. You can also pass environment variables to customize containers.
Prerequisites: If you don't have Docker installed, refer to this article: Docker installation guide
Docker Commands for Images
1. Download an Image
Browse Docker Hub to find the image you need:
docker pull <image_name>
Examples:
docker pull postgres # Latest postgres version
docker pull postgres:16 # Specific postgres version
docker pull node:18-alpine # Node.js 18 with Alpine Linux
2. List All Images
docker images
3. Remove an Image
docker rmi <image_id_or_name>
Example:
docker rmi postgres:16
Docker Commands for Containers
1. Run a Container
docker run <image_name>
Example:
docker run hello-world
2. Run with Interactive Terminal
docker run -it <image_name> /bin/bash
Example:
docker run -it ubuntu /bin/bash
3. Run in Background with Port Mapping
docker run -d --name <custom_container_name> -p <host_port>:<container_port> <image_name>
Examples:
# Redis server
docker run -d --name my-redis -p 6379:6379 redis
# PostgreSQL database
docker run -d --name my-postgres -p 5432:5432 -e POSTGRES_PASSWORD=mypassword postgres
# MongoDB
docker run -d --name my-mongo -p 27017:27017 mongo
Best Practice: Map host and container ports to the same number (e.g., 6379:6379) for consistency.
4. List Running Containers
docker ps
5. List All Containers (including stopped)
docker ps -a
6. Container Lifecycle Management
# Stop a running container
docker stop <container_id_or_name>
# Start a stopped container
docker start <container_id_or_name>
# Restart a container
docker restart <container_id_or_name>
# Remove a container (must be stopped first)
docker rm <container_id_or_name>
# Force remove a running container
docker rm -f <container_id_or_name>
Interacting with Active Containers
1. Execute Commands Inside a Container
docker exec -it <container_id_or_name> bash
Examples:
# Access PostgreSQL CLI
docker exec -it my-postgres psql -U postgres
# Access Redis CLI
docker exec -it my-redis redis-cli
# General bash access
docker exec -it my-container bash
2. View Container Logs
docker logs <container_id_or_name>
# Follow logs in real-time
docker logs -f <container_id_or_name>
# Show last 100 lines
docker logs --tail 100 <container_id_or_name>
3. Copy Files Between Container and Host
# Copy from container to host
docker cp <container_id>:/path/to/file ./local/path
# Copy from host to container
docker cp ./local/file <container_id>:/path/to/destination
Practical Examples
Setting up a Development Environment
# PostgreSQL database
docker run -d --name dev-postgres -p 5432:5432 -e POSTGRES_PASSWORD=dev123 postgres:16
# Redis cache
docker run -d --name dev-redis -p 6379:6379 redis:7-alpine
# MongoDB database
docker run -d --name dev-mongo -p 27017:27017 mongo:7
Cleanup Commands
# Stop all running containers
docker stop $(docker ps -q)
# Remove all stopped containers
docker rm $(docker ps -aq)
# Remove unused images
docker image prune
# Remove everything (use with caution!)
docker system prune -a
Next Steps
Now that you understand basic Docker commands, you're ready to:
Set up development environments quickly
Test different software versions without conflicts
Share consistent environments with your team
Subscribe to my newsletter
Read articles from Vineet Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by