# Mastering Essential Docker Commands: A Beginner’s Guide

If you’ve just started exploring **Docker**, one of the most powerful tools in the DevOps ecosystem, the very first thing you’ll need to learn is its **basic commands**. These commands are your toolkit to create, run, manage, and debug containers efficiently.

In this article, we’ll walk through some of the most important Docker commands you’ll use in day-to-day development and operations.

---

## 🔹 1. Checking Running Containers: `docker ps`

The `docker ps` command lists all the running containers in your system.

```bash
docker ps
```

**Output example:**

```plaintext
CONTAINER ID   IMAGE         COMMAND               STATUS        PORTS       NAMES
d3f9a2f7c9e1   nginx:latest  "nginx -g 'daemon of…" Up 2 minutes 80/tcp      web_server
```

👉 Add `-a` to list **all containers** (including stopped ones):

```bash
docker ps -a
```

---

## 🔹 2. Running Containers: `docker run`

`docker run` is used to start a new container from an image.

**Basic syntax:**

```bash
docker run IMAGE_NAME
```

Example: Running an NGINX web server:

```bash
docker run -d -p 8080:80 nginx
```

* `-d`: Run in detached mode (background).
    
* `-p 8080:80`: Maps **host port 8080** to **container port 80**.
    

Now, visiting [`http://localhost:8080`](http://localhost:8080) will show the NGINX welcome page.

---

## 🔹 3. Building Images: `docker build`

To create a Docker image from a **Dockerfile**, use:

```bash
docker build -t my-app .
```

* `-t my-app`: Tags the image with the name `my-app`.
    
* `.`: Specifies the current directory as the build context.
    

👉 You can list built images with:

```bash
docker images
```

---

## 🔹 4. Viewing Logs: `docker logs`

When something goes wrong, `docker logs` helps you debug.

```bash
docker logs <container_id_or_name>
```

Example:

```bash
docker logs web_server
```

👉 Use `-f` to follow logs in real time (like `tail -f`):

```bash
docker logs -f web_server
```

---

## 🔹 5. Executing Commands Inside a Container: `docker exec`

Sometimes you need to open a shell inside a running container.

```bash
docker exec -it web_server /bin/bash
```

* `-it`: Interactive terminal.
    
* `/bin/bash`: Starts a bash shell.
    

👉 Once inside, you can navigate and inspect the container like a normal Linux environment.

---

## 🔹 6. Stopping and Removing Containers

To stop a container:

```bash
docker stop <container_id_or_name>
```

To remove it completely:

```bash
docker rm <container_id_or_name>
```

👉 Remove all stopped containers in one go:

```bash
docker container prune
```

---

## 🔹 7. Removing Images

When you no longer need an image:

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

👉 Clean up unused images:

```bash
docker image prune
```

---

## 🔹 8. Checking System Usage: `docker system df`

To check how much space Docker is using:

```bash
docker system df
```

---

## 📝 Conclusion

These commands (`docker ps`, `docker run`, `docker build`, `docker logs`, `docker exec`, and clean-up commands) are the **foundation** of working with Docker. Once you’re comfortable with them, you’ll be able to:

* Run and manage containers with ease.
    
* Debug applications quickly.
    
* Build and ship your own images.
    

Mastering these basics will prepare you for more advanced Docker workflows like **Docker Compose**, **multi-stage builds**, and **container orchestration with Kubernetes**.

---

## Docker Commands Cheatsheet

| Command | Description | Example |
| --- | --- | --- |
| `docker ps` | List running containers | `docker ps -a` (all containers) |
| `docker run` | Run a new container | `docker run -d -p 8080:80 nginx` |
| `docker build` | Build image from Dockerfile | `docker build -t my-app .` |
| `docker logs` | View container logs | `docker logs -f web_server` |
| `docker exec` | Execute commands in a container | `docker exec -it web_server /bin/bash` |
| `docker stop` | Stop a running container | `docker stop web_server` |
| `docker rm` | Remove a container | `docker rm web_server` |
| `docker rmi` | Remove an image | `docker rmi my-app` |
| `docker system df` | Show Docker disk usage | `docker system df` |
