Vibe Build

Notes on building servers, web applications, deployment, and operation.

  • Basic Git Commands

    Learn essential Git commands for repository setup, inspecting changes, staging, commits, branches, remotes, and safely undoing changes.

    Read more

Basic Docker Commands

Basic Docker Commands

Article Guide

Contents

Summary

When you first learn Docker commands, understanding the following workflow matters more than memorizing every option.

  1. Check the Docker Engine and client status.
  2. Download or build an image.
  3. Create a container using the image and its creation configuration.
  4. Inspect the container’s status, logs, and internal processes.
  5. Stop, start, or recreate the container as needed.
  6. Before deleting anything, check the target and which data will be preserved.

Start with the following commands.

Replace container-name in the commands below and service-name used later with actual names. The command examples are independent illustrations, not a sequence for creating multiple containers named nginx-web. For a sequential exercise, use the Basic Practice section later in this article.

docker version
docker info
docker image ls
docker container ls
docker container ls -a
docker container logs container-name
docker container inspect container-name
docker container stop container-name
docker container start container-name

Docker commands are easier to understand when you explicitly name the type of object being managed, such as an image, container, network, or volume.

docker object-type subcommand options target

Example:

docker container ls --all

You can also use a shorter alias.

docker ps -a

Both commands serve the same purpose, but this article introduces the full form first to make the structure easier for beginners to understand.

The Basic Structure of the Docker CLI

The docker Command and Docker Engine

The docker command you type in the terminal is the Docker CLI. It sends requests to the Docker daemon to manage images, containers, networks, and volumes.

Therefore, a command can fail even when the docker executable is installed if the daemon is not running or the current user cannot access the Docker socket.

For Docker Engine installation and permission setup, see Install Docker Engine (in Korean).

Basic Command Format

docker object-type subcommand options target

Consider the following command.

docker container logs --tail 50 nginx-web
Part Meaning
docker Run the Docker CLI
container Specify containers as the object type to manage
logs View container logs
--tail 50 Show only the last 50 lines
nginx-web Name of the target container

Full Forms and Short Aliases

Docker provides shorter aliases for some commands.

Full form Short form Purpose
docker container ls docker ps List running containers
docker container ls -a docker ps -a List all containers
docker image ls docker images List images
docker container rm docker rm Remove a container
docker image rm docker rmi Remove an image
docker container logs docker logs View container logs
docker container inspect docker inspect Inspect container details

In team documentation and automation, use your chosen form consistently within a document.

Checking Installation and Connectivity

docker version: Check Client and Server Versions

docker version

When the Docker CLI connects successfully to the daemon, it normally displays both Client and Server information.

If only Client information appears along with a Server connection error, check the following.

  • Whether the Docker service is running
  • The current user’s access to the Docker socket
  • The currently selected Docker context
  • Whether a remote Docker host is configured

To display just a short version string, use:

docker --version

docker info: Check Docker System Information

docker info

This shows general Docker system information, including container and image counts, the storage and logging drivers, the operating system, and Docker Root Dir.

If the output is long, you can view it as follows.

docker info | less

docker context ls: Check Docker Contexts

docker context ls

Check which Docker daemon the docker command targets. The context marked with an asterisk (*) is the currently selected target.

If you expect to manage a local server but see unfamiliar containers, check the context before running any deletion commands.

Managing Images

An image is a read-only template for creating containers. Downloading an image and creating a container from it are separate operations.

docker image ls: List Images

docker image ls

Short form:

docker images

The output generally includes the repository name, tag, image ID, creation time, and size.

Specify a repository name to list only its images.

docker image ls nginx

docker image pull: Download an Image

docker image pull nginx:alpine

If you omit the tag, Docker normally uses latest.

docker image pull nginx

For production documentation, specify a verified tag rather than relying on latest. The same tag can point to different image contents later.

docker image inspect: Inspect Image Details

docker image inspect nginx:alpine

This displays image configuration, environment variables, the default command, exposed ports, and layer information as JSON.

Use --format to print only a particular field.

docker image inspect nginx:alpine --format '{{json .Config.ExposedPorts}}'

The output depends on the environment and image version.

docker image history: Inspect Image Layers

docker image history nginx:alpine

Use this to examine the layers that make up an image and the size of each step.

docker image rm: Remove an Image

Before deleting an image, check whether any containers use it.

docker container ls -a --filter ancestor=nginx:alpine

After checking the target, remove the image.

docker image rm nginx:alpine

Removal may be refused if containers still use the image. Do not immediately resort to a force option without investigating the cause.

Creating and Running Containers

