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]
For example:
docker run -it ubuntu
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]
Example:
docker run -it ubuntu bash
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>
Example:
docker build -t myapp:latest .
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]
Example:
docker ps
This will show all running containers.
To list all containers (including stopped ones), use the -a
flag:
docker ps -a
d. docker stop
The docker stop
command is used to stop a running container.
Syntax:
docker stop <container_name_or_id>
Example:
docker stop my_container
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>
Example:
docker start my_container
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>
Example:
docker exec -it my_container bash
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>
Example:
docker build -t myapp:latest .
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]
Example:
docker images
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>
Example:
docker rmi myapp:latest
This removes the image myapp:latest
.
j. docker network
The docker network
command allows you to manage Docker networks.
Syntax:
docker network <subcommand> [OPTIONS]
Example:
docker network ls
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
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:
-
Create a Dockerfile: Write a
Dockerfile
that specifies the image build process. -
Build the Image: Run
docker build
to create a Docker image from the Dockerfile. -
Run the Container: Use
docker run
to start a container from the built image. -
Interact with the Container: Use
docker exec
to interact with the running container. -
Stop the Container: Use
docker stop
when you’re done with the container. -
Clean Up: Use
docker rm
anddocker 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
-
docker stats
: View real-time statistics of running containers, such as CPU and memory usage.
Example:
docker stats
-
docker pause
: Pause a running container (halts its processes).
Example:
docker pause my_container
-
docker unpause
: Unpause a paused container.
Example:
docker unpause my_container
-
docker restart
: Restart a container.
Example:
docker restart my_container
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
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)