What is Dockerfile?

Tram Ho

What is Dockerfile?

A Dockerfile is a file containing a set of instructions, which when docker calls that file, it can automatically generate images. Explore more https://docs.docker.com/engine/reference/builder/

Some commands used in Dockerfile:

FROM Is the base image for us to proceed to build a new image. This directive must be placed on top of the Dockerfile

RUN Used when you want to install additional packages during the image build process.

COPY This directive is used to copy a new file or directory from the source (src) and move it to the destination (dest) in the container’s file system. The source here can be the local host path or the URL. COPY has 2 forms:

ENV Define environment variables

EXPOSE Use the declaration of the container’s listen ports.

Combined with the -p parameter to expose the port of the container to the outside (NAT port)

CMD An directive that tells what command to execute each time the container is initialized. In the Dockerfile there is only one CMD . directive

ENTRYPOINT Allows you to configure a container that runs as an executable. ENTRYPOINT will overwrite the elements used by the CMD directive.

Example: docker run -it centos top -H

Interact CMD and ENTRYPOINT. Both commands determine which command is executed when running a container.

There are a few rules in the interaction between the two commands:

At least 1 directive is specified in the Dockerfile (of course it is possible that both directives appear in the Dockerfile)

  • ENTRYPOINT defined when using the container as an executable
  • CMD used as a way to specify default parameters for the ENTRYPOINT directive or to execute a command in a container
  • CMD will be overridden when running the container with interleaved parameters.
Example of CMD & ENTRYPOINT in Dockerfile:

The ADD directive is used to copy files, directories or files with the URL from the source (src) and add them to the destination path (dest) in the image’s file system.

ADD There are 2 forms:

Note: Notice the difference between COPY/ADDCOPY support copy only local path; still ADD can support copy .tar archive and support URL path.

WORKDIR A directive used to set the working directory. It is the same as the home directory, in this case the home directory of the container. When calling WORKDIR it will create directory on first call and access it as home directory. It can be used multiple times in a Dockerfile.

USER

Used to set the user to use when running the image and for some directives: RUN, CMD & ENTRYPOINT in Dockerfile.

VOLUME

Used to mount files/directories between host and container. The purpose of VOLUME is:

  • Retains data when the container is removed
  • Sharing data between host and container
  • Sharing data between containers

=>>> For more information on directives: https://docs.docker.com/engine/reference/builder

Build image with Dockerfile

We build the nginx installation package on the centos:7 base image (CentOS7:nginx). Create a Dockerfile with the following content:

Run Nginx in FOREGROUND ( By default, nginx runs in BACKGROUND mode – daemon on)

Build image

Run container

I would like to end the Dockerfile tutorial here. Sincerely and to win

Refer: https://docs.docker.com/engine/reference/builder/

Share the news now