Docker Container Management: Essential Commands for Beginners

Master the essential Docker container management commands in this guide. Learn how to create, start, stop, inspect, and clean up containers with practical examples. Whether you're deploying applications, debugging, or optimizing your setup, this post covers everything you need.

Docker Container Management: Essential Commands for Beginners
Photo by Jack Sharp / Unsplash

Docker containers are lightweight, standalone, and executable units that contain everything needed to run a piece of software. Managing these containers effectively is essential for developers and system administrators. In this guide, we’ll cover the most fundamental container management commands, breaking down their options and showing practical examples for real-world use.

1. Running a Container: docker run

The docker run command is the most frequently used command in Docker. It does two things: creates a new container and starts it.

Basic Syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Common Options:

  • -d: Run the container in detached mode (in the background).
  • -it: Interactive mode with a pseudo-TTY (useful for interactive shells).
  • --name: Assign a custom name to the container.
  • -p: Publish container ports to the host.
  • -v: Bind-mount a volume.

Examples:

Run with volume mounting:

docker run -v /host/path:/container/path my-app

Mount a host directory or file into the container.

Run with environment variables:

docker run -e ENV_VAR=value my-app

Pass environment variables to the container.

Run a container in detached mode:

docker run -d --name my-nginx -p 8080:80 nginx

This runs an NGINX web server in the background, exposing it on port 8080 of the host.

Run a container interactively:

docker run -it ubuntu bash

This starts an Ubuntu container and opens an interactive shell.

Common Scenarios:

  • Spinning up a web server (e.g., NGINX or Apache).
  • Testing a Linux-based environment with a lightweight container like ubuntu or alpine.
  • Launching databases like MySQL for local development.

2. Listing Containers: docker ps

This command lists the currently running containers.

Basic Syntax:

docker ps [OPTIONS]

Common Options:

  • -a: Show all containers, including stopped ones.
  • -q: Display only container IDs.
  • --filter: Filter the output based on criteria like name, status, or labels.

Examples:

Display only IDs:

docker ps -q

Filter containers by status:

docker ps --filter "status=exited"

List all containers:

docker ps -a

Common Scenarios:

  • Verifying that a containerized application is running.
  • Identifying the container ID for logs, stopping, or other operations.

3. Stopping a Running Container: docker stop

This command gracefully stops a running container by sending a SIGTERM signal, giving the container time to shut down.

Basic Syntax:

docker stop [OPTIONS] CONTAINER

Example:

docker stop my-nginx

Common Scenarios:

  • Replacing an outdated web application with a new image.
  • Temporarily halting a background service for maintenance.

4. Starting a Stopped Container: docker start

This command restarts a stopped container.

Basic Syntax:

docker start [OPTIONS] CONTAINER

Example:

docker start my-nginx

Common Scenarios:

  • Restarting a database that was stopped during system downtime.
  • Resuming work on a development environment.

5. Viewing Logs: docker logs

Use this command to see the logs generated by a container.

Basic Syntax:

docker logs [OPTIONS] CONTAINER

Common Options:

  • -f: Follow the logs in real-time.
  • --tail: Show only the last N lines of logs.

Examples:

View the last 10 lines:

docker logs --tail 10 my-nginx

Follow logs live:

docker logs -f my-nginx

View all logs:

docker logs my-nginx

Common Scenarios:

  • Checking error messages from a failed application.
  • Monitoring output from a containerized build process.
  • Following real-time logs for active web or API servers.

6. Removing Containers: docker rm

Delete stopped containers to free up disk space.

Basic Syntax:

docker rm [OPTIONS] CONTAINER [CONTAINER...]

Common Options:

  • -f: Force the removal of a running container.
  • --volumes: Remove volumes associated with the container.

Examples:

Force remove a running container:

docker rm -f my-nginx

Remove multiple containers at once:

docker rm container1 container2 container3

Remove a stopped container:

docker rm my-nginx

Common Scenarios:

  • Removing old containers after upgrading to a new application version.
  • Cleaning up temporary containers used for testing.

7. Inspecting Containers: docker inspect

This command gives detailed JSON-formatted information about a container, including its configuration, networking, and state.

Basic Syntax:

docker inspect CONTAINER

Examples:

Extract specific information using jq:

docker inspect my-nginx | jq '.[0].NetworkSettings.IPAddress'

Inspect a single container:

docker inspect my-nginx

Common Scenarios:

  • Debugging a container’s network connectivity.
  • Checking mounted volumes or bound ports.
  • Retrieving the IP address of a container for manual testing.

8. Executing Commands in a Running Container: docker exec

Run commands in a running container.

Basic Syntax:

docker exec [OPTIONS] CONTAINER COMMAND

Examples:

Start an interactive bash session:

docker exec -it my-nginx bash

Run a command in a container:

docker exec my-nginx ls /var/www/html

Common Scenarios:

  • Opening a shell to investigate runtime issues.
  • Running package installations or updates within the container.
  • Checking the state of files or services inside the container.

9. Viewing Resource Usage: docker stats

Monitor live resource usage for running containers.

Basic Syntax:

docker stats [CONTAINER...]

Examples:

Monitor specific containers:

docker stats container1 container2

Monitor all containers:

docker stats

Common Scenarios:

  • Checking memory or CPU usage of a containerized service.
  • Diagnosing containers causing system performance issues.

10. Bonus: Useful Docker Commands

Pause and Unpause a Container

Unpause:

docker unpause my-container

Pause:

docker pause my-container

Check Container Disk Usage:

docker system df

Export a Container to a Tarball:

docker export -o container_backup.tar my-container

Rename a Container:

docker rename old_name new_name

Conclusion

This guide serves as a foundational reference for Docker container management. From starting and stopping containers to inspecting logs and monitoring resources, mastering these commands is a critical step toward becoming proficient with Docker. As you grow more comfortable with these basics, explore advanced commands to further streamline your container workflows.

Read next

Optimizing Docker Image Size with Multi-Stage Builds

Large Docker images slow deployments and waste storage. Multi-stage builds solve this by separating build and runtime environments, keeping only what’s needed in the final image. Learn how to optimize your images for speed, security, and efficiency

Writing a Basic Dockerfile: A Comprehensive Guide

A Dockerfile is a text file that contains all the commands and instructions to assemble a Docker image. In essence, it is a blueprint for how Docker should build and configure the image, which will later be used to create a container.