Tuesday, July 25, 2023

What is Dockerfile ?

What is Dockerfile ?


A Dockerfile contains set of instructions to build a docker image.

-In a Dockerfile we will use DSL (Domain Specific Language).
-Docker Engine will read Dockerfile instructions from top to bottom to process.
-In Dockerfile we will use below keywords:

1) FROM
2) MAINTAINER
3) COPY
4) ADD
5) RUN
6) CMD
7) ENTRYPOINT
8) ENV
9) ARG
10) WORKDIR
11) EXPOSE
12) VOLUME
13) USER
14) LABEL

1.FROM
=> It represents base image to create our docker image

Syntax:

FROM java:1.8
FROM python:1.2
FROM mysql:8.5
FROM tomcat:9.5

2. Maintainer
=> It is used to specify docker file author information

Syntax:

MAINTAINER Qader <qader@oracle.com>

3. COPY
=> It is used to copy the files from source to destination while creating docker image

Syntax:
COPY <SRC> <DESTINATION>
Ex: COPY target/sb-api.war /app/tomcat/webapps/sb-api.war

4. ADD
=> It is used to copy the files from source to destination while creating docker image

Syntax:
ADD <SRC> <DESTINATION>
ADD <HTTP-URL> <DESTINATION>
Ex: ADD <url> /app/tomcat/webapps/sb-api.war

Difference between COPY and ADD command ?

COPY : It can copy from one path to another path with in the same machine.
ADD : It can copy from one path to another path & it supprts URL also as source.

5. RUN
=> RUN instructions will execute while creating docker image

Syntax:

RUN yum install git
RUN yum install maven
RUN git clone <repo-url>
RUN cd <repo-name>
RUN mvn clean package

Note: We can write multiple RUN instructions, docker engine will process from top to bottom.

6.CMD
=> CMD instructions will execute while creating docker container
=> Using CMD command we can run our application in container

Syntax:
CMD sudo start tomcat
CMD java -jar <jar-file>

Note: We can write multiple CMD instructions but docker engine will process only last CMD instruction.

Difference between RUN and CMD ?

=> RUN instructions will execute while creating docker image
=> CMD instructions will execute while creating docker container
=> We can write multiple RUN instructions, docker engine will process from top to bottom.
=> We can write multiple CMD instructions but docker engine will process only last CMD instruction.

Note: There is no use of writing multiple CMD instructions.

7. Sample Docker

FROM ubuntu
MAINTAINER Ashok<ashok.b@oracle.com>
RUN echo "Hi, i am run - 1"
RUN echo "Hi, i am run - 2"
RUN echo "Hi, i am run - 3"

CMD echo "Hi, i am CMD-1"
CMD echo "Hi, i am CMD-2"
CMD echo "Hi, i am CMD-3"

8.ENTRYPOINT
=> It is used to execute instructions while creating container

Synatx:
ENTRYPOINT [ "echo" , "Container Created Successfully" ]
ENTRYPOINT [ "java", "-jar", "target/springboot.jar" ]

Difference between CMD and ENTRYPOINT ?

=> CMD instructions we can override while creating container
=> ENTRYPOINT instructions we can't override

9. WORKDIR
=> It is used to specify working directory for image and container

Syntax:
WORKDIR /app/usr/

10. ENV
=> ENV is used to set Environment Variables

Ex:
ENV java /etc/softwares/jdk

11.EXPOSE
=> It is used to specify on which port number our docker container will run

EX:
EXPOSE 8080

11. ARG
=> By using ARG we can take dynamic values from CLI
=> It is used to remove hard coded values in Dockerfile

Ex:
ARG branch
RUN git clone -b $branch <repo-url>
$ docker build -t <imagename> --build-arg branch=master .


12. USER
=> It is used to specify username for creating image / container

Ex:
USER dockeruser

13. VOLUME
=> It is used to specify docker volume storage location
=> Volumes are used for storage purpose


14. LABEL
=> It is used to add METADATA to docker objects in key-value format

Ex:
LABEL name="sbi_image"

No comments:

Post a Comment