Table of Contents
ToggleDocker file is basically used for automating image building.
This is done via all details provided to a file which is known
as Docker file.
It is very useful for building images. You don’t need manual
intervention to build images, upgrade it etc.
You can write it anywhere in the system where you have access.
You must ensure that file of name must be “dockerfile”. It has
only two things, one is called parameters and what it’s value
which needs to be defined in the dockerfile.
# cat dockerfile
FROM centos:8
MAINTAINER somethingtostudy12@gmail.com "it’s a practice image"
COPY run.sh /tmp
# docker build -t varelite1 .
[+] Building 2.7s (7/7) FINISHED docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from dockerfile 0.0s
=> => transferring dockerfile: 134B 0.0s
=> [internal] load metadata for docker.io/library/centos:8 2.7s
=> [internal] load build context 0.0s
=> => transferring context: 25B 0.0s
=> [1/2] FROM docker.io/library/centos:8@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 0.0s
=> CACHED [2/2] COPY run.sh /tmp 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:8fc9d16d5e48eb062ee77ba6045c7421bfbb9e31fae0bd33565ea02b6b10b39e 0.0s
=> => naming to docker.io/library/varelite1
Let’s have a look to some details of Parameters used.
There are parameters in dockerfile which are mandatory, few are
defined only once while few can be defined multiple times. Few
parameters are defined by its positions and you can’t change
its positions.
FROM: This is mandatory parameter and It always contains base
image reference to building new images.
FROM registryURL/ImageName
Or
FROM centos:8 (It will directly fetch image from docker image)
RUN: You can go ahead and define some commands you want to
execute to while provisioning new images. If you will run it
multiple times then it will increase your commit layer.
RUN dnf install httpd –y
RUN cp test.html /var/www/html/
To avoid multiple commits you can do the above things in single
RUN by defining commands using && symbol.
RUN dnf install httpd –y && cp test.html /var/www/html/ \
&& cp /etc/fstab /root/
COPY: Copy also do same thing like ADD Parameter. This can't
download from internet, only add from localhost (local docker
machine). Basically, you have some data which is not part of
image and we need keep that data in image after building.
In simple word, we need to copy data from local machine to
image file system. You can’t use wild card in COPY, you need
to use ADD parameter.
COPY src*local* destination *Image Path*
ADD: if you want to copy some files into image you can go ahead
and download content from the internet. It also supports
d-compression method while *COPY* parameter can’t do. ADD can
perform tasks similar to COPY as well.
WORKDIR: To change the present working directory in the
container. Default directory will be the root working
directory. If you want to make sure that your task is done in
your required working directory. It sets the working directory
permanent in dockerfile until you change it to back. If you
will use “cd” command using RUN parameter then it will perform
next task but after that path will change back to default root
directory.
WORKDIR /home/varelite
ENV: You can declare some variables inside the docker file and
it’s limited to docker file only.
ENV APP /home/varelite/app/data
Or
ENV PROXYIP 191.168.10.100
(And if you want to use that variable in dockerfile somewhere, you can use as show below:)
ENV $(APP)
EXPOSE: You can define port as per your requirement in
container to access your application.
If you run a docker container with Dynamic Port forwarding then
EXPOSE parameter is the thing in image which automatically
exposed the internal ports of image to outside word. If you
don’t have any EXPOSE ports in dockerfile then Dynamic Port
forwarding will not work.
EXPOSE 8080 8081 8088
CMD: When we want any process/service daemon to start
automatically when you provision the container. This can't be
used multiple time in a single docker file. If accidentally you
have defined it multiple time then last CMD command will
execute.
CMD Command
The major difference between RUN and CMD is that RUN will
execute command at build time only. There is no role of CMD at
build time. Once image has created and when you are creating
container from that image then the commands mentioned in CMD
will run. Here RUN has no role.
CMD [“command”, “paramter1”, “parameter2”] # Command Mode
CMD command parameter # Shell Mode
Shell mode execution is slow than command mode.
ENTRYPOINT: This is basically used to execute shell file at the
time of creation/booting time of container. We can't call this
multiple time.
ENTRYPOINT Command
ENTRYPOINT [“ping”, “127.0.0.1”]
ENTRYPOINT can also be written in two mode which are command
and shell mode.
When we will start container from that image, It will run the
command defined in image using ENTRYPOINT. The major difference
between CMD and ENTRYPOINT is that when we will start any
container with the some commands then it will discard the
command defined by CMD in dockerfile but you have mentioned
via ENTRYPOINT then it will not be overwritten by command put
during container creation.
ENTRYPOINT understand CMD as argument so you can use them as
mentioned below in Dockerfile.
ENTRYPOINT [“ping”]
CMD [“localhost”]
Now if you will start a container using the dockerfile having
above content then it will ping to localhost which can be
overwrite by command provided during container creation.
You can’t use CMD before ENTRYPOINT.
MAINTAINER: These can be defined to put some information so
that someone can see and understand some details, its Optional
parameter (Email, author name and Department).
USER: Default Container namespace ownership user and login user
also when access container when we start any container from an
image.
USER username
We can build non-privilege containers having access of normal
user which has required application access.
VOLUME: We create volumes to containers so that we have data
safety during container lost. Here we use VOLUME parameter to
create one volume automatically for the container and mounting
inside it.
VOLUME /var/www/html
We can’t define the name here it is not in our control. When
you will create any container with the image where you have
defined VOLUME parameter then it will automatically create a
volume and attach to defined location. But if you have defined
the already created volume to the same location which is
defined in dockerfile then it will not create volume during
container build.
ARGS: You can use input variable during image build time. It is
similar to ENV but the main difference is that when you pass
ENV it will get you permanent in container while ARGS is
available till building image.
ARGS GREET=varelite
Multi Stage Build: In this case, we can write two image build
in a single Docker file and you can name those stages. Below is
an example of that file.
FROM ubuntu as Build
RUN apt update –y && apt install apache2 -y
CMD [“/bin/bash”]
FROM Ubuntu as Prod
RUN apt update –y && apt install apache2 -y
RUN mkdir /test/testfile
CMD [“/bin/bash”]
Now when you will build image then you will define which you
image you want to build.
# docker build –t varelite . ---target=Build
And when you want to build Prod one then you can go ahead and
change target.
You can use Build data in Prod as well as
COPY –from=Build <Source from Build> <Destination on Prod>
Note: There are three parameters “RUN, ADD, and COPY” which
will create commit values as these parameters making some
changes.