Skip to content

Commit

Permalink
build: docker multistage builds (#320)
Browse files Browse the repository at this point in the history
* build: docker multistage builds

allow repeatable builds without a need to install java/maven locally

Signed-off-by: Andrew Berezovskyi <andriib@kth.se>

* fix: parent POM

Signed-off-by: Andrew Berezovskyi <andriib@kth.se>

* fix: test compose config

Signed-off-by: Andrew Berezovskyi <andriib@kth.se>

* fix: paths

Signed-off-by: Andrew Berezovskyi <andriib@kth.se>

---------

Signed-off-by: Andrew Berezovskyi <andriib@kth.se>
  • Loading branch information
berezovskyi authored Nov 16, 2024
1 parent 7d39f96 commit d5495ab
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 373 deletions.
43 changes: 4 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,17 @@ After following these steps, proceed to the steps listed in the next section _Na

If you wish to run 4 OSLC servers as 4 containers similar to how one would deploy them in the cloud, you could follow steps listed in this subsection. Prerequisites:

- JDK 17
- Maven 3
- JDK 17 (only for non-Docker builds)
- Maven 3.9 (only for non-Docker builds)
- Docker CE 19 or equivalent
- (Optional) Docker Compose
- Docker Compose v2

Docker Compose setup was also tested with Rancher Desktop 1.0.1.

```
cd src/
mvn clean install
# Using Docker Compose
docker compose up --build
# or, only using Docker
cd server-rm/
mvn clean package
docker build -t refimpl-server-rm .
docker run -d --name=oslc-refimpl-rm --rm -p 8800:8080 refimpl-server-rm
cd ../server-cm/
mvn clean package
docker build -t refimpl-server-cm .
docker run -d --name=oslc-refimpl-cm --rm -p 8801:8080 refimpl-server-cm
cd ../server-qm/
mvn clean package
docker build -t refimpl-server-qm .
docker run -d --name=oslc-refimpl-qm --rm -p 8802:8080 refimpl-server-qm
cd ../server-am/
mvn clean package
docker build -t refimpl-server-am .
docker run -d --name=oslc-refimpl-am --rm -p 8803:8080 refimpl-server-am
cd ..
```

Make sure all is up and running:
Expand All @@ -112,23 +88,12 @@ To see logs:

docker logs oslc-refimpl-am -f

To stop:

docker stop oslc-refimpl-am
docker stop oslc-refimpl-cm
docker stop oslc-refimpl-rm
docker stop oslc-refimpl-qm

### Running on Tomcat in Docker

```bash
cd src/
mvn clean install

cd server-rm/
mvn clean package -Pwith-jstl-impl
docker build -f tomcat.Dockerfile -t refimpl-server-rm .
docker run -p 8800:8080 refimpl-server-rm
docker compose -f docker-compose.tomcat.yml up --build
```

### Running on Tomcat via Maven
Expand Down
514 changes: 245 additions & 269 deletions model/representations.aird

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
overlays/
target/
*/target/
5 changes: 3 additions & 2 deletions src/client-toolchain/pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>co.oslc.refimpl</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>parent</artifactId>
<version>0.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
17 changes: 8 additions & 9 deletions src/docker-compose.tomcat.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
version: "3.9"
services:
server-rm:
build:
context: server-rm/
dockerfile: tomcat.Dockerfile
context: ./
dockerfile: ./server-rm/tomcat.Dockerfile
ports:
- "8800:8080"
server-cm:
build:
context: server-cm/
dockerfile: tomcat.Dockerfile
context: ./
dockerfile: ./server-cm/tomcat.Dockerfile
ports:
- "8801:8080"
server-qm:
build:
context: server-qm/
dockerfile: tomcat.Dockerfile
context: ./
dockerfile: ./server-qm/tomcat.Dockerfile
ports:
- "8802:8080"
server-am:
build:
context: server-am/
dockerfile: tomcat.Dockerfile
context: ./
dockerfile: ./server-am/tomcat.Dockerfile
ports:
- "8803:8080"
25 changes: 16 additions & 9 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
version: "3.9"
services:
server-rm:
build: server-rm/
build:
context: ./
dockerfile: server-rm/Dockerfile
ports:
- "8800:8080"
- "127.0.0.1:8800:8080"
server-cm:
build: server-cm/
build:
context: ./
dockerfile: server-cm/Dockerfile
ports:
- "8801:8080"
- "127.0.0.1:8801:8080"
server-qm:
build: server-qm/
build:
context: ./
dockerfile: server-qm/Dockerfile
ports:
- "8802:8080"
- "127.0.0.1:8802:8080"
server-am:
build: server-am/
build:
context: ./
dockerfile: server-am/Dockerfile
ports:
- "8803:8080"
- "127.0.0.1:8803:8080"
9 changes: 4 additions & 5 deletions src/lib-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- <parent>
<parent>
<groupId>co.oslc.refimpl</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>-->
<groupId>co.oslc.refimpl</groupId>
<version>0.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>lib-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OSLC RefImpl common library</name>
<packaging>jar</packaging>

Expand Down
14 changes: 12 additions & 2 deletions src/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>OSLC 2020 RefImpl (aggregator)</name>
<groupId>co.oslc.refimpl</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
Expand All @@ -21,8 +21,18 @@
<module>server-qm</module>
<module>server-am</module>
<module>client-toolchain</module>
</modules>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>co.oslc.refimpl</groupId>
<artifactId>lib-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<profiles>
<profile>
<id>acceptance</id>
Expand Down
9 changes: 7 additions & 2 deletions src/refimpl-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>co.oslc.refimpl</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.3.0-SNAPSHOT</version>
</parent>
<artifactId>refimpl-tests</artifactId>
<packaging>jar</packaging>
Expand Down Expand Up @@ -66,7 +66,12 @@
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
<!-- Testcontainers -->
<dependency>
<groupId>commons-io</groupId>
Expand Down
12 changes: 8 additions & 4 deletions src/refimpl-tests/src/test/groovy/OslcSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import java.time.Duration

@Testcontainers
class OslcSpec extends Specification {
public static final int STARTUP_TIMEOUT = 120

public static final String RM_SVC = "server-rm"
public static final String CM_SVC = "server-cm"
public static final String QM_SVC = "server-qm"
public static final String AM_SVC = "server-am"

// static final int RM_PORT = 8800
// static final int CM_PORT = 8801
// static final int QM_PORT = 8802
// static final int AM_PORT = 8803
// static private File composeFile = new File("../docker-compose.yml")

static final int RM_PORT = 8080
static final int CM_PORT = 8080
static final int QM_PORT = 8080
static final int AM_PORT = 8080

public static final int STARTUP_TIMEOUT = 120

// static private File composeFile = new File("../docker-compose.yml")
static private File composeFile = new File("src/test/resources/docker-compose.yml")

static private ComposeContainer environment =
Expand Down
16 changes: 12 additions & 4 deletions src/refimpl-tests/src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
version: "3.9"
services:
server-rm:
build: ../../../../server-rm/
build:
context: ../../../../
dockerfile: server-rm/Dockerfile
server-cm:
build: ../../../../server-cm/
build:
context: ../../../../
dockerfile: server-cm/Dockerfile
server-qm:
build: ../../../../server-qm/
build:
context: ../../../../
dockerfile: server-qm/Dockerfile
server-am:
build: ../../../../server-am/
build:
context: ../../../../
dockerfile: server-am/Dockerfile
4 changes: 1 addition & 3 deletions src/server-am/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
overlays/
src/
target/**/*
!target/*.war
target/
8 changes: 7 additions & 1 deletion src/server-am/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
FROM docker.io/library/maven:3-eclipse-temurin-21-alpine AS build

COPY . /src
WORKDIR /src
RUN mvn -B --no-transfer-progress -DskipTests clean package -pl server-am -am

FROM docker.io/library/jetty:12-jre21-eclipse-temurin
# WARNING DO NOT CHANGE WORKDIR or set it back to what it was before
# $JETTY_BASE must be correct before starting Jetty

COPY target/*.war /var/lib/jetty/webapps/ROOT.war
COPY --from=build /src/server-am/target/*.war /var/lib/jetty/webapps/ROOT.war

RUN java -jar "$JETTY_HOME/start.jar" --add-modules=ee9-deploy,ee9-jsp,ee9-jstl
8 changes: 7 additions & 1 deletion src/server-am/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<!-- Start of user code header
-->
<!-- TODO: Add additional header properties here to avoid them be overrriden upon future re-generation -->
<parent>
<groupId>co.oslc.refimpl</groupId>
<artifactId>parent</artifactId>
<version>0.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<!-- End of user code
-->
<groupId>co.oslc.refimpl</groupId>
Expand Down Expand Up @@ -142,7 +148,7 @@
<dependency>
<groupId>co.oslc.refimpl</groupId>
<artifactId>lib-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${project.version}</version>
</dependency>
<!-- End of user code
-->
Expand Down
10 changes: 8 additions & 2 deletions src/server-am/tomcat.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
FROM docker.io/library/maven:3-eclipse-temurin-17 AS build

COPY . /src
WORKDIR /src
RUN mvn -B --no-transfer-progress -DskipTests clean package -pl server-am -am -Pwith-jstl-impl

FROM docker.io/library/tomcat:10-jre17

# do not write log files, log everything to the Docker daemon
COPY config/tomcat-log.properties $CATALINA_BASE/conf/logging.properties
COPY --from=build /src/server-am/config/tomcat-log.properties $CATALINA_BASE/conf/logging.properties
ENV CATALINA_OUT=/dev/null

COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
COPY --from=build /src/server-am/target/*.war /usr/local/tomcat/webapps/ROOT.war
4 changes: 1 addition & 3 deletions src/server-cm/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
overlays/
src/
target/**/*
!target/*.war
target/
8 changes: 7 additions & 1 deletion src/server-cm/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
FROM docker.io/library/maven:3-eclipse-temurin-21-alpine AS build

COPY . /src
WORKDIR /src
RUN mvn -B --no-transfer-progress -DskipTests clean package -pl server-cm -am

FROM docker.io/library/jetty:12-jre21-eclipse-temurin
# WARNING DO NOT CHANGE WORKDIR or set it back to what it was before
# $JETTY_BASE must be correct before starting Jetty

COPY target/*.war /var/lib/jetty/webapps/ROOT.war
COPY --from=build /src/server-cm/target/*.war /var/lib/jetty/webapps/ROOT.war

RUN java -jar "$JETTY_HOME/start.jar" --add-modules=ee9-deploy,ee9-jsp,ee9-jstl
7 changes: 6 additions & 1 deletion src/server-cm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<!-- Start of user code header
-->
<!-- TODO: Add additional header properties here to avoid them be overrriden upon future re-generation -->
<parent>
<groupId>co.oslc.refimpl</groupId>
<artifactId>parent</artifactId>
<version>0.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<!-- End of user code
-->
<groupId>co.oslc.refimpl</groupId>
Expand Down Expand Up @@ -142,7 +148,6 @@
<dependency>
<groupId>co.oslc.refimpl</groupId>
<artifactId>lib-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- End of user code
-->
Expand Down
10 changes: 8 additions & 2 deletions src/server-cm/tomcat.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
FROM docker.io/library/maven:3-eclipse-temurin-17 AS build

COPY . /src
WORKDIR /src
RUN mvn -B --no-transfer-progress -DskipTests clean package -pl server-cm -am -Pwith-jstl-impl

FROM docker.io/library/tomcat:10-jre17

# do not write log files, log everything to the Docker daemon
COPY config/tomcat-log.properties $CATALINA_BASE/conf/logging.properties
COPY --from=build /src/server-cm/config/tomcat-log.properties $CATALINA_BASE/conf/logging.properties
ENV CATALINA_OUT=/dev/null

COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
COPY --from=build /src/server-cm/target/*.war /usr/local/tomcat/webapps/ROOT.war
Loading

0 comments on commit d5495ab

Please sign in to comment.