docker container run: Create and Start a Container

docker container run --name nginx-web -d nginx:alpine

This command performs the following operations.

  1. Downloads the image if it is not available locally.
  2. Creates a new container from the image and specified options.
  3. Starts the container’s default process.

Main options:

Option Purpose
--name nginx-web Set the container name
-d, --detach Run in the background
-p Publish a container port through a host port
--mount Attach a volume or bind mount
-e, --env Set a container environment variable
--restart Set the restart policy
--network Specify the network to join
--rm Automatically remove the container when it exits

Publishing Ports

This example publishes port 80 in the Nginx container through host port 8080.

docker container run --name nginx-web -d -p 8080:80 nginx:alpine

The order of the port-publishing fields is:

host-IP:host-port:container-port

If you omit the host IP, as in -p 8080:80, the port may be published on all network interfaces by default. Check the exposure instead of relying solely on a firewall.

Example restricted to access from the local host:

docker container run --name nginx-web -d -p 127.0.0.1:8080:80 nginx:alpine

Docker-published ports may be processed before UFW or firewalld rules. On a public server, review Docker’s firewall behavior and the DOCKER-USER chain together.

Passing Environment Variables

docker container run --name example-app -d -e APP_ENV=development example-image:tag

Do not enter real passwords, API keys, or tokens directly on the command line: command history and process information can expose them.

One-Off Containers

docker container run --rm alpine:latest echo "hello"

The container is automatically removed when its process exits. Consider whether --rm is appropriate when you need to investigate the result or preserve files created during execution.

Checking Container Status

docker container ls: List Running Containers

docker container ls

Short form:

docker ps

Check container IDs, images, commands, creation times, status, ports, and names.

docker container ls -a: List All Containers

This includes stopped containers.

docker container ls -a

Check this list before running deletion or cleanup commands.

Filtering the List

Example showing only containers that have exited:

docker container ls -a --filter status=exited

Find containers whose names contain nginx-web:

docker container ls -a --filter name=nginx-web

Available filter keys differ between commands. Check the current command’s --help output and the Docker CLI documentation.

Formatting Output

docker container ls --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'

--format uses Go template syntax. Quoting and escaping can differ between shells.

Starting, Stopping, and Restarting Containers

docker container stop: Request a Graceful Shutdown

docker container stop nginx-web

This sends a graceful shutdown signal to the container’s main process and waits for a timeout before forcing it to terminate if necessary.

docker container start: Start an Existing Stopped Container

docker container start nginx-web

docker start starts an existing stopped container with the same creation configuration. It does not change creation options such as published ports, bind mounts, the image, or environment variables.

docker container restart: Restart an Existing Process

docker container restart nginx-web

docker restart restarts the process in an existing container with the same creation configuration. If a configuration file is bind-mounted, the application inside the container may reread the changed file, but the container’s creation options themselves remain unchanged.

Changing Creation Options Requires Recreating the Container

Changing the following items generally requires removing the existing container and creating a new one with the updated configuration.

  • Published ports
  • Bind mounts and volume attachments
  • Container image
  • Environment variables
  • Various other creation options, except the restart policy

Inspect the existing container’s creation configuration first.

docker container inspect nginx-web

Do not remove a container before identifying its persistent data and how to recover it.

docker container kill: Terminate Immediately

docker container kill nginx-web

This sends a signal without waiting for a graceful shutdown and can affect applications that are writing data. For an ordinary shutdown, use docker container stop first.

Checking Logs and Internal State

docker container logs: View Logs

docker container logs nginx-web

Show only the last 50 lines:

docker container logs --tail 50 nginx-web

Follow new output in real time:

docker container logs --follow nginx-web

Stop following:

Ctrl + C

docker logs shows logs the container sends to standard output and standard error. Logs written only to files inside the container may not appear here.

docker container inspect: Inspect Detailed Configuration

docker container inspect nginx-web

This is useful for examining the following.

  • Current state and exit code
  • Image
  • Environment variables
  • Published ports
  • Mounts
  • Networks and IP addresses
  • Restart policy

Example showing a specific field:

docker container inspect nginx-web --format '{{.State.Status}}'

docker container top: List Running Processes

docker container top nginx-web

This lists the processes running inside the container.

docker container stats: Check Resource Usage

docker container stats

Monitor CPU, memory, network, and block I/O usage in real time.

Print once and exit:

docker container stats --no-stream

docker container port: Check Published Ports

docker container port nginx-web

Check which host addresses and ports a container’s ports are published on.

Running Commands Inside a Running Container

docker container exec

Run a command inside a running container.

