Docker File

Sumeet Gyanchandani
Jul 12 · 2 min read

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.

Image for post
Image for post
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

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Sumeet Gyanchandani

Written by

Machine Learning Engineer at Nomoko | Former Thesis Intern at Microsoft Research | Master of Science in Artificial Intelligence

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Sumeet Gyanchandani

Written by

Machine Learning Engineer at Nomoko | Former Thesis Intern at Microsoft Research | Master of Science in Artificial Intelligence

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store