Skip to content

1.02 Basic Docker Commands

Docker commands are used to run containers, list containers, stop containers, remove containers, manage images, download images, and execute commands inside running containers.

Goal

Learn the most common Docker CLI commands used in daily work and understand what happens when a container starts, stops, exits, or runs in the background.


Command Summary

Task Command
Run a container docker run <image>
List running containers docker ps
List all containers docker ps -a
Stop a container docker stop <container>
Remove a stopped container docker rm <container>
List images docker images
Remove image docker rmi <image>
Pull image only docker pull <image>
Execute command inside container docker exec <container> <command>
Run container in background docker run -d <image>
Attach to running container docker attach <container>

Tip

In Docker commands, you can use either the container name or the first few unique characters of the container ID.


docker run β€” Start a Container

The docker run command starts a container from an image.

docker run nginx

If the image is not available locally, Docker pulls it from Docker Hub first.

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Status: Downloaded newer image for nginx:latest

After the image is downloaded, Docker starts a container from it.

Note

Docker pulls the image only the first time. Later runs reuse the local image unless a newer image is explicitly pulled.


docker ps β€” List Running Containers

Use docker ps to list only currently running containers.

docker ps

Typical output shows:

Column Meaning
CONTAINER ID Unique container ID
IMAGE Image used to create the container
COMMAND Main command running inside the container
CREATED When the container was created
STATUS Current state
PORTS Published ports
NAMES Auto-generated or custom container name

Example

Docker automatically gives each container a random name, such as silly_sammet, unless you provide one using --name.


docker ps -a β€” List All Containers

Use docker ps -a to list both running and stopped containers.

docker ps -a

This is useful when a container exits immediately and does not appear in docker ps.

STATUS
Up 6 seconds
Exited (0) 3 seconds ago

Tip

If you ran a container but cannot see it with docker ps, check docker ps -a.


docker stop β€” Stop a Container

Use docker stop to stop a running container.

docker stop silly_sammet

You can also use the container ID.

docker stop 796b

After stopping, the container moves to an exited state.

docker ps -a

Note

docker stop stops the container, but it does not delete it.


docker rm β€” Remove a Container

Use docker rm to permanently remove a stopped container.

docker rm silly_sammet

Then verify:

docker ps -a

Warning

You cannot normally remove a running container unless you stop it first or force remove it.

docker stop <container>
docker rm <container>

docker images β€” List Images

Use docker images to list images stored locally on the Docker host.

docker images

Example output:

Repository Tag Size
nginx latest 109MB
redis latest 107MB
ubuntu latest 112MB
alpine latest 4.14MB

Note

Images remain on the host even after containers are stopped or removed.


docker rmi β€” Remove Images

Use docker rmi to remove an image.

docker rmi nginx

Warning

Before removing an image, delete all containers created from that image.

Correct cleanup order:

docker ps -a
docker stop <container>
docker rm <container>
docker rmi <image>

Production caution

Do not remove images from production nodes blindly. Existing running containers may continue running, but future restarts or scaling may fail if the image is no longer available locally and the registry is unreachable.


docker pull β€” Download an Image Only

Use docker pull to download an image without starting a container.

docker pull nginx
docker pull ubuntu

This is useful when you want the image ready before running containers.

Tip

In production, pre-pulling images can reduce startup delay during deployments.


Why Some Containers Exit Immediately

If you run Ubuntu like this:

docker run ubuntu

The container may exit immediately.

This happens because containers live only as long as their main process is running. Ubuntu is just a base OS image. It does not run a long-running service by default.

Note

A container is not meant to behave like a full virtual machine. It is meant to run a specific process.

To keep it alive temporarily, run a command:

docker run ubuntu sleep 5

This container runs for 5 seconds and then exits.

Another example:

docker run ubuntu sleep 100

Append a Command to docker run

You can override or append the command that runs inside the container.

docker run ubuntu sleep 5

Meaning:

Start an Ubuntu container
Run sleep 5 inside it
Exit when sleep completes

Example

This is useful for testing, debugging, or running short-lived jobs.


docker exec β€” Execute a Command in a Running Container

Use docker exec to run a command inside an already running container.

docker exec <container-name-or-id> cat /etc/hosts

Example:

docker ps -a
docker exec distracted_mcclintock cat /etc/hosts

Note

docker exec works only when the container is running.

For interactive shell access:

docker exec -it <container> sh

or, if Bash exists:

docker exec -it <container> bash

Tip

Minimal images like Alpine may not have Bash. Use sh instead.


Attached Mode vs Detached Mode

By default, docker run runs in attached mode.

docker run kodekloud/simple-webapp

You will see the application logs directly in the terminal.

* Running on http://0.0.0.0:8080/

In attached mode:

  • terminal stays connected to the container output
  • Ctrl + C usually stops the container
  • useful for testing and learning

docker run -d β€” Detached Mode

Use -d to run the container in the background.

docker run -d kodekloud/simple-webapp

Docker returns the container ID and gives the terminal back immediately.

docker ps

Production habit

Most long-running services are started in detached mode or managed by orchestration tools like Kubernetes.


docker attach β€” Attach Back to a Container

Use docker attach to connect back to a running container.

docker attach <container-id>

Example:

docker attach a043d

Warning

Be careful with Ctrl + C after attaching. It may stop the container depending on how the container process handles signals.


Useful Command Patterns

docker run nginx
docker run ubuntu sleep 10
docker run -d kodekloud/simple-webapp
docker ps
docker ps -a
docker images
docker stop <container>
docker rm <container>
docker rmi <image>
docker logs <container>
docker exec -it <container> sh
docker inspect <container>

Production Best Practices

Do

  • Use meaningful container names for local testing with --name.
  • Use pinned image tags instead of relying on latest.
  • Run long-running containers in detached mode.
  • Check logs before deleting failed containers.
  • Remove unused stopped containers and dangling images regularly.
  • Use docker pull before planned deployments if startup time matters.
  • Use docker exec for quick debugging, not permanent fixes.
  • Treat containers as disposable and recreate them from images.

Don't

  • Do not make manual changes inside a running container and expect them to persist.
  • Do not use containers as full virtual machines.
  • Do not remove images before checking dependent containers.
  • Do not store important data only inside a container filesystem.
  • Do not use random container names in scripts.
  • Do not run production containers without logs and restart strategy.
  • Do not depend on latest tag in production.

Common Troubleshooting

Container exits immediately

Check what command the image runs by default.

docker ps -a
docker logs <container>

Image not found locally

Docker will try to pull it from the registry.

docker pull <image>

Cannot remove image

A container still depends on it.

docker ps -a
docker rm <container>
docker rmi <image>

Cannot exec into container

The container may not be running.

docker ps
docker ps -a

Quick Cleanup Commands

Remove one stopped container:

docker rm <container>

Remove one image:

docker rmi <image>

Remove all stopped containers:

docker container prune

Remove unused images:

docker image prune

Warning

Prune commands delete resources. Use them carefully, especially on shared or production systems.


Quick Revision

docker run nginx
    Starts an nginx container. Pulls image if missing.

docker ps
    Shows running containers.

docker ps -a
    Shows running and stopped containers.

docker stop <container>
    Stops a running container.

docker rm <container>
    Removes a stopped container.

docker images
    Lists local images.

docker rmi <image>
    Removes an image.

docker pull <image>
    Downloads image without running it.

docker exec <container> <command>
    Runs a command inside a running container.

docker run -d <image>
    Runs container in background.

docker attach <container>
    Attaches terminal to a running container.

Quote

A container lives only as long as the main process inside it is running.


Exam / Interview Points

Explain docker run

docker run creates and starts a container from an image. If the image is not available locally, Docker pulls it from a registry first.

Explain docker ps vs docker ps -a

docker ps shows only running containers. docker ps -a shows all containers, including stopped or exited containers.

Explain image vs container

An image is a template or package. A container is a running instance of that image.

Explain why Ubuntu container exits

The Ubuntu image does not run a long-running process by default. Since the main process exits immediately, the container also exits.