docker container exec nginx-web nginx -t

Start an interactive shell:

docker container exec -it nginx-web sh

If Bash is installed in the image, you can use:

docker container exec -it container-name bash

A lightweight image may include only sh, without bash.

Exit the container shell:

exit

Files edited directly inside a container can disappear when the container is recreated. Manage settings that must persist through a Dockerfile, Compose file, volume, or bind mount.

Removing Containers

Check Before Removing

docker container ls -a --filter name=nginx-web
docker container inspect nginx-web

Determine whether the container’s data is stored in a volume, a bind mount, or the container’s writable layer.

Remove a Stopped Container

docker container rm nginx-web

Stop a running container first.

docker container stop nginx-web
docker container rm nginx-web

The force option, -f, terminates and removes a running container. Do not use it as the default for routine cleanup.

Volumes and Bind Mounts

A volume is storage managed by Docker. A bind mount connects an actual file or directory on the host. These are different concepts.

List Volumes

docker volume ls

Create a Volume

docker volume create app-data

Inspect Volume Details

docker volume inspect app-data

Run a Container with a Volume Attached

docker container run --name nginx-web -d \
  --mount type=volume,source=app-data,target=/usr/share/nginx/html \
  nginx:alpine

Attach a Bind Mount

docker container run --name nginx-web -d \
  --mount type=bind,source=/srv/nginx/html,target=/usr/share/nginx/html,readonly \
  -p 8080:80 \
  nginx:alpine

Use readonly to prevent the container from modifying the attached host files.

Docker recommends the --mount syntax for complex mount configurations. Whereas -v can create a directory automatically when the source host path does not exist, --mount type=bind reports an error if the source is missing, helping you catch mistyped paths.

Remove a Volume

First, check which containers use the volume and whether its data is backed up.

docker volume inspect app-data
docker container ls -a --filter volume=app-data

Remove only the volume you have checked.

docker volume rm app-data

Removing a volume can destroy persistent data. Do not remove a database volume without a verified backup and restore procedure.

Managing Networks

List Networks

docker network ls

Create a Network

docker network create app-network

Inspect Network Details

docker network inspect app-network

Connect a Container to a Network

Connect when creating the container:

docker container run --name nginx-web -d --network app-network nginx:alpine

Connect an existing running container:

docker network connect app-network nginx-web

Disconnect:

docker network disconnect app-network nginx-web

Remove a Network

docker network inspect app-network
docker network rm app-network

Removal is normally refused if containers are still connected.

Basic Docker Compose Commands

In Docker Compose, a service is a configuration unit declared in the Compose file, a container is a running instance created from a service definition, and a Compose project is the complete set managed under the same project name.

Run the following commands from the project directory containing compose.yaml.

Check the Configuration

docker compose config

Inspect the final Compose configuration, including variable interpolation. Check the output before sharing it because it may contain secrets.

Create and Start Services

docker compose up -d

This creates or recreates the necessary containers when an image or Compose service configuration has changed.

If the images also need to be rebuilt:

docker compose up -d --build

Check Status

docker compose ps

View Logs

docker compose logs --tail 100

Follow logs for a specific service:

docker compose logs --follow service-name

Run a Command in a Service Container

docker compose exec service-name sh

Stop and Remove a Compose Project

docker compose stop

This stops the containers without removing them.

docker compose start

This starts existing stopped containers with the same configuration.

docker compose down

This removes the Compose project’s containers and default network. Named volumes are not removed by default, but check the options and Compose file configuration.

docker compose down --volumes can delete the project’s volumes. Do not run it in an environment with persistent data before checking the backup and recovery procedure.

For a hands-on Compose exercise, see Docker Nginx Compose (in Korean).

Disk Usage and Cleanup Commands

docker system df: Check Docker Disk Usage

docker system df

Check the space used by images, containers, local volumes, and build cache, along with reclaimable space.

Detailed information:

docker system df -v

docker container prune: Remove All Stopped Containers

docker context show
docker container ls -a

Run this only after checking the deletion scope and which data will be preserved.

docker container prune

docker image prune

docker image prune

By default, this removes unused dangling images. Adding -a broadens the scope to images not used by any container, so it can remove images you will need to download again.

docker volume prune

docker volume ls
docker volume prune

Volumes may contain persistent data such as databases and uploaded files. Do not judge a volume unnecessary by its name alone: check the attached containers and whether you can back up and restore its data.

docker system prune

docker system prune

According to Docker’s documentation, this removes stopped containers, unused networks, dangling images, and unused build cache. -a broadens the scope to all unused images, while --volumes can include unused anonymous volumes.

