Docker Config Reference (docker rmi / docker run / docker save)

We go over docker rmi / docker run / docker save examples

Docker Config Reference (docker rmi / docker run / docker save)
Docker config examples docker rmi / docker run / docker save

Examples of the Docker rmi Command

The docker rmi command removes one or more Docker images from the local host, freeing associated storage space. It targets images by name, tag, or digest, and requires that no containers reference the image unless forced. This operation is essential for maintaining a clean image repository, reducing disk usage, and managing version control.

docker rmi nginx:latest

This command removes the "nginx:latest" image from the local Docker cache. It deletes all layers specific to this tagged version, provided no running or stopped containers depend on it, thereby reclaiming storage while preserving other Nginx variants.

docker rmi -f ubuntu:20.04

Utilizing the -f or --force flag, this command forcibly removes the "ubuntu:20.04" image, even if containers reference it. It overrides safety checks, which is useful for cleanup in automated scripts but risks breaking dependent containers.

docker rmi mysql:8.0 sha256:digest

This command removes the MySQL 8.0 image specified by its SHA256 digest, ensuring precise deletion of a particular image variant. It enhances security by allowing removal based on immutable identifiers rather than mutable tags.

docker rmi --no-prune redis

With the --no-prune flag, this command removes the "redis" image without deleting untagged parent images. It preserves intermediate layers that might be shared with other images, optimizing storage management in multi-image environments.

docker rmi image1 image2

This command removes multiple images, "image1" and "image2", in a single operation. It streamlines batch deletions, facilitating efficient cleanup after testing or when updating to newer versions.

docker rmi -f golang:1.21

This forcibly removes the "golang:1.21" image, bypassing references from containers. It is applicable in development pipelines where rapid iteration requires clearing outdated toolchains without manual intervention.

docker rmi alpine

This command removes the "alpine" image, assuming the default latest tag. It is suitable for minimalist base image management, ensuring only required versions occupy space.

docker rmi --no-prune --force busybox

Combining --no-prune and --force, this command removes "busybox" without pruning parents and overrides dependencies. It provides controlled deletion in complex dependency graphs.

docker rmi registry.example.com/custom/image:tag

This command removes a privately hosted image from "registry.example.com". It supports enterprise environments by clearing local caches of custom-built images.

docker rmi hello-world

This command removes the "hello-world" test image. It is often used for initial Docker setup verification and subsequent cleanup to maintain a minimal local repository.

Examples of the Docker run Command

The docker run command creates and starts a new container from a specified image, allowing configuration of runtime parameters such as ports, volumes, and environment variables. It combines image pulling (if needed), container creation, and execution in one step, making it a core tool for deploying applications.

docker run nginx:latest

This command launches a container from the "nginx:latest" image, starting the web server in the foreground. It automatically pulls the image if absent, assigns a random name, and exposes default ports, ideal for quick testing.

docker run -d ubuntu:20.04 sleep infinity

With the -d or --detach flag, this command runs an Ubuntu 20.04 container in the background, executing "sleep infinity" to keep it active. It is useful for creating persistent shells or base environments for further customization.

docker run -p 8080:80 --name webserver nginx

This command runs an Nginx container named "webserver", mapping host port 8080 to container port 80. It enables external access to the web service, supporting development of networked applications.

docker run -v /host/data:/container/data mysql:8.0

Utilizing the -v or --volume flag, this command runs MySQL 8.0 with a host directory mounted to the container's data path. It ensures data persistence beyond the container's lifecycle, critical for databases.

docker run --env VAR=value golang:1.21 go version

This command runs a Golang 1.21 container with an environment variable "VAR" set to "value", executing "go version". It demonstrates passing runtime configurations for build or test scripts.

docker run -it alpine sh

The -it flags enable interactive mode with a pseudo-TTY, running an Alpine container and attaching a shell "sh". It provides an interactive environment for exploration or debugging.

docker run --rm busybox echo "Hello"

With --rm, this command runs Busybox to echo "Hello" and automatically removes the container upon exit. It is efficient for one-off tasks, avoiding residual containers.

docker run --network mynet redis

This command runs a Redis container attached to the custom network "mynet". It isolates connectivity, supporting multi-container applications with defined topologies.

docker run -u user --privileged customimage

This command runs "customimage" as a specific user with --privileged mode, granting extended capabilities. It is used for privileged operations, though with security considerations.

docker run --restart always hello-world

With --restart always, this command runs "hello-world" and configures automatic restarts on failure or host reboot. It ensures high availability for critical services.

Examples of the Docker save Command

The docker save command exports one or more images to a tar archive, enabling offline transfer or backup. It includes all layers and metadata, making it suitable for distribution without registry access.

docker save -o nginx.tar nginx:latest

This command saves the "nginx:latest" image to a file "nginx.tar". It creates a portable archive for transfer via storage media, useful in air-gapped environments.

docker save ubuntu:20.04 > ubuntu.tar

Using redirection, this command saves the "ubuntu:20.04" image directly to "ubuntu.tar". It streamlines output for immediate compression or scripting.

docker save -o multi.tar image1 image2

This command saves multiple images, "image1" and "image2", to "multi.tar". It consolidates exports for batch backups or sharing related images.

docker save mysql:8.0 | gzip > mysql.tar.gz

This command saves "mysql:8.0" and pipes the output to gzip for compression, creating "mysql.tar.gz". It optimizes storage for large database images.

docker save -o golang.tar golang:1.21

This command exports "golang:1.21" to "golang.tar". It supports toolchain archival for offline development or consistent builds across machines.

docker save alpine > alpine.tar

This command saves the "alpine" image (default latest) to "alpine.tar". It is ideal for minimal base image distribution in resource-limited setups.

docker save -o busybox.tar busybox

This command archives "busybox" to "busybox.tar". It facilitates utility image transfer for embedded or testing purposes.

docker save redis | bzip2 > redis.tar.bz2

Piping to bzip2, this command saves and compresses "redis" into "redis.tar.bz2". It reduces file size for efficient network transfers.

docker save -o custom.tar registry.example.com/image:tag

This command saves a private image to "custom.tar". It enables offline handling of enterprise-specific images.

docker save hello-world > test.tar

This command exports "hello-world" to "test.tar". It is used for basic image verification or educational demonstrations.