Skip to main content
Master the essentials of Docker CLI to create, manage, and troubleshoot containers efficiently. This guide covers key commands, options, and best practices for container operations.

Table of Contents


Quick Syntax Reference

Use this general pattern for Docker commands:
Examples:

Command Syntax Styles

Docker supports both legacy and grouped syntax. We’ll use the grouped style throughout this guide.

Creating a Container

To create (but not start) a container:
Sample output:
On Linux, Docker stores images and container metadata under /var/lib/docker/:
Each container’s data lives in containers/<container-ID>/.

Listing Containers

Use docker container ls with options to filter the output:
Sample output:
Docker assigns a random human-readable name if you don’t provide one.

Starting a Container

Start an existing container by its ID or name:

Create & Start in One Step

Pull the image, create, and start the container:

Ephemeral Containers

Some images (like ubuntu) don’t run a persistent process:
Output:
Once the primary process exits, the container stops.

Interactive Shells

Keep STDIN open and allocate a pseudo-TTY with -it:
Inside the container:
On the host:
Shows:
Always place options (-i, -t, -d, --name) before the image name. Anything after the image name is interpreted as the container’s command.

Exiting Containers

Running exit inside an interactive shell stops the container:

Naming Containers

Assign a custom name at creation:
Output:
Rename an existing container:

Detached Mode

Run containers in the background with -d:
Sample output:
To reattach:
A unique ID prefix is sufficient if it’s unambiguous.

Watch Video