Dockerfile Commands with Examples
FROM - Specifies the base image.
FROM ubuntu:20.04
RUN - Executes commands in a new layer.
RUN apt-get update && apt-get install -y curl
CMD - Sets the default command to run when the container starts.
CMD ['echo', 'Hello, World!']
ENTRYPOINT - Configures the container to run as an executable.
ENTRYPOINT ['echo']
CMD ['Default message']
COPY - Copies files from the local filesystem to the container.
COPY index.html /var/www/html/
ADD - Adds files or archives from a URL or local system.
ADD https://example.com/sample.tar.gz /app/
ENV - Sets environment variables inside the container.
ENV APP_ENV=production
ARG - Defines build-time arguments.
ARG VERSION=1.0
LABEL - Adds metadata to the image.
LABEL maintainer='[email protected]'
WORKDIR - Sets the working directory inside the container.
WORKDIR /app
USER - Specifies the user to run commands as.
USER appuser
EXPOSE - Documents the container's exposed port.
EXPOSE 8080
VOLUME - Creates a mount point for storing persistent data.
VOLUME /data
ONBUILD - Adds triggers that run during the build of a child image.
ONBUILD RUN echo 'Child image build trigger'
STOPSIGNAL - Sets the signal to stop the container.
STOPSIGNAL SIGTERM
HEALTHCHECK - Defines a health check for the container.
HEALTHCHECK CMD curl --fail http://localhost:8080 || exit 1
SHELL - Sets the shell to use for subsequent RUN instructions.
SHELL ['/bin/bash', '-c']
Comprehensive Example Dockerfile
# Base image
FROM ubuntu:20.04
# Build-time argument
ARG VERSION=1.0
# Metadata
LABEL maintainer="[email protected]"
LABEL version=$VERSION
# Set environment variable
ENV APP_ENV=production
# Set working directory
WORKDIR /app
# Copy application files
COPY . /app
# Add remote file (and extract if tar.gz)
ADD https://example.com/sample.tar.gz /app/
# Install dependencies
RUN apt-get update && apt-get install -y curl python3
# Set user
USER root
# Expose application port
EXPOSE 8080
# Mount a volume
VOLUME /data
# Add a health check
HEALTHCHECK CMD curl --fail http://localhost:8080 || exit 1
# Specify stop signal
STOPSIGNAL SIGTERM
# Set up a trigger for child images
ONBUILD RUN echo "This will run on child builds"
# Use a specific shell for the commands
SHELL ["/bin/bash", "-c"]
# Default executable and arguments
ENTRYPOINT ["python3"]
CMD ["app.py"]