From 5668819cf4532881faf20d887e3d3c278f43789b Mon Sep 17 00:00:00 2001 From: Alessandro Giugno Date: Fri, 12 Jan 2024 08:11:44 +0100 Subject: [PATCH 1/4] build(docker): multi stage build removes JDK requirement on host --- .devcontainer/devcontainer.json | 8 ++++++++ .gitattributes | 3 +++ Dockerfile | 28 ++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitattributes diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..4b2ee3aa --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,8 @@ +{ + "name": "[Optional] Your project name here", + "dockerComposeFile": "../docker-compose-dev.yml", + "service": "yggdrasil", + "workspaceFolder": "/opt/app/yggdrasil", + "postCreateCommand": "./gradlew", + "shutdownAction": "stopCompose" +} diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..f9900ac7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto +gradlew binary \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2f5f5b14..ae4744a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,27 @@ +FROM eclipse-temurin:21 as builder + +# Yggdrasil build configuration +ENV YGGDRASIL_VERSION 0.0.0 +# Enable building specific branches +ARG YGGDRASIL_BRANCH="main" + +# Build environment preparation +ENV LANG C.UTF-8 + +RUN apt update && apt install -y \ + git \ + && rm -rf /var/cache/apt/archives /var/lib/apt/lists/* + +RUN echo ${YGGDRASIL_BRANCH} +RUN git clone https://github.com/Interactions-HSG/yggdrasil.git \ + --branch $YGGDRASIL_BRANCH \ + # only the specified branch + --depth=1 \ + /opt/app + +WORKDIR /opt/app/ +RUN ./gradlew + FROM eclipse-temurin:21 # Yggdrasil build configuration @@ -8,10 +32,10 @@ ENV LANG C.UTF-8 # Copy the jar RUN mkdir /opt/app -COPY build/libs/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar /opt/app +COPY --from=builder /opt/app/build/libs/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar /opt/app # Copy the configuration RUN mkdir /opt/app/conf -COPY conf/docker_disk_config.json /opt/app/conf/config.json +COPY --from=builder /opt/app/conf/docker_disk_config.json /opt/app/conf/config.json # The default http port EXPOSE 8080 From 52ec423acc220a474c882f9fef5d183540a3f790 Mon Sep 17 00:00:00 2001 From: Alessandro Giugno Date: Fri, 12 Jan 2024 08:52:58 +0000 Subject: [PATCH 2/4] build(docker): mount and cache prevents loss of changes --- .devcontainer/devcontainer.json | 2 +- Dockerfile_Dev | 10 ++++++++++ docker-compose-dev.yml | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Dockerfile_Dev create mode 100644 docker-compose-dev.yml diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 4b2ee3aa..92623d2d 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,7 @@ "name": "[Optional] Your project name here", "dockerComposeFile": "../docker-compose-dev.yml", "service": "yggdrasil", - "workspaceFolder": "/opt/app/yggdrasil", + "workspaceFolder": "/opt/app/", "postCreateCommand": "./gradlew", "shutdownAction": "stopCompose" } diff --git a/Dockerfile_Dev b/Dockerfile_Dev new file mode 100644 index 00000000..5e97168f --- /dev/null +++ b/Dockerfile_Dev @@ -0,0 +1,10 @@ +FROM eclipse-temurin:21 + +RUN apt update && apt install -y \ + git + +RUN mkdir /opt/app/ +ADD . /opt/app/ +WORKDIR /opt/app/ + +#RUN git clone https://github.com/Interactions-HSG/yggdrasil.git \ No newline at end of file diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..abc3fadd --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,21 @@ +version: '3.9' + +services: + yggdrasil: + image: yggdrasil:dev + build: + context: . + dockerfile: Dockerfile_Dev + tty: true + container_name: yggdrasil + volumes: + - .:/opt/app/:cached +# # create a project volume to keep work +# # check if git can change line endings automatically +# - .:/opt/app/y:cached + environment: + - YGGDRASIL_VERSION=0.0.0 + - GRADLE_TASK=${GRADLE_TASK} + ports: + - "8899:8080" + - "8900:8088" From a97b1687d261bef4cd5327e4ef052e6f82c6c6ec Mon Sep 17 00:00:00 2001 From: Alessandro Giugno Date: Fri, 12 Jan 2024 15:13:52 +0100 Subject: [PATCH 3/4] build(docker): multi stage build makes ignoring files unnecessary --- .dockerignore | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index ab16c5d0..00000000 --- a/.dockerignore +++ /dev/null @@ -1,4 +0,0 @@ -** -!build/libs -!conf -!Dockerfile From 1e33e4c92dc305d3c4feae73778f438982067bef Mon Sep 17 00:00:00 2001 From: Alessandro Giugno Date: Fri, 12 Jan 2024 15:19:44 +0100 Subject: [PATCH 4/4] build(docker): mergeable compose files reduce number of files to maintain Instead of writing multiple compose files independently, one basic file and one with the extensions for development can be merged on CLI --- .devcontainer/devcontainer.json | 3 +-- .env | 4 ++++ Dockerfile | 42 +++++++-------------------------- Dockerfile_Dev | 10 -------- README.md | 16 ++++++++----- docker-compose-dev.yml | 28 ++++++++++------------ docker-compose.yml | 4 +++- 7 files changed, 39 insertions(+), 68 deletions(-) create mode 100644 .env delete mode 100644 Dockerfile_Dev diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 92623d2d..672ff3ce 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,8 +1,7 @@ { "name": "[Optional] Your project name here", - "dockerComposeFile": "../docker-compose-dev.yml", + "dockerComposeFile": ["../docker-compose.yml", "../docker-compose-dev.yml"], "service": "yggdrasil", "workspaceFolder": "/opt/app/", - "postCreateCommand": "./gradlew", "shutdownAction": "stopCompose" } diff --git a/.env b/.env new file mode 100644 index 00000000..b6c3780f --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +# Yggdrasil build configuration +YGGDRASIL_VERSION=0.0.0 +# Build environment preparation +LANG=C.UTF-8 diff --git a/Dockerfile b/Dockerfile index ae4744a3..7408f99e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,45 +1,21 @@ FROM eclipse-temurin:21 as builder -# Yggdrasil build configuration -ENV YGGDRASIL_VERSION 0.0.0 -# Enable building specific branches -ARG YGGDRASIL_BRANCH="main" - -# Build environment preparation -ENV LANG C.UTF-8 - -RUN apt update && apt install -y \ - git \ - && rm -rf /var/cache/apt/archives /var/lib/apt/lists/* - -RUN echo ${YGGDRASIL_BRANCH} -RUN git clone https://github.com/Interactions-HSG/yggdrasil.git \ - --branch $YGGDRASIL_BRANCH \ - # only the specified branch - --depth=1 \ - /opt/app +ADD . /opt/app/ WORKDIR /opt/app/ RUN ./gradlew FROM eclipse-temurin:21 -# Yggdrasil build configuration -ENV YGGDRASIL_VERSION 0.0.0 +RUN apt update && apt install -y \ + git \ + && rm -rf /var/cache/apt/archives /var/lib/apt/lists/* -# Build environment preparation -ENV LANG C.UTF-8 +ARG YGGDRASIL_VERSION -# Copy the jar -RUN mkdir /opt/app -COPY --from=builder /opt/app/build/libs/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar /opt/app -# Copy the configuration -RUN mkdir /opt/app/conf +# https://stackoverflow.com/a/793867 +RUN mkdir -p /opt/app/conf +COPY --from=builder /opt/app/build/libs/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar /opt/app/libs/ COPY --from=builder /opt/app/conf/docker_disk_config.json /opt/app/conf/config.json -# The default http port -EXPOSE 8080 -# The port for interacting with CArtAgO -EXPOSE 8088 - -ENTRYPOINT java -jar /opt/app/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar +ENTRYPOINT java -jar /opt/app/libs/yggdrasil-${YGGDRASIL_VERSION}-SNAPSHOT-all.jar diff --git a/Dockerfile_Dev b/Dockerfile_Dev deleted file mode 100644 index 5e97168f..00000000 --- a/Dockerfile_Dev +++ /dev/null @@ -1,10 +0,0 @@ -FROM eclipse-temurin:21 - -RUN apt update && apt install -y \ - git - -RUN mkdir /opt/app/ -ADD . /opt/app/ -WORKDIR /opt/app/ - -#RUN git clone https://github.com/Interactions-HSG/yggdrasil.git \ No newline at end of file diff --git a/README.md b/README.md index 009916d9..384ed572 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ Lecture Notes in Computer Science, vol 11375. Springer, Cham. https://doi.org/10 [2] Alessandro Ricci, Michele Piunti, and Mirko Viroli. 2011. Environment Programming in multi-agent systems: an artifact-based perspective. Autonomous Agents and Multi-Agent Systems, 23(2):158-192. - ## Prerequisites * JDK 21+ @@ -32,7 +31,6 @@ To build the project, just use: The default Gradle task `shadowJar` generates a fat-jar in the `build/libs` directory. - ## Running Yggdrasil To start an Yggdrasil node: @@ -58,6 +56,13 @@ Run with docker-compose (by default, it exposes the port `8899` of the host mach docker-compose up ``` +Use `docker-compose-dev.yml` for an environment that contains both libraries and sources: + +```shell +docker-compose -f docker-compose.yml -f docker-compose-dev.yml build +docker-compose -f docker-compose.yml -f docker-compose-dev.yml up +``` + ## HTTP API Overview The HTTP API implements CRUD operations for 2 types of resources: @@ -110,10 +115,9 @@ Using the discovered hub and topic IRIs, a client can subscribe for notification that contains a JSON payload with the following fields (see the [W3C WebSub recommendation](https://www.w3.org/TR/2018/REC-websub-20180123/)): - * `hub.mode` - * `hub.topic` - * `hub.callback` +* `hub.mode` +* `hub.topic` +* `hub.callback` When a resource is updated, Yggdrasil issues `POST` requests with the (updated) resource representation to all registered callbacks. - diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index abc3fadd..622f325d 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -1,21 +1,17 @@ version: '3.9' +# This file adds extensions needed for development and needs to be merged with docker-compose.yml +# See https://docs.docker.com/compose/multiple-compose-files/merge/ services: yggdrasil: - image: yggdrasil:dev - build: - context: . - dockerfile: Dockerfile_Dev - tty: true - container_name: yggdrasil + # overwrite entrypoint of productive image + entrypoint: ["tail", "-F", "anything"] + container_name: yggdrasil-dev + # Sync changes with host volumes: - - .:/opt/app/:cached -# # create a project volume to keep work -# # check if git can change line endings automatically -# - .:/opt/app/y:cached - environment: - - YGGDRASIL_VERSION=0.0.0 - - GRADLE_TASK=${GRADLE_TASK} - ports: - - "8899:8080" - - "8900:8088" + - .:/opt/app/:cached # does already exist but https://stackoverflow.com/a/39647631 + # restarting causes problems when using vscode's "Reopne in Container" + restart: "no" + + + diff --git a/docker-compose.yml b/docker-compose.yml index dcf34c23..8ff3dd71 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,9 +5,11 @@ services: image: yggdrasil:latest build: context: . + args: + - YGGDRASIL_VERSION=${YGGDRASIL_VERSION} container_name: yggdrasil environment: - - YGGDRASIL_VERSION=0.0.0 + - YGGDRASIL_VERSION=${YGGDRASIL_VERSION} ports: - "8899:8080" - "8900:8088"