Fibonacci with Java (+ Docker + Bash)

1. Prerequisites

  1. A Bash terminal.

  2. Java and Maven installed (if you are running a development environment).

  3. Docker installed.

Note

Versions on my machine:

$ echo $BASH_VERSION
5.1.16(1)-release

$ java -version
openjdk version "20.0.1" 2023-04-18
OpenJDK Runtime Environment (build 20.0.1+9-29)
OpenJDK 64-Bit Server VM (build 20.0.1+9-29, mixed mode, sharing)

$ mvn -v
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: /home/pj/.sdkman/candidates/maven/current
Java version: 20.0.1, vendor: Oracle Corporation, runtime: /home/pj/.sdkman/candidates/java/20.0.1-open
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.19.0-38-generic", arch: "amd64", family: "unix"

$ docker -v
Docker version 23.0.4, build f480fb1

2. Build and execution (on a development environment)

  1. Know the script run:

    $ ./run
    You need to pass some arguments! Examples:
    ./run docker rmi         # <- remove the generated docker image (docker-java-fibonacci-demo)
    ./run docker build       # <- build a docker image named 'docker-java-fibonacci-demo'
    ./run docker run         # <- run a docker container from image 'docker-java-fibonacci-demo'
  2. Build the jar

    $ mvn package
  3. Run the application:

    1. Through java:

      $ java -jar target/docker-java-fibonacci-demo-0.0.1-SNAPSHOT.jar 10
    2. Through maven:

      $ mvn compile exec:java -Dexec.mainClass="com.paulojeronimo.Fibonacci" -Dexec.args="10"
  4. Build the Docker image:

    $ ./run docker build
    Note
    this command with call mvn package automactically.
  5. Run the application (through Docker):

    $ ./run docker run 10
  6. Do clean up actions:

    1. Remove the Docker image:

      $ ./run docker rmi
    2. Remove the generated files in the project:

      $ mvn clean

3. Build and execution (using Docker only)

  1. Build the Docker image:

    $ ./run docker build Dockerfile.maven
  2. Run the application (through Docker).