DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Mastering Docker CLI: Essential Commands and Workflow for Container Management

Docker CLI (Command Line Interface): A Comprehensive Guide

The Docker Command Line Interface (CLI) is the primary way to interact with Docker. It provides a set of commands for managing Docker containers, images, networks, volumes, and other resources. The Docker CLI is essential for developers, system administrators, and DevOps engineers to automate and manage containerized applications.

In this guide, we will explore the key Docker CLI commands, how to use them, and best practices for working with Docker containers, images, and other components directly from the terminal.


1. Introduction to Docker CLI

The Docker CLI is the interface that allows users to interact with Docker from the command line. It provides a set of commands to manage Docker containers, images, networks, volumes, and more. The basic structure of a Docker command is:

docker <command> <subcommand> [options] [arguments]
Enter fullscreen mode Exit fullscreen mode

For example:

docker run -it ubuntu
Enter fullscreen mode Exit fullscreen mode

In this command:

  • docker is the Docker CLI command.
  • run is the subcommand that tells Docker to create and start a container.
  • -it is an option that combines -i (interactive) and -t (allocate a terminal).
  • ubuntu is the image to run.

2. Common Docker CLI Commands

Here are some of the most commonly used Docker CLI commands and their functions:

a. docker run

The docker run command is used to create and start a container from a Docker image.

Syntax:

docker run [OPTIONS] IMAGE [COMMAND] [ARGUMENTS]
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -it ubuntu bash
Enter fullscreen mode Exit fullscreen mode

This command runs the ubuntu image in an interactive terminal (-it) and executes bash.

b. docker build

The docker build command is used to build a Docker image from a Dockerfile.

Syntax:

docker build -t <image_name>:<tag> <path_to_dockerfile>
Enter fullscreen mode Exit fullscreen mode

Example:

docker build -t myapp:latest .
Enter fullscreen mode Exit fullscreen mode

This builds the Docker image from the Dockerfile in the current directory (.) and tags it as myapp:latest.

c. docker ps

The docker ps command lists running containers. By default, it shows only active containers.

Syntax:

docker ps [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

Example:

docker ps
Enter fullscreen mode Exit fullscreen mode

This will show all running containers.

To list all containers (including stopped ones), use the -a flag:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

d. docker stop

The docker stop command is used to stop a running container.

Syntax:

docker stop <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Example:

docker stop my_container
Enter fullscreen mode Exit fullscreen mode

This stops the container named my_container.

e. docker start

The docker start command is used to start a stopped container.

Syntax:

docker start <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Example:

docker start my_container
Enter fullscreen mode Exit fullscreen mode

f. docker exec

The docker exec command is used to run a command in a running container.

Syntax:

docker exec [OPTIONS] <container_name_or_id> <command>
Enter fullscreen mode Exit fullscreen mode

Example:

docker exec -it my_container bash
Enter fullscreen mode Exit fullscreen mode

This runs an interactive bash shell inside the running container my_container.

g. docker build

The docker build command is used to build a Docker image from a Dockerfile.

Syntax:

docker build -t <image_name>:<tag> <path_to_dockerfile>
Enter fullscreen mode Exit fullscreen mode

Example:

docker build -t myapp:latest .
Enter fullscreen mode Exit fullscreen mode

This builds the Docker image from the Dockerfile in the current directory (.) and tags it as myapp:latest.

h. docker images

The docker images command lists all available images on the local machine.

Syntax:

docker images [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

Example:

docker images
Enter fullscreen mode Exit fullscreen mode

This shows all Docker images stored locally.

i. docker rmi

The docker rmi command is used to remove a Docker image from the local system.

Syntax:

docker rmi <image_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Example:

docker rmi myapp:latest
Enter fullscreen mode Exit fullscreen mode

This removes the image myapp:latest.

j. docker network

The docker network command allows you to manage Docker networks.

Syntax:

docker network <subcommand> [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

Example:

docker network ls
Enter fullscreen mode Exit fullscreen mode

This lists all available networks.


3. Docker CLI Options

Docker CLI commands support various options that can change the behavior of the command. Common options include:

  • -it: Combines -i (interactive) and -t (allocate a pseudo-TTY) for running containers interactively.
  • -d: Run a container in detached mode (in the background).
  • -v: Bind mount or volume option for specifying volumes.
  • -p: Publish container ports to the host machine.

Example:

docker run -d -p 8080:80 nginx
Enter fullscreen mode Exit fullscreen mode

This runs the nginx container in detached mode (-d) and maps port 8080 on the host to port 80 in the container (-p 8080:80).


4. Docker CLI Workflow for Development

Here’s a typical workflow for developing applications with Docker using the CLI:

  1. Create a Dockerfile: Write a Dockerfile that specifies the image build process.
  2. Build the Image: Run docker build to create a Docker image from the Dockerfile.
  3. Run the Container: Use docker run to start a container from the built image.
  4. Interact with the Container: Use docker exec to interact with the running container.
  5. Stop the Container: Use docker stop when you’re done with the container.
  6. Clean Up: Use docker rm and docker rmi to remove containers and images no longer needed.

5. Docker CLI for Container Management

Managing containers with Docker involves starting, stopping, inspecting, and removing containers. Here are some of the key commands for container management:

  • docker logs: Retrieve logs from a running or stopped container.

Example:

  docker logs my_container
Enter fullscreen mode Exit fullscreen mode
  • docker stats: View real-time statistics of running containers, such as CPU and memory usage.

Example:

  docker stats
Enter fullscreen mode Exit fullscreen mode
  • docker pause: Pause a running container (halts its processes).

Example:

  docker pause my_container
Enter fullscreen mode Exit fullscreen mode
  • docker unpause: Unpause a paused container.

Example:

  docker unpause my_container
Enter fullscreen mode Exit fullscreen mode
  • docker restart: Restart a container.

Example:

  docker restart my_container
Enter fullscreen mode Exit fullscreen mode

6. Best Practices for Using Docker CLI

a. Use Docker Compose for Multi-Container Applications

For applications that require multiple containers (e.g., a web server, database, etc.), Docker Compose is a helpful tool for defining and running multi-container Docker applications using a YAML configuration file. Docker Compose simplifies complex setups and orchestrates the running of multiple containers.

b. Clean Up Regularly

As you work with Docker, containers and images accumulate. To avoid clutter and free up disk space, regularly clean up unused images, stopped containers, and networks using the docker prune command.

docker system prune
Enter fullscreen mode Exit fullscreen mode

c. Keep Your Images Small

To optimize the build process and reduce image size, minimize the number of layers in your Dockerfile. Combine RUN instructions where possible, and use multi-stage builds to keep your final image lightweight.


7. Conclusion

The Docker CLI is a powerful tool that allows you to interact with Docker and manage containers and images directly from the terminal. By mastering the most common Docker CLI commands, you can efficiently build, run, and manage Docker containers for your applications. Whether you are working on a local development setup or deploying containers in a production environment, the Docker CLI is an essential part of your containerization toolkit.


Top comments (0)