Dockerized Java Workshop for GR8conf 2017

Preview:

Citation preview

01

02

Let's start!03

Dockerrrr04

Concepts05

Java's promise

06

Docker's promise

07

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

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

for starting Docker containers.“09

Docker image

10

Dockerfile

11

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

Container states

13

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

containers.“14

Docker registryA repository of Docker images

Registry can be private or public

Docker Hub is a public Docker registry

•••

15

Image/container manipulations

16

Docker Flow

17

Container packaging

18

Container packaging

19

Container packaging

20

Let's practice!21

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

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

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

23

Preparation

24

Exercise 1.125

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

Exercise 1.227

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

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

Exercise 1.330

Exercise 1.3: run java serverCompile  KnockKnockServer  using Docker

Run  KnockKnockServer  using Docker

And... surprise...

Compile  KnockKnockClient  using Docker

Run  KnockKnockClient  using Docker

•••••

32

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

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

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

Exercise 2.136

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

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

Exercise 2.239

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

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

41

Exercise 2.342

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

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

command.•

44

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

Conclusion46

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

setup.git

47

Home work48

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

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

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

Readingmaterial

52

Docker documentation

53

Docker labs

54

Book: Containerizing CD in Java

55

Book: Docker for Java Developers

56

Book: Docker Up and Running

57

Book: Using Docker

58

Book: Infrastructure as Code

59

That's all!60

Thank you!61

62