From 3300fe22151c47b0d1b93b93d68a8634de3aaeba Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Mon, 24 Jul 2023 08:18:29 -0400 Subject: [PATCH] rough in --env --- gdf.js | 60 ++++++++++++++++++++++++++ templates/Dockerfile.ejs | 37 +++++----------- test/base/bun/Dockerfile | 2 +- test/base/dev/Dockerfile | 2 +- test/base/distroless/Dockerfile | 4 +- test/base/ignore-scripts/Dockerfile | 2 +- test/base/instructions/Dockerfile | 2 +- test/base/legacy-peer-deps/Dockerfile | 2 +- test/base/litefs/Dockerfile | 7 ++- test/base/no-link/Dockerfile | 2 +- test/base/packages/Dockerfile | 2 +- test/base/port/Dockerfile | 2 +- test/base/swap/Dockerfile | 2 +- test/base/windows/Dockerfile | 2 +- test/fly/sqlite3/Dockerfile | 5 +-- test/frameworks/adonisjs/Dockerfile | 14 +++--- test/frameworks/express/Dockerfile | 2 +- test/frameworks/fastify/Dockerfile | 2 +- test/frameworks/gatsby/Dockerfile | 2 +- test/frameworks/nest/Dockerfile | 2 +- test/frameworks/next-npm/Dockerfile | 2 +- test/frameworks/next-pnpm/Dockerfile | 2 +- test/frameworks/next-yarn/Dockerfile | 2 +- test/frameworks/next-yarn3/Dockerfile | 2 +- test/frameworks/nuxt/Dockerfile | 2 +- test/frameworks/remix-epic/Dockerfile | 17 ++++---- test/frameworks/remix-indie/Dockerfile | 5 +-- test/frameworks/remix-pnpm/Dockerfile | 2 +- 28 files changed, 114 insertions(+), 75 deletions(-) diff --git a/gdf.js b/gdf.js index 537b530..276a67f 100755 --- a/gdf.js +++ b/gdf.js @@ -169,6 +169,66 @@ export class GDF { return packages.sort() } + sortEnv(env) { + if (Object.values(env).some(value => value.toString().includes("$"))) { + return Object.entries(env) + .map(([name, value]) => `${name}=${JSON.stringify(value)}`) + .join("\nENV ") + } else { + return Object.entries(env).sort((a, b) => a[0].localeCompare(b[0])) + .map(([name, value]) => `${name}=${JSON.stringify(value)}`) + .join(" \\\n ") + } + } + + get baseEnv() { + let env = { + NODE_ENV: "production" + } + + return this.sortEnv({ ...this.options.vars.base, ...env }) + } + + get buildEnv() { + let env = {} + + return this.sortEnv({ ...this.options.vars.build, ...env }) + } + + get deployEnv() { + let env = {} + + if (this.sqlite3) { + if (this.epicStack) { + env.DATABASE_FILENAME = "sqlite.db" + env.LITEFS_DIR = "/litefs" + env.DATABASE_PATH = "$LITEFS_DIR/$DATABASE_FILENAME" + env.DATABASE_URL = "file://$DATABASE_PATH" + env.CACHE_DATABASE_FILENAME = "cache.db" + env.CACHE_DATABASE_PATH = "$LITEFS_DIR/$CACHE_DATABASE_FILENAME" + env.PORT = this.port + 1 + } else { + env.DATABASE_URL = `file:///${this.litefs ? 'litefs' : 'data'}/sqlite.db` + if (this.litefs) env.PORT = this.port + 1 + } + } + + if (this.nuxtjs) { + env.HOST = 0 + } + + if (this.adonisjs) { + env.HOST = "0.0.0.0" + env.PORT = "3000" + env.CACHE_VIEWS = "true" + env.SESSION_DRIVER = "cookie" + env.DRIVE_DISK = "local" + if (this.postgres) env.DB_CONNECTION = "pg" + } + + return this.sortEnv({ ...this.options.vars.build, ...env }) + } + // what node version should be used? get nodeVersion() { const ltsVersion = '18.16.0' diff --git a/templates/Dockerfile.ejs b/templates/Dockerfile.ejs index e8f067f..1295daf 100644 --- a/templates/Dockerfile.ejs +++ b/templates/Dockerfile.ejs @@ -25,7 +25,7 @@ RUN apt-get update -qq && \ WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV <%- baseEnv %> <% if (yarn && yarnVersion != yarnClassic) { -%> ARG YARN_VERSION=<%= yarnVersion %> <% if(yarnVersion.startsWith('3.')) {-%> @@ -51,6 +51,10 @@ RUN npm install -g pnpm@$PNPM_VERSION # Throw-away build stage to reduce size of final image FROM base as build +<% } -%> +<% if (buildEnv.length > 0) { -%> +ENV <%- buildEnv %> + <% } -%> # Install packages needed to build node modules RUN apt-get update -qq && \ @@ -94,7 +98,7 @@ LABEL fly_launch_runtime="<%= runtime %>" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV <%- baseEnv %> <% } else { -%> FROM base <% } -%> @@ -130,23 +134,12 @@ VOLUME /app/tmp/uploads # Setup sqlite3 on a separate volume RUN mkdir -p /data<% if (litefs) { %> /litefs <% } %> VOLUME /data -<% if (epicStack) { -%> -ENV LITEFS_DIR="/litefs" -ENV DATABASE_FILENAME="sqlite.db" -ENV DATABASE_PATH="$LITEFS_DIR/$DATABASE_FILENAME" -ENV DATABASE_URL="file://$DATABASE_PATH" -ENV CACHE_DATABASE_FILENAME="cache.db" -ENV CACHE_DATABASE_PATH="$LITEFS_DIR/$CACHE_DATABASE_FILENAME" -ENV PORT=<%= port+1 %> -<% } else { -%> -ENV DATABASE_URL="file:///<%= litefs ? 'litefs' : 'data' %>/sqlite.db"<% if (litefs) { %> \ - PORT=<%= port+1 %> <% } %> <% } -%> - +<% if (sqlite3) { -%> <% if (remix) { -%> + # add shortcut for connecting to database CLI RUN echo "#!/bin/sh\nset -x\nsqlite3 \$DATABASE_URL" > /usr/local/bin/database-cli && chmod +x /usr/local/bin/database-cli - <% } -%> <% } -%> <% if (options.instructions.deploy) { -%> @@ -169,17 +162,7 @@ ENTRYPOINT [ <% if (litefs) { %>"litefs", "mount", "--", <% } %>"/app/<%= config <% } -%> # Start the server by default, this can be overwritten at runtime EXPOSE <%= port %> -<% if (nuxtjs) { -%> -ENV HOST=0 -<% } -%> -<% if (adonisjs) { -%> -ENV HOST="0.0.0.0" -ENV PORT="3000" -ENV CACHE_VIEWS="true" -ENV SESSION_DRIVER="cookie" -ENV DRIVE_DISK="local" -<% if (postgres) { -%> -ENV DB_CONNECTION="pg" -<% } -%> +<% if (deployEnv.length > 0) { -%> +ENV <%- deployEnv %> <% } -%> CMD <%- JSON.stringify(startCommand, null, 1).replaceAll(/\n\s*/g, " ") %> diff --git a/test/base/bun/Dockerfile b/test/base/bun/Dockerfile index ff80ab2..2461f71 100644 --- a/test/base/bun/Dockerfile +++ b/test/base/bun/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Bun" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/dev/Dockerfile b/test/base/dev/Dockerfile index 9c25489..6436e33 100644 --- a/test/base/dev/Dockerfile +++ b/test/base/dev/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/distroless/Dockerfile b/test/base/distroless/Dockerfile index d5c69aa..8387c22 100644 --- a/test/base/distroless/Dockerfile +++ b/test/base/distroless/Dockerfile @@ -8,7 +8,7 @@ FROM node:${NODE_VERSION}-slim as build WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Install packages needed to build node modules RUN apt-get update -qq && \ @@ -31,7 +31,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Copy built application COPY --from=build /app /app diff --git a/test/base/ignore-scripts/Dockerfile b/test/base/ignore-scripts/Dockerfile index 5d5c582..2fba0e0 100644 --- a/test/base/ignore-scripts/Dockerfile +++ b/test/base/ignore-scripts/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/instructions/Dockerfile b/test/base/instructions/Dockerfile index aa7a6a6..affa30c 100644 --- a/test/base/instructions/Dockerfile +++ b/test/base/instructions/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # base additions diff --git a/test/base/legacy-peer-deps/Dockerfile b/test/base/legacy-peer-deps/Dockerfile index 666c2f1..4b8b02c 100644 --- a/test/base/legacy-peer-deps/Dockerfile +++ b/test/base/legacy-peer-deps/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/litefs/Dockerfile b/test/base/litefs/Dockerfile index 0071824..6ad0309 100644 --- a/test/base/litefs/Dockerfile +++ b/test/base/litefs/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image @@ -46,9 +46,8 @@ COPY --from=build /app /app # Setup sqlite3 on a separate volume RUN mkdir -p /data /litefs VOLUME /data -ENV DATABASE_URL="file:///litefs/sqlite.db" \ - PORT=3001 - # Start the server by default, this can be overwritten at runtime EXPOSE 3000 +ENV DATABASE_URL="file:///litefs/sqlite.db" \ + PORT=3001 CMD [ "npm", "run", "start" ] diff --git a/test/base/no-link/Dockerfile b/test/base/no-link/Dockerfile index de95f92..5a89d66 100644 --- a/test/base/no-link/Dockerfile +++ b/test/base/no-link/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/packages/Dockerfile b/test/base/packages/Dockerfile index 87688ec..91856c5 100644 --- a/test/base/packages/Dockerfile +++ b/test/base/packages/Dockerfile @@ -15,7 +15,7 @@ RUN apt-get update -qq && \ WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/port/Dockerfile b/test/base/port/Dockerfile index f62fc29..f3fcf12 100644 --- a/test/base/port/Dockerfile +++ b/test/base/port/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/swap/Dockerfile b/test/base/swap/Dockerfile index 5faf6f1..7134624 100644 --- a/test/base/swap/Dockerfile +++ b/test/base/swap/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/base/windows/Dockerfile b/test/base/windows/Dockerfile index d2f5416..2d43252 100644 --- a/test/base/windows/Dockerfile +++ b/test/base/windows/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Remix/Prisma" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/fly/sqlite3/Dockerfile b/test/fly/sqlite3/Dockerfile index 100fc88..d472745 100644 --- a/test/fly/sqlite3/Dockerfile +++ b/test/fly/sqlite3/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image @@ -37,8 +37,7 @@ COPY --from=build /app /app # Setup sqlite3 on a separate volume RUN mkdir -p /data VOLUME /data -ENV DATABASE_URL="file:///data/sqlite.db" - # Start the server by default, this can be overwritten at runtime EXPOSE 3000 +ENV DATABASE_URL="file:///data/sqlite.db" CMD [ "npm", "run", "start" ] diff --git a/test/frameworks/adonisjs/Dockerfile b/test/frameworks/adonisjs/Dockerfile index 54b8dfb..533f8e3 100644 --- a/test/frameworks/adonisjs/Dockerfile +++ b/test/frameworks/adonisjs/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="AdonisJS" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image @@ -42,10 +42,10 @@ ENTRYPOINT [ "/app/docker-entrypoint.js" ] # Start the server by default, this can be overwritten at runtime EXPOSE 3000 -ENV HOST="0.0.0.0" -ENV PORT="3000" -ENV CACHE_VIEWS="true" -ENV SESSION_DRIVER="cookie" -ENV DRIVE_DISK="local" -ENV DB_CONNECTION="pg" +ENV CACHE_VIEWS="true" \ + DB_CONNECTION="pg" \ + DRIVE_DISK="local" \ + HOST="0.0.0.0" \ + PORT="3000" \ + SESSION_DRIVER="cookie" CMD [ "node", "/app/build/server.js" ] diff --git a/test/frameworks/express/Dockerfile b/test/frameworks/express/Dockerfile index b4c7cbf..1cf8aaa 100644 --- a/test/frameworks/express/Dockerfile +++ b/test/frameworks/express/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/fastify/Dockerfile b/test/frameworks/fastify/Dockerfile index 61ca958..cca4b89 100644 --- a/test/frameworks/fastify/Dockerfile +++ b/test/frameworks/fastify/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Node.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/gatsby/Dockerfile b/test/frameworks/gatsby/Dockerfile index 6b5dde6..0485d31 100644 --- a/test/frameworks/gatsby/Dockerfile +++ b/test/frameworks/gatsby/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Gatsby" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/nest/Dockerfile b/test/frameworks/nest/Dockerfile index c1680ca..a71ffb5 100644 --- a/test/frameworks/nest/Dockerfile +++ b/test/frameworks/nest/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="NestJS" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/next-npm/Dockerfile b/test/frameworks/next-npm/Dockerfile index 410d6ab..bf86bbe 100644 --- a/test/frameworks/next-npm/Dockerfile +++ b/test/frameworks/next-npm/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Next.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/next-pnpm/Dockerfile b/test/frameworks/next-pnpm/Dockerfile index 687ebbe..0c99e69 100644 --- a/test/frameworks/next-pnpm/Dockerfile +++ b/test/frameworks/next-pnpm/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Next.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Install pnpm ARG PNPM_VERSION=xxx diff --git a/test/frameworks/next-yarn/Dockerfile b/test/frameworks/next-yarn/Dockerfile index b117d07..4a73e66 100644 --- a/test/frameworks/next-yarn/Dockerfile +++ b/test/frameworks/next-yarn/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Next.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/next-yarn3/Dockerfile b/test/frameworks/next-yarn3/Dockerfile index 1287057..84d68c4 100644 --- a/test/frameworks/next-yarn3/Dockerfile +++ b/test/frameworks/next-yarn3/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Next.js" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" ARG YARN_VERSION=xxx # Install Yarn 3 diff --git a/test/frameworks/nuxt/Dockerfile b/test/frameworks/nuxt/Dockerfile index 54633bb..ad12e6c 100644 --- a/test/frameworks/nuxt/Dockerfile +++ b/test/frameworks/nuxt/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Nuxt" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image diff --git a/test/frameworks/remix-epic/Dockerfile b/test/frameworks/remix-epic/Dockerfile index 31e1b15..415e1f8 100644 --- a/test/frameworks/remix-epic/Dockerfile +++ b/test/frameworks/remix-epic/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Remix/Prisma" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image @@ -56,20 +56,19 @@ COPY --from=build /app /app # Setup sqlite3 on a separate volume RUN mkdir -p /data /litefs VOLUME /data -ENV LITEFS_DIR="/litefs" -ENV DATABASE_FILENAME="sqlite.db" -ENV DATABASE_PATH="$LITEFS_DIR/$DATABASE_FILENAME" -ENV DATABASE_URL="file://$DATABASE_PATH" -ENV CACHE_DATABASE_FILENAME="cache.db" -ENV CACHE_DATABASE_PATH="$LITEFS_DIR/$CACHE_DATABASE_FILENAME" -ENV PORT=3001 # add shortcut for connecting to database CLI RUN echo "#!/bin/sh\nset -x\nsqlite3 \$DATABASE_URL" > /usr/local/bin/database-cli && chmod +x /usr/local/bin/database-cli - # Entrypoint prepares the database. ENTRYPOINT [ "litefs", "mount", "--", "/app/docker-entrypoint.js" ] # Start the server by default, this can be overwritten at runtime EXPOSE 3000 +ENV DATABASE_FILENAME="sqlite.db" +ENV LITEFS_DIR="/litefs" +ENV DATABASE_PATH="$LITEFS_DIR/$DATABASE_FILENAME" +ENV DATABASE_URL="file://$DATABASE_PATH" +ENV CACHE_DATABASE_FILENAME="cache.db" +ENV CACHE_DATABASE_PATH="$LITEFS_DIR/$CACHE_DATABASE_FILENAME" +ENV PORT=3001 CMD [ "npm", "run", "start" ] diff --git a/test/frameworks/remix-indie/Dockerfile b/test/frameworks/remix-indie/Dockerfile index 2860090..57209e0 100644 --- a/test/frameworks/remix-indie/Dockerfile +++ b/test/frameworks/remix-indie/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Remix/Prisma" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image @@ -52,14 +52,13 @@ COPY --from=build /app /app # Setup sqlite3 on a separate volume RUN mkdir -p /data VOLUME /data -ENV DATABASE_URL="file:///data/sqlite.db" # add shortcut for connecting to database CLI RUN echo "#!/bin/sh\nset -x\nsqlite3 \$DATABASE_URL" > /usr/local/bin/database-cli && chmod +x /usr/local/bin/database-cli - # Entrypoint prepares the database. ENTRYPOINT [ "/app/docker-entrypoint.js" ] # Start the server by default, this can be overwritten at runtime EXPOSE 3000 +ENV DATABASE_URL="file:///data/sqlite.db" CMD [ "npm", "run", "start" ] diff --git a/test/frameworks/remix-pnpm/Dockerfile b/test/frameworks/remix-pnpm/Dockerfile index 394ce7f..e416c21 100644 --- a/test/frameworks/remix-pnpm/Dockerfile +++ b/test/frameworks/remix-pnpm/Dockerfile @@ -10,7 +10,7 @@ LABEL fly_launch_runtime="Remix" WORKDIR /app # Set production environment -ENV NODE_ENV=production +ENV NODE_ENV="production" # Install pnpm ARG PNPM_VERSION=xxx