dockerfile

Dockerfile #

A Dockerfile defines how Docker images are constructured.

The structure includes lines with INSTRUCTION (left) and ARGUMENT (right).

Every Docker container must be based off another image, including an operating system.

For details on how to actually use the Dockerfile to build images, go to the images page.

The flow #

Dockerfile -> Build -> Image -> Container

The file #

It’s just a file called Dockerfile. That’s it.

The directory #

Imagine that we’re working in a directory that looks like this:

project/
project/Dockerfile
project/src/

The contents #

Many of these components are optional.

FROM <base image>:<version>

ENV <key>:<value>

COPY <source> <destination>

EXPOSE <port number>/<udp or tcp - optional>

CMD ["<executable>", "<parameter>", "argument"] 

Subsections here will provide a more detailed explanation of the Dockerfile contents.

FROM #

Where the <base image> is the basis on which the new image is to be built. Every image is based off of some other image, such as Ubuntu, or something lighter that’s more application-specfic.

Specification of the <version> is optional. By default, it’ll grab the latest version.

ENV #

Key-value pair specifications that define environment variables.

Entirely optional. Environment variables can also be defined when the containers are created.

COPY #

Copies files from the host machine into the image itself.

An example of a COPY line might look like:

COPY src/ usr/share/<subdirectory>

EXPOSE #

Expose some ports. This may not be needed in all applications.

RUN #

CMD #

Command to be use for the entrypoint. Think of this as the command that gets run everytime the container is run.

Executables and arguments are passed to the command as an array (i.e., quoted values, separated with commas). Looks funky.

For example:

CMD ["nginx", "-g", "daemon off;"]

A complete example #

FROM nginx

Example Dockerfile:

# start from a base OS or another image
FROM Ubuntu


# Install dependencies
RUN apt-get update
RUN apt-get install python

RUN pip install-flask
RUN pip install flask-mysql

# Copy source code from local system
COPY ./opt/source-code

# Specify entrypoint
ENTRYPOINT FLASK_APP=/out/source-code/app.py flask run

^ For this snippet, see Youtube.

Docker ignore #

Docker ignore files tells Docker to ignore files during the build.

This file is located alongside the Dockerfile at .dockerignore.

This might include things like *.log files, intermediate build files, or anything you’d want to exclude from the container image itself.

Resources #