Skip to main content

Docker Engine (Part 4)

Container Lifecycle, Interactive Shells, and Execution Modes

Updated 7 min readMarkdown
Part of seriesDemystifying Docker

Once Docker is installed and running on your machine, the Docker CLI (docker) becomes your primary interface for managing containers, images, networks, and volumes. However, beginners often hit common roadblocks early on—such as containers exiting immediately after starting, flags placed in the wrong order, or terminals getting locked in the foreground.

In this guide, we will break down modern vs. legacy Docker CLI syntax, walk through the full container lifecycle, and explain how interactive (-it) and detached (-d) execution modes work under the hood.

1. Modern vs. Legacy Docker CLI Syntax

Every Docker command follows a structured hierarchy:

docker <object> <subcommand> [options] [arguments]

As Docker grew, its top-level CLI became crowded with dozens of commands. To keep things organized, Docker introduced management commands that group operations by object type (container, image, network, volume).

You will encounter both the modern syntax and the legacy shorthand syntax across official docs and tutorials. Both work identically:

Operation

Modern Object Syntax (Recommended)

Legacy Shorthand Syntax

Run a container

docker container run <image>

docker run <image>

List containers

docker container ls

docker ps

Stop / Kill a container

docker container stop <id>

docker stop <id>

Attach to a container

docker container attach <id>

docker attach <id>

Build an image

docker image build .

docker build .

List images

docker image ls

docker images

2. Creating, Inspecting, and Starting Containers

Managing a container's lifecycle happens in two distinct phases: creating the container filesystem/metadata, and starting its primary process.

Step 1: Create a Container (create)

The create subcommand prepares a writable container layer from an image without actually starting it:

docker container create httpd
  • If httpd is not cached locally, Docker pulls it from Docker Hub first.
  • Docker outputs a 64-character unique Container ID (e.g., 11cb8f9a...) and stores all container configuration and metadata on Linux under /var/lib/docker/containers/<container-id>/.
  • Because you didn't specify a name, Docker automatically assigns a random two-word name (such as charming_wiles or quizzical_austin).

Step 2: List Containers (ls)

By default, docker container ls only displays currently running containers. To see containers in all states (Created, Running, Exited), append -a:

# List only running containers
docker container ls

# List all containers (running, created, and stopped)
docker container ls -a

Useful ls Flags:

  • -l — Show details of the most recently created container.
  • -q — Quiet mode; output only the 12-character short Container IDs (useful for scripting).
  • -aq — Output short IDs for all containers, regardless of state.

Step 3: Start a Created Container (start)

Start a created or stopped container using its name or unique ID prefix:

docker container start charming_wiles

(Tip: You never need to type all 64 characters of a Container ID. Typing the first 3 to 4 characters—such as 11cb—is enough, provided no other container on the host shares that prefix.)

3. Combining Create + Start (docker container run)

In day-to-day workflows, you rarely run create and start separately. The run subcommand combines both steps into one:

docker container run httpd

Why Does docker container run ubuntu Exit Immediately?

If you run a container using a base OS image like Ubuntu:

docker container run ubuntu

...and then check docker container ls, nothing is running. Checking docker container ls -a reveals the container is already in an Exited (0) state.

Why does this happen? Unlike Virtual Machines, containers are not meant to boot an entire operating system. A container is designed to run a single foreground process (a web server, API, database, or script). The container stays alive only as long as that primary process stays alive:

  1. Base OS images like ubuntu or alpine launch /bin/bash (or /bin/sh) as their default command.
  2. When started without an attached terminal, bash sees no standard input (STDIN), finishes immediately, and exits.
  3. Because the container's main process exited, the container stops immediately.

4. Running Interactive Containers (-i and -t)

To keep a shell-based container alive and interact with it, you must attach an interactive terminal session using two flags:

  • -i (--interactive): Keeps STDIN open so you can send keystrokes into the container.
  • -t (--tty): Allocates a pseudo-TTY (terminal driver) so the shell formats prompts and output properly.

Combine both flags as -it:

docker container run -it ubuntu

This drops you straight into the bash prompt inside the container (root@<container-id>:/#). As long as you remain inside that shell, docker container ls will show the container as Up. Typing exit terminates the bash process and stops the container.

Critical Syntax Rule: Flag Placement Matters!

Always place Docker flags (-it, -d, --name) before the image name:

# CORRECT: Flags come BEFORE the image name
docker container run -it ubuntu

# INCORRECT: Flags placed AFTER the image name are treated as arguments inside the container
docker container run ubuntu -it

Anything written after the image name overrides the image's default startup command (for example, docker container run ubuntu ls /etc runs ls /etc inside Ubuntu and exits).

5. Naming and Renaming Containers

Instead of relying on Docker's random names, assign meaningful names with --name:

docker container run -it --name webapp ubuntu

Each container name on a host must be unique. If you need to change a container's name later, use rename:

Bash

docker container rename webapp frontend-app

6. Foreground (Attached) vs. Background (Detached) Mode

When you run a long-lived service like an Apache web server (httpd) without extra flags:

docker container run httpd

Docker runs the container in attached (foreground) mode. Your terminal is locked to the container's STDOUT/STDERR logs, and pressing Ctrl + C sends an interrupt signal that kills the container process.

Running in the Background (-d)

To run a container in the background and immediately get your terminal prompt back, pass the -d (--detach) flag:

docker container run -d --name web-server httpd

Docker prints the full 64-character Container ID and returns control of your shell while httpd continues running in the background.

Reattaching to a Background Container (attach)

If you ever want to reconnect your terminal to a running detached container's live output stream, use attach:

docker container attach web-server
# Or using the first 4 characters of its Container ID:
docker container attach 11cb

(Note: Once reattached, pressing Ctrl + C will stop the container unless you detach cleanly using the escape sequence Ctrl + P followed by Ctrl + Q.)

Quick Command Cheat Sheet

Task

Command

Create without starting

docker container create <image>

Run interactively (shell)

docker container run -it --name <name> <image>

Run in background

docker container run -d --name <name> <image>

List running containers

docker container ls

List all containers

docker container ls -a

Start / Stop container

docker container start <name_or_id> / stop <name_or_id>

Rename container

docker container rename <old_name> <new_name>

Reattach to background container

docker container attach <name_or_id>

No comments yet. Be the first to comment.