0% found this document useful (0 votes)
9 views10 pages

Self Docker Notes Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Self Docker Notes Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DOCKER

Every Software application consist of 3 layers


i) Front End ii) Back End iii) Database

Front End : refers to the UI. It is the front end with which user interacts
It can be developed by the use of html/css/react js/angular js

Back End : It represents the Business logic .


It is the backend which interacts with the Database
Backend can be implemented in any language such as python,php,node js.

Database : Data base is used to store the application data.


Available databases are MySQL,Oracle,Microsoft SQL Server etc.

Developer writes the code for the application and Devops Engineer is responsible for deploying or
executing the application.To deploy an application he needs the following enviornmental setup
.

a) Take 1 computer or machine


b) Install React JS version 16
c)Install live server
d) Install java version 17
e) Install tomcat version 9
f) Install Oracle version 12.

MACHINE

Application Code
_____________________

React JS v16
live Server
Java v17
Tomcat v9
Oracle v12
_________________________

Operating System

Once all the above dependencies installed then only the Appliction will be executed.

Developer develops the code . Now the code has to be tested in different Application Environments

1) DEV Enviornment 2) SIT Env 3) UAT envio 4)Pre Prod envio 5) Prod enviornment

In each enviornment DEVOPS engineer need to take on computer and install the above
dependencies with the correct version.

Dev Enviornment : The developers after developing the code ,test the units code to check whether it
is working in the desired manner or not. After testing the code in DEV environment we go next
with SIT Enviornment

SIT Enviornment: In our Project the software testing team is their to carry out the System
Integration Testing.
Here also the same enviornmental set up is done.

UAT enviornment:(User Acceptance Testing): Here the Client test the code wheteher the code is
fulfiling all the requirements or not. (It is called to be the test drive for the client which is very
similar to the test drive of a vehicle when we visit showroom to purchase the bike or the car)

PreProd enviornment: A final testing of the application has to be carried out before it goes for the
Production Enviornment

Production Enviornment: When the application is ready to be used by the client.

Devops Engineer must deploy the application in different enviornments or it is duty of devops
engineer to install the machine with the required depenedencies in all the above enviornments.

Let us consider the scenarion in which the DEVops enginner in SIT enviornment installed java
version 12 instead of 17
then the code will not work . The same code that runs in DEV envirnment does not run in the SIT
enviornment .These issues are called as enviornmental issues often comes when we install the
dependencies manually from one enviornment to another.

To execute our application in all the enviornments it is necessary to install the correct versions of
the dependencies .If manually it is donethen there is the chance of human error or mistakes that
disturb or halt the application execution.

To overcome the problem Containerization or DOCKER comes into picture.

Docker is a containerization software that simplifies the execution of the project in multiple
enviornments.no need to install the required dependencies’ manually if we use docker then docker
itself installed the required dependencies.

Containerization : Packaging code + dependency together to create an Docker Image (Application


+ Code Dependency)
To execute the application in different environments we actually deploy the image .
Image run inside the container and all the necessary dependencies are installed automatically.

Who create the container


Who run the image
Who install the dependencies

Answer is DOCKER.
1) Dockerfile : It contains set of instructions to download dependencies and build docker image

2) Docker Image : It is a package which contains code + required dependencies

3) Docker Registry : It is a place where we can store our Docker Images

4) Docker Container : When run docker image it will run Our application inside Container.

Steps to Install Docker in the AWS Linux:

1) In order to update the existing packages in the Linux

sudo yum update -y

2) Install the Docker Software

sudo yum install docker

3) Verify the docker installation


docker -v

4) Start the service of the docker

sudo service docker start

5) After the docker service starts type docker info


command in order to get information about the docker

6) The command will not work because first of all we have to make ec2 user as a part of the docker
group

7) Add ec2-user to the Docker Group


sudo usermod -aG docker ec2-user
8) Type exit and restart the session again

9) Now again type docker info it will run.


Docker Commands
==================

# To display docker images available in our machine


$ docker images

# To display running docker containers in our machine


$ docker ps

# To display stopped & running containers


$ docker ps -a

# To download docker image


$ docker pull <image-name>

# Run docker image to create docker container


$ docker run <image-name>

# Run docker container in detached mode


$ docker run -d <image-name>

Note: Detached mode will help us to run other commands in same tab

# Display Docker container logs


$ docker logs <container-id>

# Stop Running Docker Container


$ docker stop <container-id>

# Start Docker container which is stopped


$ docker start <container-id>

# Delete Docker container which is in stopped state


$ docker rm <container-id>

# Delete Running Docker container


$ docker rm -f <container-id>

# Remove Docker Image


$ docker rmi <image-name/image-id>

