62
01

Dockerized Java Workshop for GR8conf 2017

Embed Size (px)

Citation preview

Page 1: Dockerized Java Workshop for GR8conf 2017

01

Page 2: Dockerized Java Workshop for GR8conf 2017

02

Page 3: Dockerized Java Workshop for GR8conf 2017

Let's start!03

Page 4: Dockerized Java Workshop for GR8conf 2017

Dockerrrr04

Page 5: Dockerized Java Workshop for GR8conf 2017

Concepts05

Page 6: Dockerized Java Workshop for GR8conf 2017

Java's promise

06

Page 7: Dockerized Java Workshop for GR8conf 2017

Docker's promise

07

Page 8: Dockerized Java Workshop for GR8conf 2017

Docker engineDocker is a simple client/server application

A Docker Client talks to Docker daemon, which controls images and

containers

Docker Daemon exposes RESTFul API, that can be used remotely

••

08

Page 9: Dockerized Java Workshop for GR8conf 2017

Docker imageRead­only file system image and metadata, that serves as a template

for starting Docker containers.“09

Page 10: Dockerized Java Workshop for GR8conf 2017

Docker image

10

Page 11: Dockerized Java Workshop for GR8conf 2017

Dockerfile

11

Page 12: Dockerized Java Workshop for GR8conf 2017

Docker containerSingle process running within environment created from Docker image“12

Page 13: Dockerized Java Workshop for GR8conf 2017

Container states

13

Page 14: Dockerized Java Workshop for GR8conf 2017

Docker volumeA directory shared between the host system and one or more

containers.“14

Page 15: Dockerized Java Workshop for GR8conf 2017

Docker registryA repository of Docker images

Registry can be private or public

Docker Hub is a public Docker registry

•••

15

Page 16: Dockerized Java Workshop for GR8conf 2017

Image/container manipulations

16

Page 17: Dockerized Java Workshop for GR8conf 2017

Docker Flow

17

Page 18: Dockerized Java Workshop for GR8conf 2017

Container packaging

18

Page 19: Dockerized Java Workshop for GR8conf 2017

Container packaging

19

Page 20: Dockerized Java Workshop for GR8conf 2017

Container packaging

20

Page 21: Dockerized Java Workshop for GR8conf 2017

Let's practice!21

Page 22: Dockerized Java Workshop for GR8conf 2017

There are many exercises:Use java/javac without installing it (inside Docker container)

Play with simple client/server setup

Build and execute PetClinic application inside a container

Use Maven Docker container to isolate build and run of PetClinic

Run database in a container for PetClinic application

1.

2.

3.

4.

5.

22

Page 23: Dockerized Java Workshop for GR8conf 2017

Sharing is caring!Gist: http://bit.ly/DOCKER_JAVA_GR8CONF_GIST

Slides: http://bit.ly/DOCKER_JAVA_GR8CONF_SLIDES••

23

Page 24: Dockerized Java Workshop for GR8conf 2017

Preparation

24

Page 25: Dockerized Java Workshop for GR8conf 2017

Exercise 1.125

Page 26: Dockerized Java Workshop for GR8conf 2017

Exercise 1.1: pull java imagesPull Java image without installing Java using:  docker pull java

Pull OpenJDK image:  docker pull openjdk .

Pull image with tag:  docker pull openjdk:latest

Pull different version of Java:  docker pull openjdk:7

List local images:  docker images

Remove old image:  docker rmi openjdk:7

••••••

26

Page 27: Dockerized Java Workshop for GR8conf 2017

Exercise 1.227

Page 28: Dockerized Java Workshop for GR8conf 2017

Exercise 1.2: run java/javacLet's check Java version:  docker run java java ‐version

Is there any container running:  docker ps ?

Are there any traces:  docker ps ‐a ?

Let's clean a bit:  docker rm <container_id>

New in 1.13+:  docker container prune

•••••

28

Page 29: Dockerized Java Workshop for GR8conf 2017

Exercise 1.2: run java/javacRemove after exit:  docker run ‐‐rm java java ‐version

Let's check our status:  docker ps ‐a

Let's compile some code:  docker run ‐‐rm java javac

HelloWorld.java

Now that's the real command:  docker run ‐‐rm ‐v $PWD:/src

java javac /src/HelloWorld.java

Finally run it:  docker run ‐‐rm ‐v $PWD:/src java java ‐

classpath /src HelloWorld

•••

29

Page 30: Dockerized Java Workshop for GR8conf 2017

Exercise 1.330

Page 32: Dockerized Java Workshop for GR8conf 2017

Exercise 1.3: run java serverCompile  KnockKnockServer  using Docker

Run  KnockKnockServer  using Docker

And... surprise...

Compile  KnockKnockClient  using Docker

Run  KnockKnockClient  using Docker

•••••

32

Page 33: Dockerized Java Workshop for GR8conf 2017

Exercise 1.3: hintsKnockKnock server's port can be exposed to the host using  ‐p

4444:4444

Then KnockKnock server can be tested with  telnet localhost

4444

33

Page 34: Dockerized Java Workshop for GR8conf 2017

Exercise 1.3: hintsEach container gets an IP address:

New way:  docker inspect ‐f '{{range

.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <c_id>

Older way:  docker inspect ‐f '{{

