Skip to main content

João Senger

Docker - Basic Commands

Table of Contents

Learn the main Docker commands we use practically every day.

# Introduction

Docker is currently the most widely used tool for container management. One thing that stands out when you start learning Docker is that its commands follow a logical pattern, and understanding that pattern makes container management much easier.

# Understanding the pattern

Almost all Docker commands follow the same pattern:

docker OBJECT ACTION [options]

Where:

Object is what we want to manage, for example: images, networks, volumes, and so on.

Action is the verb, what we want to do, for example: ls, rm, run, inspect, start, stop, etc.

Options are command helpers. For example, the command docker container ls lists running containers, but if we add the option (or flag) -a, the output will list all containers, including those that are stopped, like this: docker container ls -a.

# Container commands

Containers are running instances created from an image. In daily work, these are the most commonly used commands:

# List running containers
docker container ls

# List all containers (including stopped ones)
docker container ls -a

# Create and start a container
docker container run -d --name my-nginx -p 8080:80 nginx

# Stop a running container
docker container stop my-nginx

# Start a stopped container
docker container start my-nginx

# Remove a stopped container
docker container rm my-nginx

# Show technical details about a container
docker container inspect my-nginx

# Show container logs
docker container logs my-nginx

Very common equivalent shortcuts:

docker ps        # equivalent to docker container ls
docker rm <id>   # equivalent to docker container rm <id>

# Image commands

Images are the “templates” used to create containers. They follow the same pattern as well:

# List local images
docker image ls

# Pull an image from a registry
docker image pull nginx:latest

# Build an image from a Dockerfile
docker image build -t my-api:1.0 .

# Remove an image
docker image rm nginx:latest

# Show image layer history
docker image history nginx:latest

# Show technical details about an image
docker image inspect nginx:latest

Equivalent shortcuts:

docker images          # equivalent to docker image ls
docker rmi <image>     # equivalent to docker image rm <image>

# Volume and network commands

Volumes and networks are very important support resources for data persistence and communication between containers.

# Volumes
docker volume ls
docker volume create app-data
docker volume inspect app-data
docker volume rm app-data

# Networks
docker network ls
docker network create my-network
docker network inspect my-network
docker network rm my-network

# Cleanup commands

Over time, unused resources accumulate in your environment. These commands help with maintenance:

# Remove stopped containers
docker container prune

# Remove unused images
docker image prune

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune

# General cleanup (use with care)
docker system prune -a

# Practical daily workflow

A simple and common workflow:

  1. Check what is running: docker container ls
  2. Check local resources: docker image ls, docker volume ls, docker network ls
  3. Start a service: docker container run ...
  4. Validate logs and state: docker container logs ... and docker container inspect ...
  5. Stop and clean up when needed: docker container stop, docker container rm, docker * prune

# Conclusion

Once you understand the docker OBJECT ACTION [options] pattern, learning new commands becomes much easier. Instead of memorizing everything, you start reasoning about what you want to manage and which action you want to perform.

This pattern applies to almost the entire Docker ecosystem and speeds up day-to-day work.