# Remove all stopped containers + unused docker images


$ docker system prune -a

Work with the Docker Hub

https://hub.docker.com/
In Docker Hub the default image is hello-world
In order to fetch the image in AWS Linux Machine type the following Command
a)docker pull hello-world

It run the docker image hello-world inside the container


b)docker run hello-world

The message generated shows the communication process between the docker daemon and the
docker client

Running docker container with port mapping


A spring boot java application runs inside the Tomcat Server port no 8080

Docker Image Name : azat90210/docker_desktop_page

Pulling the Docker Image from the Docker Hub

$ docker pull azat90210/docker_desktop_page

Running the Docker Image inside the container


$ docker run -p 8080:8080 -d azat90210/docker_desktop_page

Note: In the above command we are mapping container port to host port to
access application outside of the container.

=> After running application with above command we need to enable host port
in security group inbound rules.

=> Access application using below URL syntax

URL : http://public IP address: PortNo


Docker File:Dockerfile contains set of instructions to build docker image

=> To write Dockerfile we will use below keywords

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

1) FROM: It refers to the base image from which our image is created
In From we specify the software along with its version that are needed to execute the application.
(for example: In order to run the java application, I need jdk1.8
Similarly i also need mysql server version 10.1)
FROM python:3.6
FROM jdk:1.8

2)MAINTAINER:specify the author of the Docker File


MAINTAINER SumitMiglani<[email protected]>

3)RUN:is used to execute the instructions when the docker image is creating
for example before the build happens I want to download the entire repository from the GIT HUB
by typing the command :RUN 'git clone <url of the repository>'

or I want to compile the code using maven


RUN 'mvn clean package'

Note: The instructions to be executed during the docker image creating must
be written after the RUN and must be enclosed in single colon.

We can write multiple RUN instructions in the Docker file


They are executed in the serial order.

4)CMD: instructions are executed when the docker image is runnning inside the
docker container or when the docker container is creating.
My Spring Boot application is ready and I want to exeute the jar file or move war file to tomcat etc...

CMD "java-jar sb-app.jar"


Note: if you write the multiple CMD instructions then DOCKER will execute only last CMD
instructions.

docker images
It will display ubuntu image

To run CMD type docker run myImage1 only last cmd instructions will be executed.

5)COPY:It is used to copy the files from host machine to container machine

Ex:

COPY target/java-app.jar /usr/app/

COPY target/webapp.war /usr/app/tomcat/webapps/

COPY python-app.py /usr/app/

6)ADD command
=============

=> It is used to copy the files from host machine to container machine

Ex:

ADD target/java-app.jar /usr/app/

ADD target/webapp.war /usr/app/tomcat/webapps/

ADD python-app.py /usr/app/

Note: ADD command support http url for source file

ADD <url> /usr/app

7) WORKDIR Keyword

It is used to specify working directory location to process further docker file instructions

EX:

WORKDIR /usr/app
8)EXPOSE Keyword
===============

It is used to specify on which port number our container will run

Ex:

EXPOSE 8080

Note: By using EXPOSE keyword can't modify docker container port (it is just to provide
information).

9)ENTRYPOINT
It is used to run our application code in docker container

Ex:

ENTRYPOINT ["java", "-jar", "app.jar"]

ENTRYPOINT ["python", "app.py"]

10)USER keyword

It is used to specify the USER account to execute dockerfile instructions

Ex:

USER root

USER ec2-user

11)ARG Keyword

It is used to pass dynamic values

EX :

ARG branch-name

RUN sh 'git clone -b $branch-name <repo-url>

12)VOLUME keyword
It is used to specify DOCKER volume for storing the data

Ex:

VOLUME mysql-db-data

=====================
Sample Dockerfile
====================

Create Dockerfile with below command

$ vi Dockerfile

=> Keep below content in Dockerfile

FROM ubuntu

MAINTAINER SumitMiglani<[email protected]>

RUN echo 'hi'

RUN echo 'hello'

CMD echo 'cmd - 1'

CMD echo 'cmd - 2'

Save the file and close it

Build Dockerimage using above Dockerfile

$ docker build -t <image-name> .

$ docker images

$ docker run <image-name/id>

===============================================
How to push Docker Image to Docker Hub Account
===============================================

# login into docker hub account


$ docker login

# Tag docker image

$ docker tag <image-name> <tag-name>

Ex: docker tag myimg123 sumit/myimg123


$ docker images

$ docker push <tagname>

Can we change Dockerfile name ?

The default name for Docker File is "Dockerfile" (no extension)

We can change docker-file name then we have to pass that file name in build command like below

$ docker build -t <img-name> -f <file-name> .

You might also like