Dockerfile Example

When creating a Dockerfile it can start out simply and can quickly grow in complexity as you're working with it. This page provides a brief example of a Dockerfile and some of the 'instruction' syntax (i.e. commands) that is used within file.  This example is for demonstration purposes only, and shows the basic structure of the file and provides remarks for what each Docker instruction is performing.


Dockerfile Example


# Creates a layer from Ubuntu:Latest Docker image 

FROM ubuntu:latest


# Copies files from the current directory into the container.

COPY . /app


# Builds the application with the make command.

RUN make /app


# Specifies command to be run within the container.

CMD python /app/app.py


Dockerfile Instructions

Below, is a list of the most common 'Dockerfile Instructions' (aka commands) that will be used about 90% of the time when you’re creating Dockerfiles.  The list includes a brief description of what each Dockerfile Instruction does.


ADD: Copies new files, directories or remote file URLs from the <sourcePath> and then adds them to the container filesystem at the path <destinationPath>.

ARG: Defines a variable that users can pass at build-time (i.e. running 'docker build' command) using the --build-arg <varname>=<value> flag. 

CMD: Defines which executable (and any optional parameters) is run when the container starts running.

COPY: Copies new files or directories from the <sourcePath> and then adds them to the container filesystem at the path <destinationPath>. 

ENTRYPOINT: Allows a container to be  configured run as an executable.

ENV: Sets the environment variable (<key>=<value> pair), which is used by scripts and applications for configuration information about the environment. This value can be used in the environment by all subsequential instructions during the build stage.

EXPOSE: Informs Docker that the container listens on a specified network ports during runtime. You can specify whether the port listens on TCP or UDP, the default is TCP if no protocol is not specified. 

FROM: Initializes a new build stage and sets the 'Base Image' for subsequent instructions. 

Note: A valid Dockerfile must be started with the FROM command.

HEALTHCHECK: Instructs Docker how to test the container to make sure that it is still working properly.

LABEL: Adds metadata to an image. 

ONBUILD: Adds a trigger instruction to an image to be executed at a later time, when the image is used as the base for another build.

RUN: Executes any commands in a new layer on top of the current image and commits the results. The resulting committed image will be utilized for the next step in the Dockerfile.

SHELL: Allows the default shell used for shell commands to be overridden.

STOPSIGNAL: Sets the system call signal that will be sent when the container exits (i.e. shutdown or stopped).

USER:  Sets the user name (or UID) or user group (or GID) for use when running the image for: RUN, CMD and ENTRYPOINT instructions.

WORKDIR: Sets the working directory for: RUN, CMD, ENTRYPOINT, COPY and ADD instructions. 

VOLUME: Creates a mount point with the specified name and marks it as an externally mounted volume from the native host or other containers.