Understanding of Dockerfile and its commands?

We have used Docker images to create containers and we pull the images from Docker Hub to create containers. But in this article,  we will see how docker images are creating and what commands are using as part of this process. Docker can build images automatically by reading the instructions from a Dockerfile.

Image title

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 -t my-image 

If your Dockerfile is placed in another path,

 $ docker build -f /path/to/a/Dockerfile 


Let's first look at below Dockerfile and see those commands.

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build

WORKDIR /src

COPY MyMicroservice.csproj .

RUN dotnet restore

COPY . .

RUN dotnet publish -c release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:5.0

WORKDIR /app

COPY --from=build /app .ENTRYPOINT ["dotnet", "MyMicroservice.dll"]


Dockerfile Commands

  •  FROM - specifies the base(parent) image. Alpine version is the minimal docker image based on Alpine Linux which is only 5mb in size.

  •  RUN - runs a Linux command. Used to install packages into container, create folders, etc

  •  ENV - sets environment variable. We can have multiple variables in a single dockerfile.

  •  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.

There are a few commands which are little confusing. Let's have a look at them.

COPY vs. ADD

Both commands serve a similar purpose, to copy files into the image.

  •  COPY - let you copy files and directories from the host.

  •  ADD - does 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 specifying a command. If a 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.

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.

Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?