Dockerfile Cheat Sheet

In Docker

Dockerfile is a text file that contains a set of instructions used to create a Docker image. Docker is a popular containerization platform that allows developers to package their applications and dependencies into a single container, making it easier to deploy and manage applications across different environments.

A Dockerfile typically starts with a base image, which is a pre-built image that serves as the starting point for the new image. The Dockerfile then includes a series of instructions that define how to configure the image, install dependencies, and set up the application.

One of the key benefits of using Dockerfile is that it allows developers to automate the process of building and deploying their applications. By defining the configuration and dependencies in the Dockerfile, developers can ensure that the application runs consistently across different environments, from development to production.

This cheat sheet provides a comprehensive list of Dockerfile commands and their descriptions.

Basic Commands

CommandDescription
FROMSpecifies the base image for the Dockerfile
MAINTAINERSpecifies the author of the Dockerfile
RUNExecutes a command in the container
CMDSpecifies the default command to run when the container starts
EXPOSEExposes a port from the container to the host
ENVSets an environment variable in the container
ADDCopies files from the host to the container
COPYCopies files from the host to the container
ENTRYPOINTSpecifies the entry point for the container
VOLUMECreates a mount point for a volume

Advanced Commands

CommandDescription
USERSpecifies the user to run the container as
WORKDIRSets the working directory for the container
ARGDefines a build-time argument
ONBUILDSpecifies a command to run when the image is used as a base for another image
LABELAdds metadata to the image
STOPSIGNALSpecifies the signal to send to the container to stop it
HEALTHCHECKSpecifies a command to check the health of the container
SHELLSpecifies the default shell for the container

Examples

Basic Example

FROM ubuntu:latest
MAINTAINER John Doe <johndoe@example.com>
RUN apt-get update && apt-get install -y nginx
CMD [""nginx"", ""-g"", ""daemon off;""]
EXPOSE 80

Advanced Example

FROM ubuntu:latest
MAINTAINER John Doe <johndoe@example.com>
ARG APP_VERSION=1.0
ENV APP_HOME=/app
WORKDIR $APP_HOME
COPY . $APP_HOME
RUN apt-get update && apt-get install -y nginx
ENTRYPOINT [""nginx""]
CMD [""-g"", ""daemon off;""]
EXPOSE 80
LABEL version=$APP_VERSION
HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1

Conclusion

This cheat sheet provides a comprehensive list of Dockerfile commands and their descriptions. Use it as a reference when creating Docker images.

Reference:

https://docs.docker.com/engine/reference/builder/