Skip to content

Creating Images

Commands

Building Images

Command Description
docker build . Build an image from the Dockerfile in the current folder
docker build -f <file> . Build an image from in the current folder
docker build -t <name> . Build an image and tag it with :latest
docker build -t <name>:<tag> . Build an image and tag it with :
docker build --no-cache . Build an image without cache
docker build --target <stage> . Build an image up to stage
docker build --build-arg MYARG=myvalue . Build an image with the build argument MYARG

Managing Images

Command Description
docker tag <image> <name>:<tag> Tag an image with another tag
docker tag <image> <registry>/<project>/<name>:<tag> Tag an image for a private registry
docker image push <name>:<tag> Push a tag to the registry
docker image push --all-tags <name> Push all tags to the registry

Docker Cache Management

Command Description
docker image prune -a --filter "until=24h" Delete all unused images older than 24h
docker system prune Delete all unused Docker objects

Extended Run Commands

Command Description
docker run <image> <args> Start an image with arguments. Overwrites CMD
docker run --entrypoint="<command>" <image> Start an image with a different entrypoint

Keywords in Dockerfile

Command Description
FROM <baseimage> Set baseimage
FROM <baseimage> as <name> Set baseimage and give the stage a name
RUN <command> Execute a command in the container
ENTRYPOINT ["<arg1>", "<arg2>"] Set the entry point. Is executed each time a container is started
CMD ["<arg1>", "<arg2>"] Set default arguments for the entrypoint
WORKDIR <path> Set the workdir in the container for all subsequent commands
USER <user/user-id> Set the user in the container for all subsequent commands
EXPOSE <port> Document required ports
COPY <source> <target> Copy from the host to the container
COPY --from=<stage> <source> <target> Copy from a previous stage to the current stage
ADD <source> <target> Copy to the container. Can be a host path or URL
ARG <name>[=<default>] Define build argument with optional default value
ENV <name>=<default> Define environment variable in container