Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker for Java Developers

Docker for Java Developers

Slides to the Docker for Java Developers lab for the Docker birthday #4

Thorsten Hoeger

March 17, 2017
Tweet

More Decks by Thorsten Hoeger

Other Decks in Technology

Transcript

  1. About me • Thorsten Höger • Cloud Consultant - Taimos

    GmbH • twitter: @hoegertn • web: www.taimos.de
  2. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  3. Docker basics Docker is the world’s leading software container platform.

    Developers use Docker to eliminate “works on my machine” problems when collaborating on code with co-workers. Operators use Docker to run and manage apps side-by-side in isolated containers to get better compute density. Enterprises use Docker to build agile software delivery pipelines to ship new features faster, more securely and with confidence for both Linux and Windows Server apps. -- docker.com
  4. Docker basics • Docker daemon • Background process managing images,

    containers, ... • Providing REST API for clients (/var/run/docker.sock) • Docker client • Interface to send command to Docker daemon • Default client: „docker“ CLI command
  5. Docker basics • Filesystem of containers is managed as onion

    filesystem • Immutable layers containing files • File are markes as removed in the current layer but not deleted • Only top layer is writable
  6. Docker Components Image Container Registry • Contains all files •

    OS • application • Default command • Running instance of an image • Custom environment variables • Port mappings from container to host • Stores images centrally • New versions are pushed to the registry • Instance pull images when provisioned
  7. Docker images • Docker images follow a naming pattern that

    contains information about the registry • my.registry.example.org/app1:1.0 Hostname of the registry Name of the image Imagetag (e.g. version)
  8. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  9. Docker installation • Docker is running on Linux and using

    the kernel • There are ports of the CLI for Mac and Windows and a runtime using the system hypervisor • Docker for Windows • Using Hyper-V as hypervisor running Alpine Linux • Docker for macOS • Using xhyve as hypervisor running Alpine Linux
  10. Docker versions • Docker versioning changed this month • Latest

    versions for old schema • 1.11 current for many enterpise Linux systems • 1.12 first version with new cluster and networking • 1.13 • New version follow year.month schema • 17.03 current version
  11. Docker versions • Beginning with 17.03 there are two tracks

    of Docker • Docker Community edition • Formerly known as Docker • Docker Enterprise edition • Version of Docker with certified support for different OS • Security checked images • Security checked plugins
  12. Using Docker • build Build an image from a Dockerfile

    • exec Run a command in a running container • imagesList images • info Display system-wide information • inspectReturn low-level information on Docker objects • kill Kill one or more running containers • login Log in to a Docker registry • logs Fetch the logs of a container • ps List containers • pull Pull an image or a repository from a registry • push Push an image or a repository to a registry • rm Remove one or more containers • rmi Remove one or more images • run Run a command in a new container • tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  13. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  14. Running Docker containers • Often used options for „docker run“

    • -d, --detach Run container in background and print container ID • -e, --env list Set environment variables (default []) • -i, --interactive Keep STDIN open even if not attached • --link list Add link to another container (default []) • --name string Assign a name to the container • -p, --publish list Publish a container's port(s) to the host (default []) • --restart string Restart policy to apply when a container exits (default "no") • --rm Automatically remove the container when it exits • -t, --tty Allocate a pseudo-TTY • -v, --volume list Bind mount a volume (default [])
  15. Running Docker containers • docker pull openjdk:8-alpine • Pull latest

    version of the Alpine based OpenJDK image • docker run --rm -ti openjdk:8-alpine sh • Run a shell in the OpenJDK container
  16. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  17. Creating Docker images • Images are created using the command

    „docker build“ • Images are build following a spec file called „Dockerfile“ • Every line in a Dockerfile creates a new file system layer • Last layer is tagged with the name and represents the image
  18. Creating Docker images • FROM openjdk:8-alpine • MAINTAINER Thorsten Hoeger

    • COPY test.jar /opt/test.jar • RUN apk add -U tzdata && ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime • WORKDIR /opt • CMD [“java", "-jar", “test.jar"]
  19. Creating Docker images • docker pull openjdk:8-alpine • Pull latest

    version of the Alpine based OpenJDK image • docker build -t birthday-java:1 . • Build Docker image named „birthday-java“ with tag „1“ from the current folder • docker run --rm -ti birthday-java:1 • Run Docker image and run Java application
  20. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  21. Maven integration for Docker • Maven plugin: io.fabric8/docker-maven-plugin • Goals

    to • build images • start/stop containers • push images to a registry • ... and more • Configuration inside pom.xml (no Dockerfile)
  22. Maven integration for Docker <image> <name> my.registry.host/myAppName:${project.version} </name> <build> <from>openjdk:8-jdk-alpine</from>

    <maintainer>${project.organization.name}</maintainer> <tags> <tag>latest</tag> <tag>${project.version}</tag> </tags> <ports> <port>8080</port> </ports> <workdir>/opt/daemon</workdir> <runCmds> <run>apk --update --no-cache add ttf-dejavu</run> </runCmds> <cmd> ... </cmd> <cleanup>false</cleanup> <assembly> ... </assembly> </build> </image>
  23. Maven integration for Docker <cmd> <exec> <args>java</args> <args>-cp</args> <args>${project.artifactId}.jar:lib/*</args> <args>-Xmx256m</args>

    <args>-XX:+ExitOnOutOfMemoryError</args> <args>-Djava.net.preferIPv4Stack=true</args> <args>-Djava.awt.headless=true</args> <args>${mainClass}</args> </exec> </cmd>
  24. Maven integration for Docker <assembly> <targetDir>/opt/daemon</targetDir> <inline xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ...

    "> <id>docker-assembly</id> <fileSets> <fileSet> <directory>target</directory> <outputDirectory>/</outputDirectory> <includes> <include>${project.artifactId}.jar</include> </includes> </fileSet> <fileSet> <directory>target/lib</directory> <outputDirectory>/lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </inline> </assembly>
  25. Maven integration for Docker <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.0</version> <executions> <execution>

    <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <prependGroupId>true</prependGroupId> <includeScope>runtime</includeScope> </configuration> </execution> </executions> </plugin>
  26. Maven integration for Docker <executions> <execution> <id>build</id> <phase>package</phase> <goals> <goal>build-nofork</goal>

    </goals> </execution> <execution> <id>push</id> <phase>deploy</phase> <goals> <goal>push</goal> </goals> </execution> </executions>
  27. Maven integration for Docker <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip>

    </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> <configuration> <skip>true</skip> </configuration> </plugin>
  28. Maven integration for Docker • mvn verify will then •

    build your application • run all tests • download all dependencies to target/lib • create a Docker image with all jars • you can then run it using • docker run --rm my.registry.host/myAppName:latest • or better add „--restart=always“
  29. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  30. Agenda • Docker basics • Docker installation • Running containers

    • Creating images • Build images using Maven • Using the IDE • CI/CD using Docker
  31. CI/CD using Jenkinsfile node { def version try { stage('Preparation')

    { slackSend channel: "infrastructure", message: "Build & Deployment starting" checkout scm version = sh(script: 'git rev-list --all --count', returnStdout: true).trim() echo "Building version ${version}" sh 'docker pull openjdk:8-alpine' } stage('Build') { echo "Building version ${version}" sh "mvn versions:set -DnewVersion=${version}" sh "mvn clean deploy" } stage('Deploy') { echo “Trigger update of production using version ${version}" } stage('Notify') { slackSend channel: "infrastructure", message: "Build & Deployment of version ${version} complete" } } catch (err) { slackSend channel: "infrastructure", message: "Build & Deployment of version ${version} failed" } }