.NetworkSettings.IPAddress }}' <c_id>

Again,  telnet <c_ip> 4444

••

34

Page 35: Dockerized Java Workshop for GR8conf 2017

Exercise 1.3: hintsContainers can be "linked" within container network

docker create network knock‐knock

Use  ‐‐net=knock‐knock  to run container in the network

Container name (e.g.  ‐‐name=knock‐knock‐server ) will be its

hostname visible to other containers running in the same network

••••

35

Page 36: Dockerized Java Workshop for GR8conf 2017

Exercise 2.136

Page 37: Dockerized Java Workshop for GR8conf 2017

Exercise 2.1: build and run PetClinicClone  https://github.com/spring‐projects/spring‐petclinic

Create  Dockerfile  to build runnable PetClinic image

Run PetClinic container out of that image

•••

37

Page 38: Dockerized Java Workshop for GR8conf 2017

Exercise 2.1: hintsThere is Maven Wrapper inside the repository. Use it to build.

mvnw install spring‐boot:repackage  goals will build the

application.

mvnw spring‐boot:run  will run the application.

It may be worth putting Maven Home inside a volume.

••

••

38

Page 39: Dockerized Java Workshop for GR8conf 2017

Exercise 2.239

Page 40: Dockerized Java Workshop for GR8conf 2017

Exercise 2.2: separate build and runUse Maven Docker image/container to build PetClinic artifacts.

Use the output of that execution and Java image to package runnable

production image as Spring­Boot fat­jar.

••

40

Page 41: Dockerized Java Workshop for GR8conf 2017

Exercise 2.2: hintsUse  mvn spring‐boot:repackage  to build fat JAR artifact.•

41

Page 42: Dockerized Java Workshop for GR8conf 2017

Exercise 2.342

Page 43: Dockerized Java Workshop for GR8conf 2017

Exercise 2.3: run PetClinic with adatabaseChange PetClinic application code to use MySQL. For that modify the

data‐access.properties  file and verify that the  mysql‐

connector‐java  artifact is enabled in  pom.xml .

Start MySQL container:

   docker run ‐e MYSQL_ROOT_PASSWORD=petclinic 

              ‐e MYSQL_DATABASE=petclinic 

              ‐p 3306:3306 mysql:5.7.8

•01.

02.

03.

43

Page 44: Dockerized Java Workshop for GR8conf 2017

Exercise 2.3: hintsUse  docker‐compose  to start database and PetClinic in one

command.•

44

Page 45: Dockerized Java Workshop for GR8conf 2017

Docker Compose exampleversion: '2'

services:

  db:

    ports:

      ‐ "3306:3306"

    image: "mysql:5.7.8"

    ...

  petclinic:

    image: "petclinic:1.0"

    ....

01.

02.

03.

04.

05.

06.

07.

08.

09.

10.45

Page 46: Dockerized Java Workshop for GR8conf 2017

Conclusion46

Page 47: Dockerized Java Workshop for GR8conf 2017

Solutionshttps://github.com/aestasit/talks2017­gr8confeu­dockerized­java­

setup.git

47

Page 48: Dockerized Java Workshop for GR8conf 2017

Home work48

Page 49: Dockerized Java Workshop for GR8conf 2017

HW1: WebLogicOracle does not share much on GitHub...

But they have this: https://github.com/oracle/docker­images

Contains Dockerfiles and instruction on how to run WebLogic inside

Docker.

That should be easy to run PetClinic in WebLogic in Docker :)!

•••

49

Page 50: Dockerized Java Workshop for GR8conf 2017

HW2: Jenkins 2Jenkins 2 supports pipeline­as­code approach with the help of Groovy

DSL.

That DSL supports Docker­based stages and steps.

How about running Jenkins inside Docker container and make it use

Docker client to start builds in the same host as Jenkins?

Ha, that's going to be Docker­in­a­Docker :)!

••

50

Page 51: Dockerized Java Workshop for GR8conf 2017

HW3: PetClinic refactoredPetClinic's architecture is rather monolithic...

Of course, it's time to refactor for more microservices!

Here it goes: https://github.com/spring­petclinic/spring­petclinic­

microservices.

Well, it is already dockerized for you :)!

But you can play with it, to experience what if feels like to have many

containers.

•••

••

51

Page 52: Dockerized Java Workshop for GR8conf 2017

Readingmaterial

52

Page 53: Dockerized Java Workshop for GR8conf 2017

Docker documentation

53

Page 54: Dockerized Java Workshop for GR8conf 2017

Docker labs

54

Page 55: Dockerized Java Workshop for GR8conf 2017

Book: Containerizing CD in Java

55

Page 56: Dockerized Java Workshop for GR8conf 2017

Book: Docker for Java Developers

56

Page 57: Dockerized Java Workshop for GR8conf 2017

Book: Docker Up and Running

57

Page 58: Dockerized Java Workshop for GR8conf 2017

Book: Using Docker

58

Page 59: Dockerized Java Workshop for GR8conf 2017

Book: Infrastructure as Code

59

Page 60: Dockerized Java Workshop for GR8conf 2017

That's all!60

Page 61: Dockerized Java Workshop for GR8conf 2017

Thank you!61

Page 62: Dockerized Java Workshop for GR8conf 2017

62