Introduction to Docker Command-Line Interface (CLI)
The Docker Command-Line Interface (CLI) is the primary tool for interacting with the Docker platform. It allows you to manage Docker containers, images, networks, and other Docker-related resources from the command line. In this answer, we'll explore the essential Docker CLI commands and how to use them effectively.
Understanding the Docker CLI Structure
The Docker CLI follows a hierarchical structure, where the main command docker is followed by various subcommands that perform specific actions. The general syntax for a Docker CLI command is:
docker [subcommand] [options] [arguments]
For example, the command docker run -it ubuntu /bin/bash would create and start a new container based on the Ubuntu image, and then enter an interactive terminal session within the container.
Common Docker CLI Commands
Here are some of the most commonly used Docker CLI commands:
- docker run: Create and start a new container.
- docker images: List all Docker images on the system.
- docker containers: List all running and stopped containers.
- docker build: Build a new Docker image from a Dockerfile.
- docker push: Upload a Docker image to a registry (e.g., Docker Hub).
- docker pull: Download a Docker image from a registry.
- docker start/stop/restart: Start, stop, or restart a container.
- docker exec: Execute a command inside a running container.
- docker logs: View the logs of a container.
- docker network: Manage Docker networks.
- docker volume: Manage Docker volumes.
Let's explore some of these commands in more detail:
docker run
The docker run command is used to create and start a new container. Here's an example:
docker run -it ubuntu /bin/bash
This command will:
- Create a new container based on the Ubuntu image.
- Start the container in interactive mode (
-ifor interactive,-tfor terminal). - Execute the
/bin/bashcommand inside the container, which will drop you into a Bash shell.
docker images
The docker images command lists all the Docker images available on your system:
docker images
This will display a table with information about each image, including the image name, tag, image ID, creation date, and size.
docker containers
The docker containers command lists all the Docker containers on your system, both running and stopped:
docker containers
This will display a table with information about each container, including the container ID, image, command, creation time, status, and ports.
docker build
The docker build command is used to build a new Docker image from a Dockerfile. Here's an example:
docker build -t my-app .
This command will:
- Build a new Docker image using the Dockerfile in the current directory.
- Tag the image with the name "my-app".
Visualizing Docker Concepts with Mermaid
To better understand the core concepts of the Docker CLI, let's use a Mermaid diagram:
This diagram shows the main subcommands of the Docker CLI and how they are related to the overall Docker platform.
Practical Examples and Use Cases
To help you better understand the Docker CLI, let's go through some practical examples and use cases:
-
Running a web server in a container:
docker run -d -p 80:80 --name my-web-server nginxThis command will create and start a new Nginx web server container, exposing port 80 on the host to port 80 in the container.
-
Building a custom Docker image:
# Create a Dockerfile echo "FROM ubuntu\nRUN apt-get update\nRUN apt-get install -y nginx" > Dockerfile # Build the image docker build -t my-custom-nginx . # Run the custom image docker run -d -p 80:80 --name my-custom-web-server my-custom-nginxThis example demonstrates how to create a custom Docker image by writing a Dockerfile and then building and running the image.
-
Inspecting container logs:
docker logs my-custom-web-serverThis command will display the logs for the "my-custom-web-server" container, which can be useful for troubleshooting and monitoring.
-
Executing a command in a running container:
docker exec -it my-custom-web-server /bin/bashThis command will open an interactive Bash shell inside the "my-custom-web-server" container, allowing you to inspect the container's file system and run additional commands.
By understanding these common Docker CLI commands and practicing with real-world examples, you'll be well on your way to becoming a Docker power user!
