Skip to content

Setting up a Java and Gradle Developer Environment with Docker

Java is a cross-platform (Windows, Linux, etc.) programming language. Gradle is a build system used to orchestrate builds. Docker is a tool used to capture a machine environment for building etc.

Install Docker & Docker Compose on Any OS

Just follow the directions from Docker, they are pretty good:

For Docker Compose:

Verify Docker Setup

If you have docker running, then scroll to the next section: Setup Developer Machine

Check Docker was installed correctly…

C:\Users\admin>docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete                                                                                                                                                                                                           Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Verify Docker Compose is working:

C:\Users\admin> docker-compose
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

If you see see this same output (or similar), then you are good to go. Otherwise, docker.com has great resources and Google can help you setup your environment.

Setup Development Host

IntelliJ IDEA from JetBrains is a great IDE. I download this and I get the Linux build because even though I’m building on Windows I’m starting in a Linux development environment.

Download IntelliJ IDEA from JetBrains:

This is a virtualized environment on Windows and large builds can suffer a performance cost, so you may have interest in running the IDE on Windows directly (i.e. not through this virtualization layer).

Mind you, that this is only true when running Linux containers on Windows. If running Windows on Windows or Linux on Linux, then there should not be a performance cost, as the kernel is shared and there is no virtualization.

Because the Docker container is a standardized environment, if we can development in that same environment, then we should realize a fair amount of stability, this is why this guide sets up the IDE through Docker.

Extract IDEA and install like this:

Remember this is the Linux version of IDEA

First thing let’s agree on a host machine type to start us out on:

  • Ubuntu 18.04

This means our Dockerfile should look something like this:

# Copyright (c) 2020 eContriver LLC
# See README.md

FROM ubuntu:18.04

RUN dpkg --add-architecture i386

ENV TZ=America/Denver
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

####
# Download 
RUN apt-get update && apt-get install -y \
  curl \
  unzip \
  xorg \
  default-jdk \
  libz1 \
  libncurses5 \
  libbz2-1.0:i386 \
  libstdc++6 \
  libbz2-1.0 \
  lib32stdc++6 \
  lib32z1 \
  python2.7 \
  python-pip \
  cmake \
  pkg-config \
  libfontconfig1-dev \
  build-essential libgtk-3-dev \
  libglew-dev \
  libcurl4-openssl-dev \
  libsqlite3-dev \
  vim-gnome \
  vim \
  ant \
  iputils-ping \
  locate \
  git \
  && apt-get clean && apt-get purge

RUN apt-get update && apt-get install -y \
  ninja-build \
  && apt-get clean && apt-get purge

####
# Index
RUN updatedb

We save this file to our docker folder:

We also want to setup our Docker Compose configuration so edit that file:

This files content looks like:

# Copyright (c) 2020 eContriver LLC
#
# Start IDE with:
#   docker-compose up
# Build images with:
#   docker-compose build
# Run shell commands with:
#   docker-compose exec ide bash
# Stop and teardown all associated containers etc.
#   docker-compose down
# Run with
#   docker-compose run idea

version: "3.8"

services:
  deps:
    image: deps:latest
    build: 
      context: docker
      dockerfile: Dockerfile
  install:
    image: deps:latest
    depends_on: 
      - deps
    volumes:
      - type: bind
        source: ../
        target: /hydrogen/
      - type: bind
        source: ../hydrogen.root
        target: /root/
      - type: bind
        source: ../hydrogen.opt
        target: /opt/
    command:
      bash -c "/hydrogen/infra/install-tools.sh"
  ide:
    image: deps:latest
    depends_on: 
      - deps
    environment: 
      DISPLAY: 192.168.1.100:0
 # Current IP address, or leave blank to inherit
    ports:
      - '5555:5555'
    volumes:
      - type: bind
        source: ../
        target: /hydrogen/
      - type: bind
        source: ../hydrogen.root
        target: /root/
      - type: bind
        source: ../hydrogen.opt
        target: /opt/
      - type: bind
        source: ../hydrogen.keystore
        target: /keystore/
    command:
      bash -c "/opt/idea-IC-201.7846.76/bin/idea.sh"

Then we install

Contact Us

1 Example Street
Anytown, CA 10100
USA
%d bloggers like this: