-
Notifications
You must be signed in to change notification settings - Fork 2
Building 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.
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:
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.
The ENV and ARG commands define environment variables during the build process. There are 2 key differences between them:
-
ENVcommands persist to the final container, soENV key valuewill persist to the launched container, whileARG key valuewill not -
ARGcommands 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.
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
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
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.
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
There are sample Dockerfiles available in the docker directory of this repo.
Docker
Nautilus Basics
Nautilus Advanced Usage
Jupyter