tips
32 tips Images and volumes
One command, setting or habit at a time about Images and volumes: what it does, the line to copy and a caveat or two.
Images and volumes 32
-
Registers a trigger that runs inside the next image build that uses yours as its base.
ONBUILD RUN /usr/local/bin/python-build --dir /app/src -
Overrides the default shell for shell-form RUN, CMD and ENTRYPOINT lines in the rest of the Dockerfile.
SHELL ["/bin/bash", "-c"] -
Saves the current state of a container as a new image, with optional Dockerfile-style changes.
docker commit <container> myname/testimage:version3 -
Builds a fresh image from a root filesystem archive, from a file, a URL or stdin.
docker import /path/to/exampleimage.tgz -
Builds an image from a Dockerfile passed through a pipe or a here-document, with or without a context.
docker build -t myimage:latest - <<EOF -
ARG values are for the build and ENV values persist in containers, so they do different jobs.
ARG NODE_ENV=production -
Switches the build output from the redrawing view to plain text that you can scroll and copy.
docker build --progress=plain -t myimage . -
Mounts only a subdirectory of a named volume, so several containers can share one volume without seeing each other.
docker run --rm --mount src=logs,dst=/logs alpine mkdir -p /logs/app1 /logs/app2 -
Prints the sha256 digest of an image as the registry has it, so you can pin it.
crane digest nginx:latest -
Asks a registry over its HTTP API which repositories it holds.
curl http://localhost:5000/v2/_catalog -
Copy files into their own independent layer so base image updates do not force a recopy.
COPY . . -
Copy a default config file out of an image without starting the app.
docker create --name tmp nginx:alpine -
Know which unused volumes a prune removes, and when it removes your data too.
docker volume prune -a -
List only the images whose name or tag matches a pattern.
docker image ls --filter 'reference=nginx' -
Keep the old image under a second tag so you can go back after a bad update.
docker tag example/app:latest example/app:rollback -
List build cache entries with size and last use before you prune them.
docker buildx du -
Know why a volume starts with default files while a bind mount starts empty.
docker run --rm -v /etc/nginx nginx:alpine ls /etc/nginx -
Change the base image tag from the build command, and reuse the value inside the stage.
ARG PY_VERSION=3.13 -
Build and push an image that runs natively on amd64 and arm64.
docker buildx build \ -
Give copied files to the app user in the same step that copies them.
COPY --chown=app:app . . -
Write multi-line shell steps in a Dockerfile without backslashes and ampersands.
RUN <<EOF -
Builds a multi-stage Dockerfile only up to the stage you name, for debugging or testing.
FROM golang:1.26 AS build -
Passes a private value to one Dockerfile step as a temporary file that never lands in the image.
docker build --secret id=token,src=$HOME/.build-token -t example/app:1.0 . -
Writes the full filesystem of a container, running or stopped, to a tar file.
docker export red_panda > latest.tar -
Creates a Docker volume that mounts a share from your NAS over NFS, no plugin needed.
docker volume create --driver local \ -
Lists the CPU platforms of a container image before you pull it.
docker buildx imagetools inspect alpine -
Exports an image to a compressed archive that you can copy elsewhere and load again.
docker save myapp:1.2 | gzip > myapp.tar.gz -
Starts a throwaway shell in an image so you can check what files and tools it really contains.
docker run --rm -it --entrypoint sh nginx:stable -
Shows how much disk space Docker uses before you delete anything with prune.
docker system df -
Keeps unneeded and sensitive files out of the build context so builds run faster.
.git -
Archives the contents of a container's volume into a tar file on the host.
docker run --rm \ -
Removes only unused images older than a set age instead of every unused image.
docker image prune -a --filter "until=240h"
Nothing in this filter yet.