Showing posts with label dockerfile. Show all posts
Showing posts with label dockerfile. Show all posts

Saturday, March 12, 2022

EXAMPLES: Dockerfile MultiStage file




Docker MultiStage file

THIS IS UNI STAGE DOCKER FILE

Step1:-
Download a sample git maven project for this example

    git clone https://github.com/onlineTrainingguy/jenkinscicd.git

Step2:-
go to jenkinscicd dir

    cd jenkinscicd

Step3:-
Modify Dockerfile content as given below

    FROM maven:latest
    WORKDIR /
    COPY src /src
    COPY pom.xml /
    RUN mvn clean package
    CMD java -cp /target/myproj-1.0-SNAPSHOT.jar com.raman.App



Step4:-
Build the image using docker build command

    docker build . -t unistagemavenimg

Step 5:-
Check the image size it will approximate 700 MB+

    docker images unistagemavenimg

Step 6:-
Create container with above image and check the output

    docker run -it unistagemavenimg

output will be
Maven Job is running on Build Server! (but if you following this steps, you will get a different output. why? because in jenkinscicd/src/main/java/com/raman/App.java, the app.java file has a different output. you can check by vi into it and even edit it to your liking!)



Now lets create multi stage Docker file
Step 1: Modify Dockerfile content as given below (notice the AS build)

    FROM maven:latest AS build
    WORKDIR /
    #copy from localfile system to docker file system
    COPY src /src
    COPY pom.xml /
    RUN mvn clean package

    #(if above Dockerfile haven't been run before, include below as well) 
    CMD java -cp /target/myproj-1.0-SNAPSHOT.jar com.raman.App

    FROM openjdk:alpine
    COPY --from=build /target/myproj-1.0-SNAPSHOT.jar /myproj-1.0-SNAPSHOT.jar
    EXPOSE 8080
    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

output will be same but the image size is reduced
Maven Job is running on Build Server!


Thursday, March 10, 2022

EXAMPLES: Dockerfile Copy Add Entrypoint CMD

 


DOCKERFILE COMMANDS:

Run below command to build the image:

docker build . -t <nameofimage>

To run the image in a container:

    docker run container --name c1 -dit img1

To go into the container:

    docker exec -it c1 bash

to run without container
    
    docker run -it img1

if the file name is not Dockerfile but something else such as abc then use -f:

    docker build . -f abc -t imgnodockfile

Thursday, February 24, 2022

Dockerfile

 




DOCKERFILE (on top of docker image, there is an instruction layer)

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.

vi Dockerfile (no extensions)

FROM ubuntu #downloading base

ARG DEBIAN_FRONTEND=noninteractive 
#noninteractive for automation so it does not ask geographical bla bla bla..

RUN apt-get update 
#run is adding instruction on the instruction layer

RUN apt-get -y install apache2

ADD . /var/www/html 
#add is copy the file from host machine to docker image folder

ENTRYPOINT apachectl -D FOREGROUND 
#entrypoint whenever docker image is used in a container, some commands need to be executed automatically

ENV name DEVOPS 
#environment variable is used on the global level






BUILD THE DOCKERFILE

docker build . -t webimg



when doing this, docker will create an intermediate container to try each steps. after each steps, it will destroy the intermediate container and go to next step, then create an intermediate container again.

run the build image in a container:


docker container run --name webserver -dit -p 80:80 prod/webimg

from here you can do the following:

A) check if FROM ubuntu is confirmed:
go into the container:
    docker exec -it webserver bash
then run this command:
    uname -a
(this will confirm the ubuntu)

B) check if RUN is confimed:
in container, type:
    service apache2 status

C) check if ADD . is confirmed:
in container, type:
    ls /var/www/html/

D) check if ENV name DEVOPS is confimed:
in container, type:
    env


Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...