Saturday, May 22, 2021

Creating your Custom Image using Dockerfile

How do you create docker image?

There are multiple ways to create new docker images

  • docker commit: creates a new layer (and a new image) from a running container.
  • docker build: performs a repeatable build sequence using dockerfile.
  • docker import: loads a tarball into Docker engine, as a standalone base layer.

We don't prefer to use docker import, because the import option can be used for various hacks, but its main purpose is to bootstrap the creation of base images.

Working on Docker Custom Image

Docker images can be pulled from Docker registries. Docker owns Docker Hub and Docker cloud public repositories where you could find the free images. Docker store is a place where Docker images could be on sale. You can create an image and sell it who need it. Docker client is CLI which allows us to deal with the Docker objects. Docker objects are Docker images, Containers, Network, volumes and services. An image is a read-only template that allows us to create instances in runtime containers.

Dockerfile structure



We can create an image from a base image by adding some customizations like installing new libraries, software. For example, using oraclelinux as base image on top it installs httpd.
Here base image could be available in the public registry or custom image build for your project.

The easiest way to customize our docker image using Dockerfile, Where Dockerfile includes instructions to create layers in your docker image. Let's explore how to build our own image with dockerfile with syntax

How to create an efficient image via a Dockerfile?


  • Start with an appropriate base image
  • Do NOT Combine multiple applications into a single container
  • Avoid installing unnecessary packages
  • Use multi-stage builds 


Dockerfile format

The dockerfile is a construct of a set of instructions to build the docker image. The dockerfile starts with comments where every scripting language allows us to use '#'(hash symbol) to comment a line. Next base image FROM where the image can be pulled. Here we have two options Docker Private registry for your organization otherwise Docker Hub public cloud which can be used for the initial learnings or for Proof of Concepts to built.
Create a Dockerfile in any editor, my preferrable editor is VisualStudio Code, which gives me nice syntax highlighting for Dockerfile instructions.
# This is first sample Dockerfile
FROM busybox
LABEL AUTHOR Pavan Devarakonda
RUN echo "Building sample docker image"
CMD echo "Hello Vybhava Container!"

To create the docker image following command:
docker build -t hello .
Here the important thing is that Dockerfile don’t follow any root directory you need to specify at the end of the build command in the above we have used dot(.) that is the current directory. You have other option too you can define other PATH as per your needs of project team collaboration.
Docker build command best options
docker build -f /path/to/a/Dockerfile . #absolute path
docker build -t vybhava/myapp . #relative path of Dockerfile
docker build -t vybhava/myapp:1.0 . # with tag label value

Usage of .dockerignorefile


To increase the build performance, exclude files and directories by adding a .dockerignore file to the context directory.
 *.md
 !README.md
 *.tmp

Escape Parser Directive


Exploring Docker instructions 

Let's examine each command in detail how it works.
 

FROM Instruction

The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. Let's see an example
# This is first sample Dockerfile
FROM ubuntu
CMD ["/usr/bin/wc","--help"]
Ref: https://docs.docker.com/engine/reference/builder/#from
 

COPY and ADD instructions

copies all files and folders from current directory to /app folder in container-Image
COPY . /app 

copies everything from the web folder to container-Image /app/web
COPY ./web /app/web

copies specific file to /data/my-sample.txt file inside container-Image
COPY sample.txt /data/my-sample.txt

Compressed files such as tar, tar.gz, gz or zip files when copying to container-Image it will uncompresses it which can be any format.
ADD jdk-8u241-linux-x64.tar.gz /u01/app
ADD APEX_21.1.zip /u01/

You can add files from the url to a container Image
ADD http://github.com/sample.txt /data/

EXPOSE

The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. https://docs.docker.com/engine/reference/builder/#expose 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. 

Reference : https://docs.docker.com/engine/reference/builder/#label
 

RUN instruction

RUN has 2 forms:
RUN  (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN ["executable", "param1", "param2"] (exec form)
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. 

Reference: https://docs.docker.com/engine/reference/builder/#run

 

ENV instruction


The ENV instruction sets the environment variable 'key' to the value "value". Example:
ENV JDK_DIR /tmp
ENV myvar=/home/tomcat
ENV myvar=/home/apache2 var2=$myvar
ENV var3=$myvar

The environment variables set using ENV will persist when a container is run from the resulting image.
 

WORKDIR instruction

Once the container comes to running state will switch to the directory which you mentioned as WORKDIR instruction. The value you pass it to WORKDIR is a PATH that should be existing in the container.
 
Example: WORKDIR /tmp

ENTRYPOINT and CMD instructions

The ENTRYPOINT and CMD both the commands are executed run-time that is Container startup time. We cannont have multiple ENTRYPOINT or CMD commands in a Dockerfile

An ENTRYPOINT allows configuring a container that will run as an executable.

ENTRYPOINT has two forms:

   ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
   ENTRYPOINT command param1 param2 (shell form)


Example1

ENTRYPOINT with CMD params

   
# This is Entrypoint sample dockerfile
FROM alpine
LABEL MAINTAINER BHAVANISHEKHAR@GMAIL.COM
ENTRYPOINT ["ping"]
CMD ["-c","4","www.docker.com"]
Build the image from the above instructions:
 docker build -t entrycp:1.0 -f entrycmdparm.Dockerfile .

#Run the container

docker run entrycp:1.0

Example 2:

ENTRYPOINT param with CMD param

   
# This is Entrypoint sample dockerfile
FROM ubuntu
LABEL USER Pavan
ENTRYPOINT ["top", "-b"]
CMD ["-c"]

dockerfile is a blue-print for docker image created with instructions

No comments:

Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)