Skip to main content

docker image cheatsheet

1. List Docker Images

docker images

• Lists all images on your local machine.

Flags:

-a: Show all images, including intermediate layers.

--filter: Filter the output based on conditions (e.g., --filter dangling=true).

2. Pull an Image from a Registry

docker pull <image-name>:<tag>

• Downloads an image from a registry (default: Docker Hub).

• Example:

docker pull ubuntu:latest

If no tag is specified, it defaults to :latest.

3. Build an Image

docker build -t <image-name>:<tag> <path>

• Builds an image from a Dockerfile in the specified directory.

Example:

docker build -t my-app:1.0 .

4. Tag an Image

docker tag <source-image>:<tag> <new-image>:<new-tag>

• Assigns a new name or tag to an image.

Example:

docker tag my-app:1.0 myrepo/my-app:latest

5. Push an Image to a Registry

docker push <image-name>:<tag>

• Pushes a tagged image to a container registry.

Example:

docker push myrepo/my-app:latest

6. Inspect an Image

docker inspect <image-id-or-name>

• Displays detailed information about an image, such as configuration and layers.

7. Remove an Image

docker rmi <image-id-or-name>

• Removes an image from your local machine.

Force Removal:

docker rmi -f <image-id-or-name>

8. Save an Image

docker save -o <filename>.tar <image-name>:<tag>

• Saves an image to a tarball.

Example:

docker save -o my-image.tar my-app:1.0

9. Load an Image

docker load -i <filename>.tar

• Loads an image from a tarball.

Example:

docker load -i my-image.tar

10. View Image History

docker history <image-id-or-name>

• Shows the layers and commands used to create an image.

11. Prune Unused Images

docker image prune

• Removes dangling (unused) images.

Remove All Unused Images:

docker image prune -a

12. Export Image Layers

docker save <image-name>:<tag> | tar -xvf -

• Extracts individual layers of an image.

13. Check Disk Usage

docker system df

• Displays disk usage by images, containers, and volumes.

Common Workflows

Build and Run a New Image

1. Create a Dockerfile.

2. Build the image:

docker build -t my-app:1.0 .

3. Run a container from the image:

docker run -d -p 8080:8080 my-app:1.0