Skip to content

Commit

Permalink
Optimized docker image size.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenstott committed Aug 28, 2024
1 parent 218c69c commit 1f1b24c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 40 deletions.
60 changes: 26 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ RUN cargo install cargo-chef

# planning stage
FROM chef AS planner
COPY Cargo.toml .
COPY Cargo.lock .
COPY crates ./crates
COPY Cargo.toml Cargo.lock crates ./app/
RUN cargo chef prepare --recipe-path recipe.json

# caching stage
Expand All @@ -20,48 +18,42 @@ FROM chef AS builder
COPY . .
RUN cargo build --release --bin ndc-calcite --bin ndc-calcite-cli

# java-build stage
FROM debian:trixie-slim AS java-build
COPY scripts/java_env.sh ./scripts/
RUN apt-get update && apt-get install -y openjdk-21-jdk maven ca-certificates
RUN . /scripts/java_env_jdk.sh
RUN java -version && mvn --version
COPY calcite-rs-jni/ /calcite-rs-jni/
WORKDIR /calcite-rs-jni/calcite
RUN ./gradlew assemble --no-daemon
WORKDIR /calcite-rs-jni
RUN mvn clean install -DskipTests

# Put all the jars into target/dependency folder
RUN mvn dependency:copy-dependencies

# runtime stage
FROM debian:trixie-slim AS runtime
RUN apt-get update && apt-get install -y openjdk-21-jdk maven ca-certificates && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# set JAVA_HOME based on architecture
ENV JAVA_HOME_ARM64=/usr/lib/jvm/java-21-openjdk-arm64
ENV JAVA_HOME_AMD64=/usr/lib/jvm/java-21-openjdk-amd64
RUN if [ "$(uname -m)" = "aarch64" ]; then \
echo "Setting JAVA_HOME for ARM64"; \
ln -s ${JAVA_HOME_ARM64} /usr/local/java_home; \
else \
echo "Setting JAVA_HOME for AMD64"; \
ln -s ${JAVA_HOME_AMD64} /usr/local/java_home; \
fi
COPY scripts/java_env.sh ./scripts/

ENV JAVA_HOME=/usr/local/java_home
ENV MAVEN_HOME=/usr/share/maven
ENV PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
RUN apt-get update && \
apt-get install -y coreutils openjdk-21-jre-headless ca-certificates && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/*

# Verify Java and Maven installations
RUN java -version && echo $JAVA_HOME
RUN mvn --version
RUN . /scripts/java_env_jre.sh && \
mkdir -p /calcite-rs-jni/target && \
mkdir -p /etc/ndc-calcite

COPY --from=builder /app/target/release/ndc-calcite /usr/local/bin
COPY --from=builder /app/target/release/ndc-calcite-cli /usr/local/bin
RUN mkdir -p /etc/ndc-calcite
COPY --from=java-build /calcite-rs-jni/target/ /calcite-rs-jni/target/

ENV HASURA_CONFIGURATION_DIRECTORY=/etc/connector
ENV RUST_BACKTRACE=full
COPY calcite-rs-jni/ /calcite-rs-jni/
WORKDIR /calcite-rs-jni/calcite

RUN ./gradlew assemble --no-daemon
WORKDIR /calcite-rs-jni
RUN mvn clean install -DskipTests && \
mvn dependency:copy-dependencies && \
rm -rf /root/.m2 && \
rm -rf /usr/share/maven-repo && \
rm -rf /calcite-rs-jni/calcite /calcite-rs-jni/src

WORKDIR /app

ENTRYPOINT ["ndc-calcite"]
CMD ["serve"]
CMD ["serve"]
8 changes: 8 additions & 0 deletions build-local.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# prevents local build artifacts being added to image
cd calcite-rs-jni
mvn clean
cd calcite
./gradlew clean

create a tag name from the last connector release
cd ../..
#release_info=$(curl -L \
# -H "Accept: application/vnd.github+json" \
# -H "Authorization: Bearer $GITHUB_TOKEN" \
Expand Down
12 changes: 6 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# prevents local build artifacts being added to image
#cd calcite-rs-jni
#mvn clean
#cd calcite
#./gradlew clean
cd calcite-rs-jni
mvn clean
cd calcite
./gradlew clean

# create a tag name from the last connector release
create a tag name from the last connector release
cd ../..
release_info=$(curl -L \
-H "Accept: application/vnd.github+json" \
Expand All @@ -14,7 +14,7 @@ release_info=$(curl -L \
TAG=$(echo "$release_info" | grep 'tag_name' | awk -F':' '{print $2}' | tr -d ' "",')

# build arm & amd versions
docker build . --platform linux/arm64,linux/amd64 -t kstott/meta_connector:latest
docker build . --no-cache --platform linux/arm64,linux/amd64 -t kstott/meta_connector:latest
#docker buildx build --platform linux/arm64 --output type=oci,dest=./image.tar .
docker tag kstott/meta_connector:latest kstott/meta_connector:"$TAG"

Expand Down
15 changes: 15 additions & 0 deletions scripts/java_env_jdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
# Setting JAVA_HOME based on architecture
export JAVA_HOME_ARM64=/usr/lib/jvm/java-21-openjdk-arm64
export JAVA_HOME_AMD64=/usr/lib/jvm/java-21-openjdk-amd64
if [ "$(uname -m)" = "aarch64" ]; then
# Setting for ARM64
echo "Setting JAVA_HOME for ARM64"
ln -s ${JAVA_HOME_ARM64} /usr/local/java_home
else
# Setting for AMD64
echo "Setting JAVA_HOME for AMD64"
ln -s ${JAVA_HOME_AMD64} /usr/local/java_home
fi
export JAVA_HOME=/usr/local/java_home
export PATH=$JAVA_HOME/bin:$PATH
15 changes: 15 additions & 0 deletions scripts/java_env_jre.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
# Setting JAVA_HOME based on architecture
export JAVA_HOME_ARM64=/usr/lib/jvm/jre-21-openjdk-arm64
export JAVA_HOME_AMD64=/usr/lib/jvm/jre-21-openjdk-amd64
if [ "$(uname -m)" = "aarch64" ]; then
# Setting for ARM64
echo "Setting JAVA_HOME for ARM64"
ln -s ${JAVA_HOME_ARM64} /usr/local/java_home
else
# Setting for AMD64
echo "Setting JAVA_HOME for AMD64"
ln -s ${JAVA_HOME_AMD64} /usr/local/java_home
fi
export JAVA_HOME=/usr/local/java_home
export PATH=$JAVA_HOME/bin:$PATH

0 comments on commit 1f1b24c

Please sign in to comment.