* Builds a Docker image * @param {Object} params - Build parameters * @param {string} params.containerName - Name of the container * @param {string} params.containerPath - Path to container source * @param {string} params.imageUri - Full image URI with tag * @param {string} [params.do
({
containerName,
containerPath,
imageUri,
dockerFileString = null,
buildArgs = {},
buildOptions = [],
aiFramework = null,
platform = 'linux/amd64',
builder = null,
})
| 146 | * @returns {Promise<string>} The URI of the built image |
| 147 | */ |
| 148 | async buildImage({ |
| 149 | containerName, |
| 150 | containerPath, |
| 151 | imageUri, |
| 152 | dockerFileString = null, |
| 153 | buildArgs = {}, |
| 154 | buildOptions = [], |
| 155 | aiFramework = null, |
| 156 | platform = 'linux/amd64', |
| 157 | builder = null, |
| 158 | }) { |
| 159 | try { |
| 160 | if (aiFramework === 'mastra') { |
| 161 | dockerFileString = ` |
| 162 | # Builder stage: compile TypeScript and build the application |
| 163 | FROM node:lts-alpine AS builder |
| 164 | |
| 165 | # Set working directory |
| 166 | WORKDIR /app |
| 167 | |
| 168 | # Copy dependency manifests for layer caching |
| 169 | COPY package.json package-lock.json ./ |
| 170 | |
| 171 | # Install all dependencies (including dev dependencies for build) |
| 172 | RUN npm i |
| 173 | |
| 174 | # Copy configuration files |
| 175 | COPY tsconfig.json ./ |
| 176 | |
| 177 | # Copy source code |
| 178 | COPY src/ ./src/ |
| 179 | |
| 180 | # Build the application using mastra as specified in package.json |
| 181 | RUN npx mastra build |
| 182 | |
| 183 | # Production stage: lightweight image with only necessary components |
| 184 | FROM node:lts-alpine AS production |
| 185 | |
| 186 | # Set environment variables |
| 187 | ENV NODE_ENV=production |
| 188 | |
| 189 | # Set working directory |
| 190 | WORKDIR /app |
| 191 | |
| 192 | # Create a non-root user for security |
| 193 | RUN addgroup -S appgroup && \ |
| 194 | adduser -S -G appgroup appuser |
| 195 | |
| 196 | # Copy the built application from the builder stage |
| 197 | COPY --from=builder /app/.mastra/output /app |
| 198 | |
| 199 | # Set ownership to non-root user |
| 200 | RUN chown -R appuser:appgroup /app |
| 201 | |
| 202 | # Switch to non-root user |
| 203 | USER appuser |
| 204 | |
| 205 | # Expose port if needed (uncomment and set the appropriate port) |
no test coverage detected