Monday, 8 July 2019

Docker Tutorial

Docker is a container management service. The keywords of Docker are develop, ship and run anywhere


Docker containers works on top of virtual machine. Docker container are lightweight alternatives of virtual machine, we need not pre allocate any RAM or disk space it will be allocated as per the requirement. It is easier to create deploy and run application using Docker container.

1.      Docker file build a Docker image that contain all the project code.
2.      By running the Docker image, we can create as many container as we needed.
3.      Docker image will uploaded into Docker hub; anyone can pull the Docker image from Docker hub and create the container.
4.      Docker hub is a Docker image repository, we can pull public.


Install Desktop Docker –

1.      Download Docker from https://docs.docker.com/docker-for-windows/install/

2.      After installation run Desktop Docker or run Docker from comman systemctl start docker and then run below command to check Docker running status


Run below command to fetch all running images in Docker- docker images


     Run below command to check the status of all running container - docker ps -a


   To remove existing Docker container- docker rm <DOCKER_ID, DOCKER_ID>
   To remove all stopped container- docker container prune
    Remove Docker image from local repository - docker rmi <image_id>


Deploy the spring boot application in Docker

Created a Spring boot application test-main
Create executable jar of test-main, use below gradle to create jar
plugins {
    id 
'java'
    
id 'application'
    
id "org.springframework.boot" version "2.0.0.RELEASE"
}
version = '1.0'
group 'com.mk'
mainClassName = 'com.mk.SpringBootConfig'
sourceCompatibility = 8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    // https://mvnrepository.com/artifact/io.reactivex/rxjava
    compile group: 'io.reactivex', name: 'rxjava', version: '1.0.2'
    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.6.RELEASE'
    compile  group: 'se.transmode.gradle', name: 'gradle-docker', version: '1.1'
}

jar {
    manifest {
        attributes 'Main-Class': 'com.mk.SpringBootConfig'
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Create a manifest.txt file in project folder. File content –

Manifest-Version: 1.0
Created-By: Me
Main-Class: test-main

·         Create a DockerFile with below content-
FROM java:8
WORKDIR /
ADD ./build/libs/TestMain-1.0-SNAPSHOT.jar TestMain-1.0-SNAPSHOT.jar
EXPOSE 8080
CMD java - jar TestMain-1.0-SNAPSHOT.jar.jar
·         Login to docker with command – docker login
·         Run command to create image-  docker build  -t  <Image_Name>:<Tag-ID> <project-folder>
·         Run the Docker container docker run -d -P --name static-site test-main:test-01
<In above command –d will detach our terminal –p will publish all exposed port to random port 
and –name we can specify the software name.>
·         Run below command to check the port
docker port static-site – the output of this command is 
8080/tcp -> 0.0.0.0:32768
·         Now run the spring boot application http://localhost:32768/<app-path>

Now run docker ps -a to check the container status
Lets create another container and deploy the same application:-
Refer below snippet-
Now same application is running on two different port;-
http://localhost:32768/hello
http://localhost:32769/hello


Download the spring boot app
and Docker file from below URL




3 comments: