# 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
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1750313776476/feec0dcc-3516-4a2d-a7d7-1049f17ca0c1.png align="center")
    

# Docker Terminology

1. **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.
    
2. **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](https://blog.unknownbug.tech/docker-installation-guide)

# Docker Commands for Images

## 1\. Download an Image

Browse [Docker Hub](https://hub.docker.com/) to find the image you need:

```bash
docker pull <image_name>
```

**Examples:**

```bash
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

```bash
docker images
```

## 3\. Remove an Image

```bash
docker rmi <image_id_or_name>
```

**Example:**

```bash
docker rmi postgres:16
```

# Docker Commands for Containers

## 1\. Run a Container

```bash
docker run <image_name>
```

**Example:**

```bash
docker run hello-world
```

## 2\. Run with Interactive Terminal

```bash
docker run -it <image_name> /bin/bash
```

**Example:**

```bash
docker run -it ubuntu /bin/bash
```

## 3\. Run in Background with Port Mapping

```bash
docker run -d --name <custom_container_name> -p <host_port>:<container_port> <image_name>
```

**Examples:**

```bash
# 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

```bash
docker ps
```

## 5\. List All Containers (including stopped)

```bash
docker ps -a
```

## 6\. Container Lifecycle Management

```bash
# 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

```bash
docker exec -it <container_id_or_name> bash
```

**Examples:**

```bash
# 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

```bash
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

```bash
# 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

```bash
# 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

```bash
# 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
