Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
# Start with Go image to build the binary
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS builder

# Install build dependencies including bash
RUN apk add --no-cache make git bash

# Set working directory
FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS go-builder
ARG TARGETOS
ARG TARGETARCH
WORKDIR /app
RUN apk add --no-cache make git bash

# Clone the repository
RUN git clone https://github.com/akave-ai/akavesdk .
# Copy all files to the container
COPY . /app/

# Set the target platform for the build
ARG TARGETOS
ARG TARGETARCH
# Compile the Go application
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH make build

# Final image
FROM --platform=$TARGETPLATFORM alpine:3.19
# Install Node.js dependencies and compile TypeScript (if present)
FROM node:16-alpine AS node-builder
WORKDIR /app

# Install Node.js and npm
RUN apk add --no-cache nodejs npm
# Copy Node.js package files
COPY package*.json ./

# Install all dependencies including development ones necessary for build
RUN npm install

# Copy the rest of the application files
COPY . .

# Copy binary from builder
COPY --from=builder /app/bin/akavecli /usr/local/bin/akavecli
# Compile TypeScript only if tsconfig.json is present
RUN if [ -f tsconfig.json ]; then \
npm install -g typescript && tsc; \
else \
echo "No TypeScript files detected"; \
fi

# Set working directory
# Stage 3: Final runtime image
FROM alpine:3.19 AS runtime
WORKDIR /app

# Copy package files first (better layer caching)
COPY package*.json ./
# Install Node.js and npm for the runtime environment
RUN apk add --no-cache nodejs npm

# Install only production dependencies
RUN npm install --production
# Copy binary from Go builder
COPY --from=go-builder /app/bin/akavecli /usr/local/bin/akavecli

# Copy source files
COPY server.js ./
COPY index.js ./
COPY web3-utils.js ./
# If TypeScript compiled, use it, otherwise use source
COPY --from=node-builder /app/dist/ ./dist
COPY --from=node-builder /app/src/ ./src

# Copy package files and install only production dependencies
COPY package*.json ./
RUN npm install --only=production

# Environment variables with defaults
# Set environment variables
ENV NODE_ADDRESS=""
ENV PRIVATE_KEY=""
ENV PORT=3000
Expand All @@ -49,7 +59,7 @@ EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1

# Start the API server
CMD ["node", "server.js"]
# Prefer compiled JS from dist, fallback to src
CMD ["sh", "-c", "if [ -d dist ]; then node dist/server.js; else node src/server.js; fi"]