Working with Docker CLI: Basic Commands and Concepts

Docker CLI Demystified: Basic Commands and Concepts with Code Examples

The Docker command-line interface (CLI) serves as your gateway to interacting with Docker. It empowers you to build, manage, run, and interact with containers and images – the building blocks of containerized applications. This guide equips you with a foundational understanding of essential Docker CLI commands and concepts, illustrated with practical code examples.

Essential Docker CLI Concepts:

  • Images: Think of them as blueprints for creating containers. Images encapsulate the application code, dependencies, and configuration needed to run your software.
  • Containers: These are lightweight, portable units created from Docker images. They provide isolated execution environments for your applications.
  • Dockerfile: A text file that defines the instructions for building a Docker image. It specifies the base image, installation steps, and final configuration of the image.

Basic Docker CLI Commands:

  1. docker pull: Retrieves a Docker image from a registry (like Docker Hub) and stores it locally on your system.

Code Snippet (Pulling an Image):

Bash
docker pull ubuntu:latest

This command pulls the official Ubuntu image (latest version) from Docker Hub.

  1. docker images: Lists all the Docker images currently available on your Docker host.

Code Snippet (Listing Images):

Bash
docker images

This command displays a list of image names, tags, and image IDs.

  1. docker run: Creates and runs a new container from a specified image.

Code Snippet (Running a Container):

Bash
docker run ubuntu:latest bash

This command creates and runs a container based on the Ubuntu image, launching a bash shell within the container.

  1. docker ps: Lists all the running containers on your Docker host.

Code Snippet (Listing Running Containers):

Bash
docker ps

This command displays information about the container IDs, images, names, ports, and statuses.

  1. docker stop: Stops a running container gracefully, allowing it to perform any necessary cleanup tasks before termination.

Code Snippet (Stopping a Container):

Bash
docker stop container_id

Replace container_id with the actual ID of the container you want to stop (obtainable from docker ps).

  1. docker rm: Removes a stopped container.

Code Snippet (Removing a Container):

Bash
docker rm container_id

This command permanently deletes the specified stopped container.

  1. docker build: Constructs a new Docker image based on the instructions defined in a Dockerfile.

Code Snippet (Building an Image from a Dockerfile):

Bash
docker build -t my-custom-image:latest .

This command builds an image named my-custom-image:latest from the Dockerfile located in the current directory (.).

  1. docker version: Displays the version of the Docker engine installed on your system.

Code Snippet (Checking Docker Version):

Bash
docker version

This command outputs information about the Docker daemon version, API version, and operating system version.

Additional Considerations:

  • Flags and Options: Most Docker commands accept various flags and options to customize their behavior. Use docker <command> --help to view available options for a specific command.
  • Interactive vs. Detached Mode: The docker run command can be used in interactive mode (default) or detached mode (using -d). Interactive mode allows you to interact with the container’s terminal, while detached mode is suitable for long-running background processes.
  • Managing Volumes: Docker volumes provide a persistent storage solution for your containers, allowing data to survive container restarts or deployments across different machines. The docker volume command helps manage volumes.

By grasping these fundamental Docker CLI commands and concepts, you’ll be well-equipped to navigate the world of containerized applications. The provided code examples offer a practical starting point for wielding the power of Docker at your command line!