Dockerfile

  • Dockerfile is a script, composed of various commands (instructions) and arguments listed successively to automatically perform action on a base image in order to create (or form) new one.
Dockerfile is used to build or run Docker image by giving some instructions to customize am existing Docker image based on our requirement, in an automated way with running a Docker container.

Syntax to write instruction and it’s argument within a Dockerfile is:-

# comment

INSTRUCTION arguments

* Instruction can be given in lowercase or uppercase letters. But to differentiate from the instructions and arguments we use uppercase letters.*

  • Docker runs instructions in a Dockerfile in order. A Dockerfile must start with a `FROM` instruction. The FROM instruction specifies the Base Image from which you are building.
  • A Docker image consists of read-only layers each of which represents a Dockerfile instruction. The layers are stacked and each one is a delta of the changes from the previous layer.
  • When we run an image and generate a container, we add a new writable layer (the “container layer”) on top of the underlying layers. All changes made to the running container, such as writing new files, modifying existing files, and deleting files, are written to this thin writable container layer.

Understand Instructions:-

FROM: It define the base image to use to start the build process. This instruction is used to set the base image for subsequent instructions. It is mandatory to set this in the first line of a Dockerfile. You can use it any number of times though.

Example:

FROM UBUNTU

MAINTAINER: This is a non-executable instruction used to indicate the author of the Dockerfile.

It should come nonetheless after FROM 

Example:

MAINTAINER author_name

RUN: This instruction lets us execute a command on top of an existing layer and create a new layer with the result of command execution. This is what runs within the container at build time.

 Example:

RUN apt-get update && apt-get install –y apache2 

ENV: This instruction can be used to set the environment variables in the container. These variables consist of “key-value” pairs which can be access within the container by scripts and application alike. 

Example:

ENV APACHE_RUN_USER www-data

ENV APACHE_RUN_GROUO www-data

EXPOSE: While running your service in the container you may want your container to listen on specified port The EXPOSE instruction helps you do this.

Example:

EXPOSE 8080

CMD: The major difference between CMD and RUN is that CMD doesn’t execute anything during the build time. It just specifies the intended command for the image. Whereas RUN actually executes the command during build time.

** Note: there can be only one CMD instruction in a Dockerfile, if you add more, only the last one takes effect. **

 Example:

CMD [“/usr/sbin/apache2”, “-D”, “FOREGROUND”]

ENTRYPOINT: You can use this instruction to set the primary command for the image. For example, if you have installed only one application in your image and want it to run whenever the image is executed, ENTRYPOINT is the instruction for you.

Also, all the elements specified using CMD will be overridden, except the arguments. They will be passed to the command specified in ENTRYPOINT.

Example:

ENTRYPOINT ["/usr/sbin/apache2"]

CMD ["-D", "FOREGROUND"]

LABEL: You can assign metadata in the form of key-value pairs to the image using this instruction. It is important to notice that each LABEL instruction creates a new layer in the image, so it is best to use as few LABEL instructions as possible.

Example:

LABEL version="1.0" description="This is a custom apache2 Image"

COPY: This instruction is used to copy files and directories from a specified source to a destination (in the file system of the container).

EXAMPLE:

COPY index.html /var/www/html

ADD: This instruction is similar to the COPY instruction with few added features like remote URL support in the source field and local-only tar extraction. But if you don’t need an extra features, it is suggested to use COPY as it is more readable.

Example:

ADD http://www.site.com/downloads/sample.tar.xz /usr/src

VOLUME: You can use the VOLUME instruction to enable access to a location on the host system from a container. Just pass the path of the location to be accessed.

Example:

VOLUME /var/www/html

USER: The USER directive is used to set the UID (or username) which is to run the container based on the image being built.

Example:

USER 1111 or name

WORKDIR: This is used to set the currently active directory for other instructions such as RUN, CMD, ENTRYPOINT, COPY and ADD.

**Note that if relative path is provided, the next WORKDIR instruction will take it as relative to the path of previous WORKDIR instruction. **

Example:

WORKDIR /user

WORKDIR home

RUN pwd

This will output the path as /user/home.

ONBUILD: This instruction adds a trigger instruction to be executed when the image is used as the base for some other image. It behaves as if a RUN instruction is inserted immediately after the FROM instruction of the downstream Dockerfile. This is typically helpful in cases where you need a static base image with a dynamic config value that changes whenever a new image has to be built (on top of the base image).

Example:

ONBUILD RUN rm –rf /usr/temp

To view or add a comment, sign in

More articles by Shubham Agarwal

  • Ansible Playbook

    A playbook consists of simple set of steps called tasks that run on remote machines defined in inventory file. These…

    2 Comments

Others also viewed

Explore content categories