'ecs container env vars aren't readable from node container
container definition
self.fargate_host_definition.add_container(
...,
environment={
'KEY1':'REACT_APP_API_URL',
'VALUE1':api_url,
},
...,
)
dockerfile:
FROM node:16
WORKDIR /usr/src/client
COPY createEnv.js .
RUN node createEnv.js
COPY package*.json ./
RUN npm i
COPY . .
RUN npm run build
RUN npm i -g serve
EXPOSE 3000
CMD ["serve", "-s", "build"]
createEnv.js
const fs = require("fs");
const createEnvFile = () => {
console.log("process.env");
console.log(process.env);
console.log("KEY1:");
console.log(process.env.KEY1);
console.log("VALUE1:");
console.log(process.env.VALUE1);
let data = process.env.KEY1 + "=" + process.env.VALUE1;
fs.writeFile(".env", data, (err) => {
if (err) console.log(err);
else {
return;
}
});
};
createEnvFile();
Am I missing something? I saw this post saying it should be possible to read ecs container env vars from node within docker but when I run this through my pipeline, I'm getting nothing but undefined for my env vars. my process.env doesn't even have the container env vars listed.
Am I missing something here? I know for sure the container level env vars are there because after my build succeeds, I checked aws to make sure they were there.
edit: here's a copy of the process.env
{
NODE_VERSION: '16.14.2',
HOSTNAME: 'f04c86a5a885',
YARN_VERSION: '1.22.18',
HOME: '/root',
PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
PWD: '/usr/src/client'
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
