Docker File
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.
- Introduction
- Docker File (You are here!)
- Basic Docker Commands
- Port and Volume Mapping
- Docker Networking
- Docker Storage
- Docker Compose
- 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/gamesCOPY 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.
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.
- 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.
- ENTRYPOINT should be defined when using the container as an executable.
- 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.
- 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