Skip to main content

Dockerfile


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

Popular posts from this blog

Networking in Kubernetes

Pods that are running inside Kubernetes are running on a private, isolated network. By default they are visible from other pods and services within the same Kubernetes cluster, but not outside that network Every Pod has a unique IP address And it is reachable from all other Pods in the K8s cluster A pod is a host, just like your laptop, having an ip-address and a range of ports that can be alloted to containers A container runs on a specific port inside a pod In a Kubernetes environment, when services are deployed within the same namespace , they can communicate with each other using the service name as the hostname e.g. in the following snippet from appsetting.json form a .net core project, ' document-api'  is the name of the service "DocumentApiConfiguration" : { "BaseUrl" : "http://document-api/" } What if I want to access a service from another namespace? When you want to access a service from another namespace in Kubernetes, you typica...

Terraform

It is an Infrastructure as Code tool Normally, if one has to configure VMs or other resources on the cloud, they have to go to the cloud provider's website and click a lot to get things done as supposed, terraform can do all of that provided you tell it precisely what to do in a .tf file e.g. which cloud provider you are using(GCP, Azure etc), which resource to configure with what specifications. One writes the file in hashicorp language (kinda like JSON) Free and Open source One has to install the CLI terraform init terraform apply  (to make the changes to cloud) terraform destroy

Kubernetes

Some keywords: Node A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster Clusters Kubernetes coordinates a highly available cluster of computers (nodes) that are connected to work as a single unit Namespace Namespaces are a way to organize clusters into virtual sub-clusters — they can be helpful when different teams or projects share a Kubernetes cluster . Any number of namespaces are supported within a cluster , each logically separated from others but with the ability to communicate with each other Kubernetes: Kubernetes is a portable, extesible open-source platform for managing and orchestration containerized workloads . It abstracts away complex container management tasks Provides us with declarative configuration to orchestrate containers in different computing environments This orchestration platform gives you the same ease of use and flexibility you might already know from Platform-as-a-Service (PaaS) or Infrastruct...