Skip to main content

Command Palette

Search for a command to run...

Mastering Essential Docker Commands: A Beginner-Friendly Guide

Published
4 min read
Mastering Essential Docker Commands: A Beginner-Friendly Guide
T

I'm a full-stack developer. Programming isn't just my job but also my hobby. I like developing seamless user experiences and working on server-side complexities

Docker has become a fundamental tool for modern software development. Whether you’re deploying applications, experimenting with microservices, or isolating development environments, containers make your workflow simpler, faster, and more consistent.

In this guide, we’ll walk through the core Docker commands you’ll use every day. By the end, you'll clearly understand how to run containers, manage images, and interact with processes inside containers—all before jumping into hands-on practice.

1. Running Containers with docker run

The most important command in Docker is:

docker run <image>

For example:

docker run nginx

This command:

  • Starts a new container based on the nginx image

  • Automatically pulls the image from Docker Hub if it isn’t already on your machine

  • Reuses the downloaded image in future runs

If the image exists locally, Docker skips the download step.

2. Listing Containers with docker ps

To see which containers are running:

docker ps

This shows:

  • Container ID

  • Image name

  • Status

  • Internally assigned random container name (e.g., silly_sammet)

To view all containers, including those that have exited:

docker ps -a

Example output might show a stopped container with status Exited.

3. Stopping Containers with docker stop

To stop a running container:

docker stop <container-id-or-name>

You can find the ID or name using docker ps.

After stopping, running docker ps again will show no active containers.
But docker ps -a will still show the stopped container.


4. Removing Containers with docker rm

Stopped containers still consume disk space. To delete one permanently:

docker rm <container-id-or-name>

After removing it, it will no longer appear in docker ps -a.


5. Working with Images: docker images and docker rmi

To list all images stored on your host:

docker images

This shows:

  • Image repository

  • Tags

  • Size

  • Available base images (e.g., ubuntu, alpine, nginx, redis)

If you want to remove an image you no longer need:

docker rmi <image-name>

Important:
You must first stop and remove all containers based on that image. Docker will not remove an image if a container is still using it.


6. Downloading Images Without Running Them: docker pull

If you want to download an image ahead of time (so you don’t wait during docker run):

docker pull ubuntu

This simply pulls the image to your local machine without creating a container.


7. Why Some Containers Exit Immediately

If you run:

docker run ubuntu

You’ll notice the container starts and exits instantly.

Why?

Because containers are not virtual machines—they do not run an operating system continuously. They only run a process.

If the image has no foreground process running, the container stops automatically.

Example: Running a short-lived command

docker run ubuntu sleep 5

This container will run sleep for 5 seconds, then exit.


8. Executing Commands Inside a Running Container: docker exec

If a container is running—say one that sleeps for 100 seconds—you can execute commands inside it:

docker exec <container> cat /etc/hosts

This prints the /etc/hosts file inside that container.


9. Running Containers in Attached vs. Detached Mode

Attached Mode (default)

When you run:

docker run -p 8080:80 dockersamples/static-site

You stay attached to the container’s output.
You cannot type anything else into the terminal until you press CTRL + C to stop the container.

Detached Mode

To run the container in the background:

docker run -d -p 8080:80 dockersamples/static-site

You immediately get your shell prompt back, and the container continues running in the background.

To see it:

docker ps

10. Attaching to a Running Container

If the container is running in the background, you can reattach to it:

docker attach <container-id>

And Docker accepts the first few characters of the ID, as long as they are unique.


Summary

You’ve now seen the essential Docker commands that form the foundation of container management:

  • docker run

  • docker ps / docker ps -a

  • docker stop

  • docker rm

  • docker images

  • docker rmi

  • docker pull

  • docker exec

  • docker attach

These tools give you full control over images, containers, processes, and background execution. With this knowledge, you’re ready to dive into hands-on practice and begin working with Docker confidently.