Skip to main content

Command Palette

Search for a command to run...

Mastering Essential Docker Commands: A Beginner’s Guide

Learn how to use docker ps, docker run, docker build, docker logs, and more with practical examples

Published
3 min readView as Markdown
Mastering Essential Docker Commands: A Beginner’s Guide
S

Welcome to my corner of the cloud, where ideas scale faster than servers and downtime is not an option! Here, I write about everything from spinning up VPCs to tearing down myths about the cloud. Whether you’re an engineer, a curious learner, or someone who just likes seeing words like 'serverless' and 'auto-scaling,' you’re in the right place. Consider this blog your high-availability zone for tips, tutorials, and tech thoughts—delivered with 99.99% uptime .

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.

docker ps

Output example:

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):

docker ps -a

🔹 2. Running Containers: docker run

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

Basic syntax:

docker run IMAGE_NAME

Example: Running an NGINX web server:

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 will show the NGINX welcome page.


🔹 3. Building Images: docker build

To create a Docker image from a Dockerfile, use:

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:

docker images

🔹 4. Viewing Logs: docker logs

When something goes wrong, docker logs helps you debug.

docker logs <container_id_or_name>

Example:

docker logs web_server

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

docker logs -f web_server

🔹 5. Executing Commands Inside a Container: docker exec

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

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:

docker stop <container_id_or_name>

To remove it completely:

docker rm <container_id_or_name>

👉 Remove all stopped containers in one go:

docker container prune

🔹 7. Removing Images

When you no longer need an image:

docker rmi <image_id_or_name>

👉 Clean up unused images:

docker image prune

🔹 8. Checking System Usage: docker system df

To check how much space Docker is using:

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

CommandDescriptionExample
docker psList running containersdocker ps -a (all containers)
docker runRun a new containerdocker run -d -p 8080:80 nginx
docker buildBuild image from Dockerfiledocker build -t my-app .
docker logsView container logsdocker logs -f web_server
docker execExecute commands in a containerdocker exec -it web_server /bin/bash
docker stopStop a running containerdocker stop web_server
docker rmRemove a containerdocker rm web_server
docker rmiRemove an imagedocker rmi my-app
docker system dfShow Docker disk usagedocker system df