Docker CLI
Docker CLI (Command Line Interface) is a powerful tool for interacting with Docker, allowing users to manage containers, images, networks, and volumes through commands in the terminal. Below is a detailed guide covering common Docker commands, flags they accept, and usage examples.
docker pull
Purpose:
This command downloads a Docker image from a registry (such as Docker Hub) to your local machine.
Syntax:
docker pull [OPTIONS] IMAGE[:TAG|@DIGEST]
Flags:
--all-tags, -a
: Download all tags for the image.--platform
: Specify the platform to pull from (e.g.,linux/amd64
,linux/arm64
).--quiet, -q
: Suppress output; only shows image ID.
Examples:
-
Pull the latest version of the
ubuntu
image:docker pull ubuntu
-
Pull a specific version of the
nginx
image:docker pull nginx:1.21
-
Pull an image for a specific platform:
docker pull --platform linux/arm64 ubuntu
-
Pull all tags of an image:
docker pull --all-tags ubuntu
docker build
Purpose:
This command is used to build a Docker image from a Dockerfile.
Syntax:
docker build [OPTIONS] PATH | URL | -
Flags:
-f, --file
: Specify the Dockerfile to use.-t, --tag
: Name and optionally tag an image (e.g.,image:tag
).--no-cache
: Do not use cache when building the image.--build-arg
: Set build-time variables.--pull
: Always attempt to pull a newer version of the image.--platform
: Set the platform to build for.
Examples:
-
Build an image using the current directory’s
Dockerfile
:docker build -t myapp .
-
Build an image with a specific Dockerfile:
docker build -f Dockerfile.dev -t myapp .
-
Build an image with no cache:
docker build --no-cache -t myapp .
-
Build an image for a specific platform:
docker build --platform linux/amd64 -t myapp .
docker exec
Purpose:
This command allows you to run commands in a running container.
Syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Flags:
-d, --detach
: Run the command in the background.-i, --interactive
: Keep STDIN open even if not attached.-t, --tty
: Allocate a pseudo-TTY.
Examples:
-
Execute a command inside a running container (
mycontainer
) to start a bash shell:docker exec -it mycontainer bash
-
Run a command in the background:
docker exec -d mycontainer touch /tmp/hello.txt
-
Run a command interactively:
docker exec -it mycontainer ls /app
docker rm
Purpose:
This command removes one or more containers.
Syntax:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Flags:
-f, --force
: Forcefully remove a running container (use with caution).-v, --volumes
: Remove the volumes associated with the container.--link
: Remove the specified link.
Examples:
-
Remove a stopped container (
mycontainer
):docker rm mycontainer
-
Forcefully remove a running container (
mycontainer
):docker rm -f mycontainer
-
Remove a container and its associated volumes:
docker rm -v mycontainer
docker start
Purpose:
This command starts one or more stopped containers.
Syntax:
docker start [OPTIONS] CONTAINER [CONTAINER...]
Flags:
-a, --attach
: Attach to the container's output.-i, --interactive
: Keep STDIN open even if not attached.
Examples:
-
Start a stopped container (
mycontainer
):docker start mycontainer
-
Start a container and attach to its output:
docker start -a mycontainer
docker stop
Purpose:
This command stops one or more running containers.
Syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Flags:
-t, --time
: Specify a grace period (in seconds) to stop the container. Default is 10 seconds.
Examples:
-
Stop a running container (
mycontainer
):docker stop mycontainer
-
Stop a container with a 5-second grace period:
docker stop -t 5 mycontainer
docker pause
Purpose:
This command pauses all processes within one or more running containers.
Syntax:
docker pause CONTAINER [CONTAINER...]
Examples:
-
Pause a running container (
mycontainer
):docker pause mycontainer
docker unpause
Purpose:
This command unpauses one or more paused containers, resuming their processes.
Syntax:
docker unpause CONTAINER [CONTAINER...]
Examples:
-
Unpause a paused container (
mycontainer
):docker unpause mycontainer
docker ps
Purpose:
This command lists all running containers.
Syntax:
docker ps [OPTIONS]
Flags:
-a, --all
: Show all containers (default shows only running).-q, --quiet
: Show only container IDs.--filter
: Filter containers based on criteria (e.g., status, name).--format
: Format the output using Go templating.
Examples:
-
List all running containers:
docker ps
-
List all containers (including stopped ones):
docker ps -a
-
List only container IDs:
docker ps -q
-
Filter containers by status:
docker ps --filter "status=exited"
docker images
Purpose:
This command lists all Docker images on your local machine.
Syntax:
docker images [OPTIONS]
Flags:
-a, --all
: Show all images (including intermediate images).-q, --quiet
: Show only image IDs.
Examples:
-
List all Docker images:
docker images
-
List only image IDs:
docker images -q
docker prune
Purpose:
This command removes unused containers, networks, images, and volumes, helping to clean up disk space.
Syntax:
docker prune [OPTIONS]
Flags:
--all
: Remove all unused images, not just dangling ones.--force, -f
: Force the prune action without confirmation.--volumes
: Also prune unused volumes.
Examples:
-
Remove all unused containers, networks, and dangling images:
docker system prune
-
Remove all unused images (not just dangling):
docker image prune --all
-
Remove unused volumes:
docker volume prune
-
Force removal without confirmation:
docker system prune -f
docker logs
Purpose:
This command fetches logs from a container, useful for debugging and monitoring.
Syntax:
docker logs [OPTIONS] CONTAINER
Flags:
-f, --follow
: Follow the log output (real-time).--tail
: Show the lastN
lines of logs.--since
: Show logs since a specific timestamp.
Examples:
-
View logs of a container (
mycontainer
):docker logs mycontainer
-
Follow logs in real-time:
docker logs -f mycontainer
-
View the last 100 lines of logs:
docker logs --tail 100 mycontainer
-
View logs since a specific time:
docker logs --since "2025-01-12T00:00:00" mycontainer
docker debug
Purpose:
This command is used to provide more detailed information and assist with debugging Docker components.
Syntax:
docker debug
Examples:
- Running
docker debug
typically gives information about container runtime errors, failed commands, and system diagnostics, though it might require additional debugging tools or flags depending on the environment and context.