Skip to content

Instantly share code, notes, and snippets.

@codenickycode
Created January 3, 2023 13:38
Show Gist options
  • Save codenickycode/af6e8f585bac04f62ff56933f2cec110 to your computer and use it in GitHub Desktop.
Save codenickycode/af6e8f585bac04f62ff56933f2cec110 to your computer and use it in GitHub Desktop.
[Docker: Dockerfile] Syntax to build an image #docker
# Argument outside the build stage
# To be used in first FROM
ARG <key>=<value>
# Parent image
FROM [--platform=<platform>] <image> [AS <name>]
# or
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
# or
FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
# Set the working directory
# Create it if it doesn't exist
WORKDIR /foo
# Run a command and commit the result
RUN <command>
# or, parsed as json (use "" and escape characters)
RUN ["executable", "param1", "param2"]
# or, creating a bind mount
# https://docs.docker.com/engine/reference/builder/#run---mount
RUN --mount=[type=<TYPE>][,option=<value>[,option=<value>]...]
# Default command when executing a container
# This can be overridden by cli `docker run` arguments
# exec form, this is the preferred form
CMD ["executable","param1","param2"]
# or, as default parameters to ENTRYPOINT
CMD ["param1","param2"]
# Indicate a port intended to be published
# `docker run -P` will publish all exposed ports
EXPOSE <port>[/<protocol>]
# Set environment variable that persists in running container
# This can be overridden by `docker run --env <key>=<value>`
ENV <key>=<value>
# To not persist a variable into container, use an ARG
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y ...
# Copy files to image's filesystem
COPY <src> <dest>
# Copy from previous build stage
COPY --from=<name> <src> <dest>
# Copy files with more options
# https://docs.docker.com/engine/reference/builder/#add
ADD <src> <dest>
# Create a mount point
VOLUME <name>
# Configure container to run as executable
# Can be overridden with `docker run --entrypoint`
# additional cli args will override any CMD args
ENTRYPOINT ["node", "index.js"]
# Note: May be wise to dumb-init first, for PID 1
# https://github.com/Yelp/dumb-init
RUN apt install dumb-init
# then...
ENTRYPOINT [ "/usr/bin/dumb-init", "--" ]
# start your actual command
CMD [ "node", "index.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment