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

2017 DevNexus - Docker Tips & Tricks for Java Developers

Ray Tsang
February 24, 2017

2017 DevNexus - Docker Tips & Tricks for Java Developers

Presented at 2017 DevNexus. https://devnexus.com/s/devnexus2017/presentations/17356

Most of the presentation was live coded/demonstrated rather than going through these slides. But these captures most of what I showed.

Parts of the talk also shared what I learned here:
https://medium.com/google-cloud/my-slow-internet-vs-docker-7678ae1cae72#.5aj7ych1c
https://medium.com/google-cloud/deep-dream-with-containers-d28a26e4c8a3#.dmx9a66nc

Ray Tsang

February 24, 2017
Tweet

More Decks by Ray Tsang

Other Decks in Technology

Transcript

  1. @saturnism @googlecloud #kubernetes #devnexus
    Container Tips & Tricks
    For Java Developers

    View full-size slide

  2. @saturnism @googlecloud #kubernetes #devnexus
    Ray Tsang
    Developer Advocate
    Google Cloud Platform
    @saturnism | +RayTsang

    View full-size slide

  3. @saturnism @googlecloud #kubernetes #devnexus
    Ray Tsang
    Developer
    Architect
    Traveler
    Photographer
    flickr.com/saturnism

    View full-size slide

  4. @saturnism @googlecloud #kubernetes #devnexus

    View full-size slide

  5. @saturnism @googlecloud #kubernetes #devnexus
    Containers!

    View full-size slide

  6. @saturnism @googlecloud #kubernetes #devnexus

    View full-size slide

  7. @saturnism @googlecloud #kubernetes #devnexus
    Hello World
    Service - Greet
    Guestbook
    Service - Create
    Guestbook Service -
    Retrieve

    View full-size slide

  8. @saturnism @googlecloud #kubernetes #devnexus
    Guestbook
    UI
    Hello World
    Service
    Redis
    session replication
    greeting
    MySQL
    Guestbook
    Service
    CRUD

    View full-size slide

  9. @saturnism @googlecloud #kubernetes #devnexus
    Package & Deployment
    application.ear
    app.war
    helloworld-service.jar
    application.war
    /...
    helloworld-service.jar

    View full-size slide

  10. @saturnism @googlecloud #kubernetes #devnexus
    Machine
    app.war / app.ear
    Application Server
    Kernel
    Shell / CLI / Tools

    View full-size slide

  11. @saturnism @googlecloud #kubernetes #devnexus
    Machine
    app.war / app.ear
    Application Server
    Kernel
    Shell / CLI / Tools
    Machine
    app.war / app.ear
    Application Server
    Kernel
    Shell / CLI / Tools
    Machine
    app.war / app.ear
    Application Server
    Kernel
    Shell / CLI / Tools

    View full-size slide

  12. @saturnism @googlecloud #kubernetes #devnexus
    Old Way: Shared machines
    kernel
    libs
    app
    app app
    No isolation
    No namespacing
    Common libs
    Highly coupled apps and OS
    app

    View full-size slide

  13. @saturnism @googlecloud #kubernetes #devnexus
    Old Way: Virtual machines
    Some isolation
    Expensive and inefficient
    Still highly coupled to the guest OS
    Hard to manage
    app
    libs
    kernel
    libs
    app app
    kernel
    app
    libs
    libs
    kernel
    kernel

    View full-size slide

  14. @saturnism @googlecloud #kubernetes #devnexus
    New Way: Containers
    libs
    app
    libs
    app
    libs
    app
    libs
    app

    View full-size slide

  15. @saturnism @googlecloud #kubernetes #devnexus
    Process isolation
    CPU, Memory, Network, Filesystem, ...

    View full-size slide

  16. @saturnism @googlecloud #kubernetes #devnexus
    Static Binary!
    Runs anywhere

    View full-size slide

  17. @saturnism @googlecloud #kubernetes #devnexus
    NOT a VM
    Just a process

    View full-size slide

  18. @saturnism @googlecloud #kubernetes #devnexus
    NOT a Security Boundary!
    Repeat after me!

    View full-size slide

  19. @saturnism @googlecloud #kubernetes #devnexus
    Containerize Option #1
    Dockerfile

    View full-size slide

  20. @saturnism @googlecloud #kubernetes #devnexus
    Containerize Option #2
    spotify/docker-maven-plugin
    mvn docker:build

    View full-size slide

  21. @saturnism @googlecloud #kubernetes #devnexus
    Containerize Option #3
    Docker Hub / GitHub
    saturnism/spring-boot

    View full-size slide

  22. @saturnism @googlecloud #kubernetes #devnexus
    Containerize Option #4
    Slim JARs!
    spotify/dockerfile-maven-plugin

    View full-size slide

  23. @saturnism @googlecloud #kubernetes #devnexus
    Containerize Option #5
    Fabric8
    fabric8 maven plugin

    View full-size slide

  24. @saturnism @googlecloud #kubernetes #devnexus
    Which port?
    Internal port vs machine port

    View full-size slide

  25. @saturnism @googlecloud #kubernetes #devnexus
    Versioning container image
    docker tag spring-boot-demo spring-boot-demo:1.0
    docker tag spring-boot-demo spring-boot-demo:1.0-k39fa

    View full-size slide

  26. @saturnism @googlecloud #kubernetes #devnexus
    Build Number Plugin
    http://www.mojohaus.org/buildnumber-maven-plugin/
    https://github.com/ktoso/maven-git-commit-id-plugin
    Append build number, or SCM commit hash to image tag

    View full-size slide

  27. @saturnism @googlecloud #kubernetes #devnexus
    Space Saving Tips

    View full-size slide

  28. @saturnism @googlecloud #kubernetes #devnexus
    Combine RUN commands
    apt-get update && \
    apt-get install -y --no-install-recommends ... && \
    rm -rf /var/lib/apt/lists/*
    Saves you space.

    View full-size slide

  29. @saturnism @googlecloud #kubernetes #devnexus
    Don’t Log to Container Filesystem!
    Log to a volume… docker -v /tmp/log:/log
    Or, better yet,
    Send it elsewhere! I prefer STDOUT

    View full-size slide

  30. @saturnism @googlecloud #kubernetes #devnexus
    Clean up disk spaces
    Every image, layer, and, even containers litters
    docker rm $(docker ps -a -q)
    docker rmi $(docker images -q --filter dangling=true)

    View full-size slide

  31. @saturnism @googlecloud #kubernetes #devnexus
    Clean up disk spaces
    Docker 1.13 makes it easier
    docker system prune

    View full-size slide

  32. @saturnism @googlecloud #kubernetes #devnexus
    Spotify's Docker GC
    https://github.com/spotify/docker-gc

    View full-size slide

  33. @saturnism @googlecloud #kubernetes #devnexus
    docker run --rm
    Removes container afterwards

    View full-size slide

  34. @saturnism @googlecloud #kubernetes #devnexus
    Security, Immutability

    View full-size slide

  35. @saturnism @googlecloud #kubernetes #devnexus
    Don't run as root!
    It's default… :(
    Specify via USER directive and switch users

    View full-size slide

  36. @saturnism @googlecloud #kubernetes #devnexus
    Know what's in that container
    Vulernerabilities

    View full-size slide

  37. @saturnism @googlecloud #kubernetes #devnexus
    Know what's in that public
    container!
    Vulernerabilities

    View full-size slide

  38. @saturnism @googlecloud #kubernetes #devnexus
    Security Scanning
    X-Ray, Quay.io, Stacksmith

    View full-size slide

  39. @saturnism @googlecloud #kubernetes #devnexus
    Pin your versions
    Consistent rebuild
    Repeatability

    View full-size slide

  40. @saturnism @googlecloud #kubernetes #devnexus
    Java Specific

    View full-size slide

  41. @saturnism @googlecloud #kubernetes #devnexus
    SecureRandom - slow =(
    For development and testing
    -Djava.security.egd=file:/dev/urandom

    View full-size slide

  42. @saturnism @googlecloud #kubernetes #devnexus
    How many CPUs?
    Red Hat has it right - check their scripts!
    Don't trust Runtime.availableProcessors()
    https://github.com/fabric8io-images/run-java-sh

    View full-size slide

  43. @saturnism @googlecloud #kubernetes #devnexus
    JDK 9
    https://bugs.openjdk.java.net/browse/JDK-6515172

    View full-size slide

  44. @saturnism @googlecloud #kubernetes #devnexus
    How much memory?
    Check Red Hat's script!
    https://github.com/fabric8io-images/run-java-sh
    java -XX:+PrintFlagsFinal -version | grep HeapSize

    View full-size slide

  45. @saturnism @googlecloud #kubernetes #devnexus
    Using container for testing
    https://www.testcontainers.org/

    View full-size slide

  46. @saturnism @googlecloud #kubernetes #devnexus
    Composition

    View full-size slide

  47. @saturnism @googlecloud #kubernetes #devnexus
    Let’s run the container!
    docker run -ti -p 8080:8080 helloworld-service

    View full-size slide

  48. @saturnism @googlecloud #kubernetes #devnexus
    MySQL
    docker run -d --name mysql -p 3306:3306 -e
    MYSQL_ROOT_PASSWORD=yourpassword -e MYSQL_DATABASE=app mysql

    View full-size slide

  49. @saturnism @googlecloud #kubernetes #devnexus
    Guestbook Service
    docker run -ti --name guestbookservice --link mysql:mysql
    saturnism/guestbook-service

    View full-size slide

  50. @saturnism @googlecloud #kubernetes #devnexus
    Redis
    docker run -d --name redis redis

    View full-size slide

  51. @saturnism @googlecloud #kubernetes #devnexus
    Hello World Service
    docker run -ti --name helloworldservice \
    saturnism/spring-boot-helloworld-service:1.0

    View full-size slide

  52. @saturnism @googlecloud #kubernetes #devnexus
    Guestbook UI
    docker run -ti --rm --link redis:redis \
    --link helloworldservice:helloworldservice \
    --link guestbookservice:guestbookservice \
    -p 8080:8080 saturnism/spring-boot-helloworld-ui

    View full-size slide

  53. @saturnism @googlecloud #kubernetes #devnexus
    Test locally!
    Deployment during development could be painful
    Set environmental variables
    Use Docker Compose

    View full-size slide

  54. @saturnism @googlecloud #kubernetes #devnexus
    Docker Compose
    docker-compose up

    View full-size slide

  55. @saturnism @googlecloud #kubernetes #devnexus
    Configuration
    Environmental variable
    Command line arguments
    Properties files

    View full-size slide

  56. @saturnism @googlecloud #kubernetes #devnexus
    Don't store credentials...
    For obvious reasons!

    View full-size slide

  57. @saturnism @googlecloud #kubernetes #devnexus
    JAVA_OPTS
    Make sure you can set it via the environmental variable!

    View full-size slide

  58. @saturnism @googlecloud #kubernetes #devnexus
    Dealing low Bandwidth

    View full-size slide

  59. @saturnism @googlecloud #kubernetes #devnexus
    Bathroom
    50" TV
    Ikea Sofa Bed
    Mini Fridge
    Closet
    Door

    View full-size slide

  60. @saturnism @googlecloud #kubernetes #devnexus
    But the WIFI was FREE!
    But soooooo SLOW

    View full-size slide

  61. @saturnism @googlecloud #kubernetes #devnexus
    Bad WIFI
    Good WIFI

    View full-size slide

  62. @saturnism @googlecloud #kubernetes #devnexus
    20'
    40'
    45'
    48'
    53'

    View full-size slide

  63. @saturnism @googlecloud #kubernetes #devnexus
    20'
    40'
    45'
    48'
    53'
    My Apartment!

    View full-size slide

  64. @saturnism @googlecloud #kubernetes #devnexus
    Use Docker Machine
    In the cloud - faster network to download images

    View full-size slide

  65. @saturnism @googlecloud #kubernetes #devnexus
    Share a Docker Daemon
    Docker Daemon is just a server!

    View full-size slide

  66. @saturnism @googlecloud #kubernetes #devnexus
    Build inside of Docker container
    Consistent build environment

    View full-size slide

  67. @saturnism @googlecloud #kubernetes #devnexus
    STDOUT/STDIN
    You can pipe input and outputs like a regular command line

    View full-size slide

  68. @saturnism @googlecloud #kubernetes #devnexus
    Compression
    docker build --compress ...

    View full-size slide

  69. @saturnism @googlecloud #kubernetes #devnexus
    Squashing?
    This is experimental!
    docker build --squash .

    View full-size slide

  70. @saturnism @googlecloud #kubernetes #devnexus
    Orchestration!

    View full-size slide

  71. @saturnism @googlecloud #kubernetes #devnexus
    Use an Orchestrator!
    Don't deploy containers to individual machines yourself…
    Let an orchestrator do it for you!

    View full-size slide

  72. @saturnism @googlecloud #kubernetes #devnexus
    Try out Google Container Engine
    https://cloud.google.com/container-engine/

    View full-size slide

  73. @saturnism @googlecloud #kubernetes #devnexus
    Thanks!
    Images by Connie
    Zhou
    http://kubernetes.io
    http://bit.ly/1QLg5E1

    View full-size slide