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
Command | Description |
---|---|
FROM | Specifies the base image for the Dockerfile |
MAINTAINER | Specifies the author of the Dockerfile |
RUN | Executes a command in the container |
CMD | Specifies the default command to run when the container starts |
EXPOSE | Exposes a port from the container to the host |
ENV | Sets an environment variable in the container |
ADD | Copies files from the host to the container |
COPY | Copies files from the host to the container |
ENTRYPOINT | Specifies the entry point for the container |
VOLUME | Creates a mount point for a volume |
Advanced Commands
Command | Description |
---|---|
USER | Specifies the user to run the container as |
WORKDIR | Sets the working directory for the container |
ARG | Defines a build-time argument |
ONBUILD | Specifies a command to run when the image is used as a base for another image |
LABEL | Adds metadata to the image |
STOPSIGNAL | Specifies the signal to send to the container to stop it |
HEALTHCHECK | Specifies a command to check the health of the container |
SHELL | Specifies 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: