Pass env from system to api routes
parent
36aa35f2c3
commit
6f56e4359f
|
@ -36,9 +36,14 @@ COPY --from=builder /app/public ./public
|
|||
COPY --from=builder /app/.next/standalone ./
|
||||
COPY --from=builder /app/.next/static ./.next/static
|
||||
|
||||
COPY --from=builder /app/bin/dumpEnv.js ./bin/dumpEnv.js
|
||||
COPY --from=builder /app/dockerEntrypoint.sh .
|
||||
RUN ["chmod", "+x", "./dockerEntrypoint.sh"]
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
ENV PORT 3000
|
||||
ENV HOSTNAME localhost
|
||||
|
||||
ENTRYPOINT ["./dockerEntrypoint.sh"]
|
||||
CMD ["node", "server.js"]
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
// NOTE: Next.js does something super weird in production/standalone mode where process.env is cleared
|
||||
// of system process.env. The only reasonable workaround I found for this is to dump an .env file
|
||||
// in the deployment directory before running server (next.js loads this into process.env automatically)
|
||||
const envVars = [
|
||||
"DATA_PATH",
|
||||
// TODO: more
|
||||
];
|
||||
|
||||
console.log("Preparing env variables…");
|
||||
|
||||
const envDeclarations = envVars
|
||||
.map((name) => {
|
||||
const val = process.env[name];
|
||||
if (!val) {
|
||||
throw new Error(`Missing env variable ${name}`);
|
||||
}
|
||||
return `${name}=${val}`;
|
||||
})
|
||||
.join("\n");
|
||||
|
||||
const shouldOutputHere = process.argv.includes('--docker')
|
||||
const outputDir = shouldOutputHere ? '' : '.next/standalone'
|
||||
|
||||
console.log(`Dumping env variables to ${outputDir}...`);
|
||||
const envPath = path.join(__dirname, "..", outputDir, ".env");
|
||||
fs.writeFileSync(envPath, envDeclarations);
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/sh
|
||||
# no verbose
|
||||
set +x
|
||||
node bin/dumpEnv.js --docker
|
||||
echo "Starting Nextjs"
|
||||
exec "$@"
|
|
@ -2,7 +2,9 @@
|
|||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build"
|
||||
"build": "next build",
|
||||
"build-prod": "npm run build && cp -r public .next/standalone && cp -r .next/static .next/standalone/.next",
|
||||
"start-prod": "node bin/dumpEnv.js && node .next/standalone/server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "latest",
|
||||
|
|
|
@ -2,16 +2,10 @@
|
|||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import getConfig from 'next/config';
|
||||
// const { serverRuntimeConfig } = getConfig();
|
||||
|
||||
// console.log({
|
||||
// env: process.env,
|
||||
// serverRuntimeConfig,
|
||||
// })
|
||||
console.log(`Note: data path is ${process.env.DATA_PATH}`)
|
||||
|
||||
// TODO: Why don't env variables propagate to here?
|
||||
// const messagePath = path.join(serverRuntimeConfig.dataPath, 'message')
|
||||
const messagePath = '/data/message'
|
||||
const messagePath = path.join(process.env.DATA_PATH, 'message')
|
||||
|
||||
function getFileData() {
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue