Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed docker volume.

In this blog post, we will discuss Dockerfile. We will also create a docker image using Dockerfile.

Prerequisite for this demo:

  • One AWS EC2 instance with Linux 2 AMI and internet access.
  • Docker

What is Dockerfile?

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build with a Dockerfile, users can create an automated build that executes several command-line instructions in succession. The Docker daemon runs the instructions in the Dockerfile one-by-one, committing the result of each instruction to a new image if necessary, before finally outputting the ID of your new image. You can refer below documentation for detail usage instruction of Dockerfile.

Dockerfile components:

FROM: The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.

RUN: The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

CMD: The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

LABEL: The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing.

MAINTAINER (deprecated): The MAINTAINER instruction sets the Author field of the generated images. The LABEL instruction is a much more flexible version of this and you should use it instead.

EXPOSE: The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

ENV: The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.

ADD: The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.

COPY: The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

ENTRYPOINT: An ENTRYPOINT allows you to configure a container that will run as an executable.

VOLUME: The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.

USER: The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.

WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

ARG: The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the –build-arg <varname>=<value> flag.

ONBUILD: The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build.

STOPSIGNAL: The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit.

HEALTHCHECK: The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working.

SHELL: The SHELL instruction allows the default shell used for the shell form of commands to be overridden. Next, we are going to create a docker volume and attach it to a container.

Next, we are going to build a docker image using Dockerfile.

Dockerfile Demo:

Hope you have enjoyed this article. In the next blog post, we will discuss docker compose.

To get more details on docker, please refer below docker documentation


Leave a Reply

Close Menu