Docker File

Sumeet Gyanchandani
The Startup
Published in
2 min readJul 12, 2020

Two very important aspects of becoming a Docker super user is the understanding of the Dockerfile and knowing your Docker commands. The idea of putting together this page is not to memorize but to familiarize yourself with available options. You can always revisit the page if you know what you are looking for.

  1. Introduction
  2. Docker File (You are here!)
  3. Basic Docker Commands
  4. Port and Volume Mapping
  5. Docker Networking
  6. Docker Storage
  7. Docker Compose
  8. Deleting Docker Entities

Docker file is a text file written in a special format that docker can understand. It is in INSTRUCTION and argument format. We’ll discuss several Instructions in this tutorial.

Here is the format of the Dockerfile:

# Comment
INSTRUCTION arguments

This is an example of a simple Dockerfile for whalesay application

FROM ubuntu:14.04# install cowsay, and move the "default.cow" out of the way so we can overwrite it with "docker.cow"
RUN apt-get update && apt-get install -y cowsay --no-install-recommends && rm -rf /var/lib/apt/lists/* \ && mv /usr/share/cowsay/cows/default.cow /usr/share/cowsay/cows/orig-default.cow
# "cowsay" installs to /usr/games
ENV PATH $PATH:/usr/games
COPY docker.cow /usr/share/cowsay/cows/RUN ln -sv /usr/share/cowsay/cows/docker.cow /usr/share/cowsay/cows/default.cowCMD ["cowsay"]

The instruction is not case-sensitive. However, the convention is for them to be UPPERCASE to distinguish them from arguments more easily. Docker runs instructions in a Dockerfile in order. A Dockerfile must begin with a “FROM” instruction. The FROM instruction specifies the Parent Image from which you are building. If a Dockerfile does not have a parent, it starts FROM scratch.

Dockerfile Intructions

Understand how CMD and ENTRYPOINT interact

Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are a few rules that describe their co-operation.

  1. Dockerfile should specify at least one of CMD or ENTRYPOINT commands. If you do not provide one, it would be inherited from the base image.
  2. ENTRYPOINT should be defined when using the container as an executable.
  3. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
  4. CMD will be overridden when running the container with alternative arguments.

In the next section, we will see basic Docker Commands.

Reference:

[1] Docker, Dockerfile reference (2020), Docker Documentation

--

--

Sumeet Gyanchandani
The Startup

Associate Director at UBS | Former Machine Learning Engineer at Apple, Microsoft Research, Nomoko, Credit Suisse | Master of Science in Artificial Intelligence