Skip to content

Building a Dockerfile

Alex Hurt edited this page Nov 17, 2022 · 4 revisions

What is a Dockerfile?

A Dockerfile defines how a docker image will be built via a series of commands. The Dockerfile is a powerful tool because given a Dockerfile, your container can be built on any system with Docker installed.

Dockerfile Commands

Each line in a Dockerfile begins with a command, which is the action that should be taken in the Docker build process. The most common and necessary commands are listed below:

FROM

The FROM command defines the starting point for the Docker build process. If you are building custom software from the ground up, you may be setting this to a linux operating system, such as ubuntu.

If you are working in a framework or programming language, this may also be a certain version of that framework, such as python:3.8, node:8, pytorch:1.8, etc.

The docker image specified in the from command must be present either on the public docker hub repository, or be visible to the build context via a full URL.

ENV & ARG

The ENV and ARG commands define environment variables during the build process. There are 2 key differences between them:

  1. ENV commands persist to the final container, so ENV key value will persist to the launched container, while ARG key value will not
  2. ARG commands can be overridden at build time, which allows for templating of Dockerfiles

The ENV command is very useful for tasks like ensuring your executable is present on the PATH of the container, while ARG is useful for things like ensuring you are building the correct version of the software.

CMD and ENTRYPOINT

The CMD and ENTRYPOINT commands both set the command to be run when the container starts via a docker run command.

There is 1 key difference between them: a CMD can be overridden via either a downstream Dockerfile (i.e., someone uses your container in their FROM command) or via the command line during container startup. An ENTRYPOINT cannot be as easily overridden, and requires a special flag to be overridden.

Best practice is to set a CMD unless you have reasoning behind using ENTRYPOINT

COPY

The COPY command is how local files are copied into the container. Recall that Docker containers are mini virtual machines, meaning that it has its own filesystem. In order to build and run software in docker, we often need to copy our code and scripts into the container. To do this, we use the COPY command

RUN

The RUN command tells the docker build process to run a command inside the container. This could be everything from installing a package with apt (RUN apt install) to creating and removing files in the container.

The commands available for the RUN command are determined by the base container, i.e. apt will only be available in debian based containers.

A Simple Example

Here is a simple example of a Dockerfile that installs two python packages and copies in our code

FROM python:3.8 

RUN pip install --upgrade pip && pip install numpy pandas

ENV MYKEY=MYVALUE

RUN mkdir -p /workspace
COPY myscript.py  /workspace

CMD /bin/bash

Sample Dockerfiles

There are sample Dockerfiles available in the docker directory of this repo.

Clone this wiki locally