Monday, April 22, 2019

Docker - Dockerfile

Greetings!

We used Docker images to create containers multiple times. We used images from Docker Hub to create those containers. Ever wondered how to create a Docker image? Docker can build images automatically by reading the instructions from a Dockerfile.

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Think of it as a shellscript. It gathered multiple commands into a single document to fulfill a single task.
build command is used to create an image from the Dockerfile.

$ docker build .
You can name your image as well.
$ docker build - my-image .

Let's first look at a Dockerfile and discuss what are those commands.
This is extracted from official MySQL Dockerfile.

FROM debian:stretch-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql

RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*

RUN mkdir /docker-entrypoint-initdb.d

ENV MYSQL_MAJOR 8.0
ENV MYSQL_VERSION 8.0.15-1debian9

VOLUME /var/lib/mysql
# Config files
COPY config/ /etc/mysql/
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 3306 33060
CMD ["mysqld"]

As you can see, this is how you install MySQL in your Linux machine. First we select our OS and install necessary software. Then configure the environment. All those instructions are added into Dockerfile using Docker specific commands.

Dockerfile Commands

  • FROM - specifies the base(parent) image.
  • RUN - runs a Linux command. Used to install packages into container, create folders, etc
  • ENV - sets environment variable.
  • COPY - copies files and directories to the container.
  • EXPOSE - expose ports
  • ENTRYPOINT - provides command and arguments for an executing container.
  • CMD - provides a command and arguments for an executing container. There can be only one CMD.
  • VOLUME - create a directory mount point to access and store persistent data.
  • WORKDIR - sets the working directory for the instructions that follow.
  • LABEL - provides metada like maintainer.
  • ADD - Copies files and directories to the container. Can unpack compressed files.
  • ARG - Define build-time variable.

COPY vs ADD

Both commands serve the similar purposes. Copy files into the image.
COPY let you copy files and directories from the host.
ADD do the same. Additionally it lets you use URL location and unzip files into image.
Docker documentation recommends to use COPY command.

ENTRYPOINT vs CMD

CMD - allows you to set a default command which will be executed only when you run a container without spedifying a command. If Docker container runs with a command, the default command will be ignored.
ENTRYPOINT - allows you to configure a container that will run as an executable. ENTRYPOINT command and parameters are not ignored when Docker container runs with command line parameters.
what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile

VOLUME

You declare VOLUME in your Dockerfile to denote where your container will write application data. When you run your container using -v you can specify its mounting point.
difference-between-volume-declaration-in-dockerfile-and-v-as-docker-run-paramet