Docker file
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
users can create an automated build that executes several command-line instructions in succession.
Some of the Keyword's definitions
FROM
is define th base image on which we are building eg FROM ubuntu
ADD
is used to add the files to the container being built, ADD <source> <destination in the container>
RUN
is used to add layers to the base image, by installing components.Each RUN statement add a new layer to the docker image
CMD
is used to run the command at the start of the container. These commands run when there is no argument specified while running the container.
ENTRYPOINT
is used to strictly run the commands the moment the container initializes. The difference between CMD and ENTRYPOINT is, that ENTRYPOINT runs irrespective of the fact that whether the argument is specified or not.
ENV
is used to define the environment in a container.
docker build
Description
Build an image from a Dockerfile
sudo docker build -t nonrootimage . # create custom image (nonrootimage)
Examples.
Create an image which has base image ubuntu and apache2 is to be installed on it and create an index.html file in current directory, all the files from the current directory is to be copied to /var/www/html folder. Once the container is started it should run the apache service and also create one environment variable called "name" and it should have value "DEVOPS
#base Ubunutu image, manadatory command
FROM ubuntu
#ARGRUMENT it will not ask any question
ARG DEBIAN_FRONTEND=noninteractive
#Run command is used to add an instruction layer on top of base image layers
#Adding apt update instructions on top of ubuntu base image
RUN apt-get update
#Adding apache software on top of ubuntu base image
RUN apt-get -y install apache2
#ADD command will copy test.html(create test.html file) from local current directory
#to docker image /var/www/html
ADD test.html /var/www/html
#ENTRYPOINT when a container is started with this image then it will first run
# the command which written in front of EntryPoint
# below command starts apache in Foreground
ENTRYPOINT apachectl -D FOREGROUND
#ENV is to setup Enviornment variable
ENV name DEVOPS
Run the below command to build the image
docker build . -t img1 #Created the image from the above Dockerfile
Example 2
Create a Docker file that uses a base image of CentOS 7 and create a user john and change to non-root privilege
# Base image is CentOS 7
FROM centos:7
# Add a new user "john" with user id 8877
RUN useradd -u 8877 john
# Change to non-root privilege
USER john
Example 3
#Eample of COPY and ADD
#Base Image
FROM centos:7.4.1708
#Create a directory in the image mydata
RUN mkdir /mydata
# Copy myfiles (should be created under current folder ) in /mydata
COPY myfiles /mydata/myfiles
# Copy myfile2 (should be created under current folder ) in /mydata
ADD myfile2 /mydata/myfile2
# Download the file using ADD command and copy under /mydata
ADD https://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz /mydata
# ADD command will unzip the file and create under /mydata/maven dir
# wget https://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/source/apache-maven-3.6.3-src.tar.gz
ADD apache-maven-3.6.3-src.tar.gz /mydata/maven
# CMD and ENTRYPOINT
#base image
FROM ubuntu
# When a container get started from this image
# it will print Hello World
# CMD execute the commands but if we pass command line argument
# then echo "Hello World" will not execute and command line argument
# command get executed
CMD echo "Hello World"
docker build . -t img1 #Created the image from above Dockerfile
docker run -it img1 # it will return Hello World
docker run -it img1 echo "Hello India" # it will overwrite the CMD and Print Hello India
EntryPoint Example
#base image
FROM ubuntu
# When a container get started from this image
# it will print Hello World
# ENTRYPOINT execute the commands once container get started
# it will not consider the command line arguments
ENTRYPOINT echo "Hello World"
docker build . -t img1 #Created the image from above Dockerfile
docker run -it img1 # it will return Hello World
docker run -it img1 echo "Hello India" # it will not overwrite the ENTRYPOINT and Print Hello World
Note:- if the file name is not Dockerfile
docker build . -f abc -t img8 # abc is the file name which represents the dockerfile contents
Docker MultiStage file
Whenever you create a docker image then the size of docker image matters, You need to create a docker image with minimal size without compromising the functionality of the docker image
In the below example first, a docker image is created without a multistage Dockerfile, and later a multistage docker file will be created which reduces around 1/5 of the docker image size.
Step1:-
Download a sample git maven project for this example
git clone https://github.com/onlineTrainingguy/jenkinscicd.git
Step2:-
goto jenkinscicd dir
cd jenkinscicd
Step3:-
Modify Dockerfile content as given below
#base image is maven because this code is maven code
FROM maven:latest
# WORKDIR / makes root directory as pwd
WORKDIR /
# it will copy the src folder from current host directory to image /src
COPY src /src
# it will copy pom.xml file from current folder to / folder of image
COPY pom.xml /
# maven command to create a jar file under target folder in docker image
RUN mvn clean package
# CMD will execute jar file which is created in above step (Log entry)
CMD java -cp /target/myproj-1.0-SNAPSHOT.jar com.raman.App
Step4:-
Build the image using docker build command
docker build . -t myprojimg
Step 5:-
Check the image size it will approximate 700 MB+
docker images myprojimg
Step 6:-
Create container with above image and check the output
docker run myprojimg
output will be
Maven Job is running on Build Server!
Now lets create multi stage Docker file
Step 1: Modify Dockerfile content as given below
# Stage 1 or build stage to build the jar file
#base image is maven because this code is maven code
FROM maven:latest as build
# WORKDIR / makes root directory as pwd
WORKDIR /
# it will copy the src folder from current host directory to image /src
COPY src /src
# it will copy pom.xml file from current folder to / folder of image
COPY pom.xml /
# maven command to create a jar file under target folder in docker image
RUN mvn clean package
# Final Stage. because we have the jar file in build stage
# using openjdk:alpine image (light weight openjdk image)
FROM openjdk:alpine
# copy the jar file from build /target/myproj-1.0-SNAPSHOT.jar dir to /myproj-1.0-SNAPSHOT.jar
COPY --from=build /target/myproj-1.0-SNAPSHOT.jar /myproj-1.0-SNAPSHOT.jar
# Execute the jar file in the container
CMD java -cp /myproj-1.0-SNAPSHOT.jar com.raman.App
Step 2:- Build image using docker build command
docker build . -t multistageimg
Step 3:- Check the image size it will approximate 100 MB
docker images multistageimg
Step 4:-Create container with above image and check the output
docker run multistageimg
The outcome will be the same but the image size is reduced
Comments
Post a Comment