Friday, May 24, 2019

Docker - getting started (very basics)


Docker-Overview

Docker makes much easier to deploy applications. It helps to resolve one main problem - “works in my machine problem”. 

Getting started
  • Download docker from here for your OS: 
  • Install the docker DMG if Mac.
  • Login to docker once it is installed. 
  • Open terminal and execute the following: 
    • Docker -version
    • Docker run hello-world
    • Docker run —name deiveehello hello-world
    • Docker run -it —name mylinuc-container ubuntu bash
For a hands on video - click here

——————————————————————
Some basic docker commands
——————————————————————

Docker images // lists all images
Docker ps // lists all process
Docker run —help

———————————————————————
Running a docker using simple ubuntu image
———————————————————————

docker ps -a -f status=exited -q
docker rm $(docker ps -a -f status=exited -q)
docker run -it --name my-linuc-container-1 ubuntu bash


docker run -it --name my-linux-container1 --rm -v /Users/deiveehannallazhagappan/Documents/volumes/project1:/my-data ubuntu bash
Cd my-data
Touch aa.txt
Ls

———————————————————————
Creating image from a local docker file.
———————————————————————

  • Create a docker file with the following content.
FROM ubuntu
CMD echo "Hello Deivee"
  • Run the following command. 

docker $docker build -t deivee-tracker-image .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM ubuntu
 ---> f975c5035748
Step 2/2 : CMD echo "Hello Deivee"
 ---> Running in 10c68c4eb454
Removing intermediate container 10c68c4eb454
 ---> b235e94e39b4
Successfully built b235e94e39b4
Successfully tagged deivee-tracker-image:latest

docker $docker images
REPOSITORY             TAG                 IMAGE ID            CREATED              SIZE
deivee-tracker-image   latest              b235e94e39b4        About a minute ago   112MB
ubuntu                 latest              f975c5035748        4 weeks ago          112MB
hello-world            latest              f2a91732366c        4 months ago         1.85kB

———————————————————————
Run the docker image
———————————————————————
docker $docker run deivee-tracker-image
Hello Deivee
docker $docker ps -a
CONTAINER ID        IMAGE                  COMMAND                   CREATED             STATUS                      PORTS               NAMES
9a65c141ec46        deivee-tracker-image   "/bin/sh -c 'echo \"H…"   5 seconds ago       Exited (0) 4 seconds ago                        optimistic_stonebraker
0e922f16f866        ubuntu                 "bash"                    12 minutes ago      Exited (0) 11 minutes ago                       tracker
be1311055d15        ubuntu                 "bash"                    23 minutes ago      Created                                         my-linux-container
77447d4d6c02        hello-world            "--name deiveehello"      12 hours ago        Created                                         elastic_wozniak

For a more hands on video on getting started with Docker, click here.
https://www.youtube.com/watch?v=hfL0USkCmZ0&t=305s


Java 8 - new important features


Java 8 has included a lot of new features, this post will explain a few important features. 
  1. Lamda Expressions. 
  2. Functional Interfaces. 
  3. Streams
  4. Nashorn
Other features. 
  • Date and time api changes. 
  • Foreach. 
——————————————
Lamda expressions
——————————————

Lamda expression is a function with no name and with implementation. 
A basic expression format would be..
(x, y) -> x + y

Where (x, y) are the function input parameters. And x+y is the statement executed by the function and returned. 

Example: 
FileFilter filterLamda = (File pathName) -> pathName.getName().endsWith(".png");

File dir = new File("/Users/deiveehannallazhagappan/workspace/xplore-java/supporting");
File[] files = dir.listFiles(filterLamda);

for (File f: files) {
    System.out.println("f = " + f);
}


——————————————
Functional Interfaces
——————————————
Functional interfaces are interfaces that have only one abstract method in it. 

Example: 
@FunctionalInterface
public interface TestFuncInterface {
    public void testmethod();

default void testdefaultmethod(){
         System.out.println(“default method“);
     }
}

Lamda expressions can be used to represent the instance of a functional interface. 

Note: 
  • You can add default methods .


——————————————
Streams
——————————————
Streams are new feature introduced in Java 8 to simplify the logic required to process data - filtering, transforming data etc., 

For example, the below example does the following

  • Creates a new array list from 1-30
  • Creates a stream of integers. 
  • Filters the list by extracting only no.s divisible by 3 and converts them into a integer array. 

Stream example: 
List<Integer> list = new ArrayList<Integer>();
for(int i = 1; i< 30; i++){
    list.add(i);
}
Stream<Integer> stream = list.stream();
Integer[] evenNumbersArr = stream.filter(i -> i%3 == 0).toArray(Integer[]::new);
System.out.print(evenNumbersArr);

Why streams: reduce complexity, less code. 

——————————————
Nashorn
——————————————
Nashorn is a javascript engine which allows the java applications to interact with the JS code. 

ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("nashorn");
engine.eval("print('deiveehan')");

You can also execute an external script instead of using inline js code. 
engine.eval(new FileReader("/Users/deiveehannallazhagappan/workspace/xplore-java/supporting/js/deiveescript.js"));

You will get the output of the js code when you run the java application. 



Refreshing - Git basics

This post explains some of the basics of git (a simple refresher post to GIT commands).


Git clone <git url> download a project from git remote repository

init
add <file> or .
commit -m <commit message> (Commit files to local repository)

status (list all new and modified files)
diff (Shows files modified)

fetch: fetches the remote repo into the local repo, but does not merge
pull: does and fetch and also does the merge into the local repo. 

Remote: lists the remote
git remote
git remote -v

Log: logs all changes
git log
git log —all
git log —all —oneline —decorate —graph



SCENARIOS:


———————————————————
# Local folder to Git remote. 
————————————————————

Steps to commit an already existing local project to git. 
git init
git add README.md
Git commit -m “Initial commit”
Create a repository in git and get the 
git remote add origin git@github.com:deiveehan/<reponame.git>
Git push -u origin master. 

————————————————————
# Create a branch from command line.
————————————————————

git branch —list (shows all the branches). 
git checkout -b develop (creates a local branch called develop)
change a file
perform git add and commit to the local code.
git push —set-upstream origin develop

————————————————————
# Switching branches.
————————————————————
Git branch (shows local branches)
Git checkout develop

————————————————————
# Merging with parent branch (say develop)
————————————————————
Master
  • Develop
  • A

commit A changes to local repository
git checkout develop
git merge A
git push

————————————————————
# Deleting remote branch
————————————————————
git branch -d A (Delete references of local branch)
git push origin -d A (delete remote branch)
————————————————————