A docker file is a text file that contains instructions on how to build a docker image, in other words a dockerfile is a set of instructions that builds up the docker image with the exact software you need in it to run your application - including your application itself
Dockerfile's contents start with Identify a base image (to which you add more files and configurations)
For example, if you have an ASP.NET application that you want to package into a container, Microsoft publishes an image called mcr.microsoft.com/dotnet/aspnet that already contains the ASP.NET runtime
The syntax for writing a Dockerfile and Format
- FROM
- A FROM statement defines which image to download and start from often called a base image.
- A Dockerfile can have multiple FROM statements which means the Dockerfile can produce more than one image.
- The specified base image is pulled from a container registry, like Docker Hub by default, unless otherwise specified.
- e.g. the follwing is using mcr.microsoft.com/dotnet/sdk as the base image and naming it as build,
- FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
- MAINTAINER
- This statement is a kind of documentation, which defines the author who is creating this Dockerfile or who should you contact if it has bugs.
- e.g.
- MAINTAINER Firstname Lastname <example@geeksforgeeks.com>
- RUN
- The RUN statement defines running a command through the shell, waiting for it to finish and saving the result. It tells what process will be running inside the container at the run time.
- e.g.
- RUN unzip install.zip /opt/install
- RUN echo hello
- WORKDIR
- WORKDIR sets the working directory for all the future Dockerfile commands
- e.g.
- WORKDIR /directory-name
- ADD
- Used to Copy
- e.g. ADD source_path destination_path
- source path is from where the docker while exists and destination path would be inside and of the image
- Used to copy contents of a tar archives
- e.g. ADD project.tar.gz /install/
- Used to download files via URLs
- e.g. ADD http://source.file/url /destination/path
- COPY
- Like Add, COPY is used for copying from a source folder to a destination folder
- Unlike ADD, COPY is only concerned about only the copying, i.e. no extracting tar content and no downloading via urls
- ENV
- ENV statement sets the environment variables both during the build and when running the result. It can be used in the Dockerfile and any scripts it calls. It can be used in the Dockerfile as well as any scripts that the Dockerfile calls.
- These are also persistent with the container and can be referred to at any moment
- e.g.
- ENV URL_POST=urlpost.example.com
- CMD
- Like ENTRYPOINT, CMD is also used to run commands at container startup
- If you use multiple CMDs in a single dockerfile then only the last one(the one at the bottom) runs
- If you use a CMD in combination with an ENTRYPOINT then both will be taken into account when running a container
- Can be overriden
- ENTRYPOINT
- The ENTRYPOINT instruction is used to configure the executables that run after the container is initiated
- It specifies the starting of the expression to use when starting your container.
- e.g. ENTRYPOINT ["dotnet", "Products.dll"]
- Can not be overriden
- EXPOSE
- EXPOSE statement maps a port into the container. The ports can be TCP or UDP but by default, it is TCP.
- e.g. EXPOSE 3030
- VOLUME
- The VOLUME statement defines shared volumes or ephemeral volumes depending upon whether you haev one or two arguments
- 1. If you have two arguments, it maps a host path into a container path.
- VOLUME ["/host/path" "/container/path/"]
- 2. If you have one arguments, it creates a volume that can be inherited by the later containers.
- VOLUME ["/shared-data"]
- ARG
- A variable that can be provided at build time is defined by an ARG Instruction. Once it has been specified in the Dockerfile, you can specify it using the –build-arg switch when creating the image. The Dockerfile supports multiple ARG instructions.
- The only instruction in the Dockerfile that can come before the FROM instruction is ARG.
- e.g.
- Dockerfile
- ARG image_name=latest
FROM centos:$image_name - docker command
- docker build -t <image-name>:<tag> --build-arg image_name=centos8




Comments
Post a Comment