Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added standalone test for NextJS, making the image much smaller #59

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ export class GDF {
return !!this.#pj.dependencies?.next
}

get standaloneNextjs() {
if (!this.nextjs) return false

if (fs.existsSync(path.join(this._appdir, 'next.config.mjs'))) {
const config = fs.readFileSync(path.join(this._appdir, 'next.config.mjs'), 'utf-8')
return /output\s*:\s*(["'`])standalone\1/.test(config)
} else if (fs.existsSync(path.join(this._appdir, 'next.config.js'))) {
const config = fs.readFileSync(path.join(this._appdir, 'next.config.js'), 'utf-8')
return /output\s*:\s*(["'`])standalone\1/.test(config)
} else return false
}

// Does this application use nuxt.js?
get nuxtjs() {
return !!this.#pj.dependencies?.nuxt
Expand Down
6 changes: 6 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ ENV PORT=4321
ENV HOST=0.0.0.0
<% } else if (svelte) { -%>
COPY --from=build /app/build /app/build
<% } else if (standaloneNextjs) { -%>
COPY --from=build /app/.next/standalone /app
COPY --from=build /app/.next/static /app/.next/static
COPY --from=build /app/public /app/public
<% } else { -%>
COPY --from=build /app /app
<% } -%>
Expand Down Expand Up @@ -235,6 +239,8 @@ EXPOSE <%= port %>
<% } -%>
<% if (foreman) { -%>
CMD [ "<%= npx %>", "foreman", "start", "--procfile", "Procfile.prod" ]
<% } else if (standaloneNextjs) { -%>
CMD [ "node", "server.js" ]
<% } else if (Array.isArray(startCommand)) { -%>
CMD <%- JSON.stringify(startCommand, null, 1).replaceAll(/\n\s*/g, " ") %>
<% } else { -%>
Expand Down
36 changes: 36 additions & 0 deletions test/frameworks/next-standalone/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
47 changes: 47 additions & 0 deletions test/frameworks/next-standalone/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=20.9.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --omit=dev


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app/.next/standalone /app
COPY --from=build /app/.next/static /app/.next/static
COPY --from=build /app/public /app/public

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "node", "server.js" ]
6 changes: 6 additions & 0 deletions test/frameworks/next-standalone/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone"
};

export default nextConfig;
Loading
Loading