Do not use this as a convenient first troubleshooting step on a production host. It is safer to inspect targets with docker system df -v and individual listing commands, then explicitly remove only the objects you no longer need.

Getting Help and Exploring Commands

Top-Level Help

docker --help

Help for an Object Type

docker container --help
docker image --help
docker network --help
docker volume --help
docker compose --help

Subcommand Help

docker container run --help
docker container logs --help
docker image ls --help

Supported options can change when the Docker CLI is updated. Check the installed version’s --help output alongside Docker’s official CLI documentation.

Basic Practice

This example assumes Docker Engine is installed and the current user can run docker commands. It was not executed in a separate environment while preparing this document.

Step 1: Check Docker Status

docker version
docker info

Step 2: Check for an Existing Target

Check whether a container with the same name already exists.

docker container ls -a --filter name=nginx-web

If a result appears, inspect the existing container’s purpose and configuration; do not run this exercise unchanged.

Step 3: Download the Nginx Image

docker image pull nginx:alpine

Check:

docker image ls nginx

Step 4: Run the Nginx Container

docker container run --name nginx-web -d -p 127.0.0.1:8080:80 nginx:alpine

This configuration publishes the port only on the host’s local address.

Step 5: Check Status and Ports

docker container ls --filter name=nginx-web
docker container port nginx-web

Step 6: Check the HTTP Response

Run this on the Docker host.

curl -I http://127.0.0.1:8080

The expected output includes an HTTP status line and Nginx response headers. Actual output can vary with the image version and environment.

Step 7: Check Logs

docker container logs --tail 50 nginx-web

Step 8: Check the Configuration Inside the Container

docker container exec nginx-web nginx -t

Step 9: Check Stopping and Starting

docker container stop nginx-web
docker container ls -a --filter name=nginx-web
docker container start nginx-web
docker container ls --filter name=nginx-web

Step 10: Remove the Practice Container

Double-check that this is the nginx-web container you created in this exercise.

docker container inspect nginx-web

Stop and remove the verified container.

docker container stop nginx-web
docker container rm nginx-web

Do not automatically remove the image, because it can be reused in other exercises.

Quick Reference for Common Commands

Command Purpose
docker version Check the client and Docker daemon versions
docker info View general Docker system information
docker context ls List Docker contexts and the current target
docker image ls List images
docker image pull Download an image
docker image inspect Inspect image details
docker container run Create and start a new container
docker container ls List running containers
docker container ls -a List all containers, including stopped ones
docker container logs View container logs
docker container inspect Inspect detailed container configuration
docker container exec Run a command inside a running container
docker container stats Check container resource usage
docker container stop Request a graceful container shutdown
docker container start Start an existing stopped container
docker container restart Restart an existing container’s process
docker container rm Remove a container
docker volume ls List volumes
docker network ls List networks
docker compose up -d Create and start Compose services
docker compose ps Check the status of a Compose project’s containers
docker compose logs View Compose service logs
docker compose down Remove a Compose project’s containers and default network
docker system df Check Docker disk usage

Key Takeaways

Before running Docker commands, check the current Docker context and target.

docker context show
docker container ls -a
docker image ls
docker volume ls
docker network ls

When a container has a problem, inspect its status, logs, and configuration before immediately removing or recreating it.

docker container inspect container-name
docker container logs --tail 100 container-name
docker container stats --no-stream container-name

docker start and docker restart do not change an existing container’s creation configuration. Changing creation options such as published ports, mounts, the image, or environment variables requires recreating the container.

Deletion commands directly affect data on the Docker host. In particular, check the scope and your ability to back up and restore data before removing volumes or using prune commands.

Precautions and Limitations

  • Docker CLI commands and options can vary with the Docker Engine, Docker Desktop, and Compose CLI versions.
  • Container names, image tags, ports, and paths are examples. Check that they do not conflict with existing objects in your environment.
  • docker container rm, docker image rm, docker volume rm, docker network rm, and the various prune commands perform deletions.
  • Whether volume or bind-mount data is automatically removed when a container is deleted depends on the options and configuration. Identify the data’s location first.
  • Volumes and bind mounts are not the same thing. Check the source type and mount configuration, and distinguish them clearly.
  • Published ports can affect firewall policy. Check external exposure and the DOCKER-USER chain together.
  • This article introduces the Docker CLI. It does not replace comprehensive coverage of Dockerfiles, image security, registry operations, the Compose Specification, or production deployment.

Next Steps

Previous article: Basic Linux Commands

Next article: Link to be added

References and Sources

Related Documents