Skip to content

Instantly share code, notes, and snippets.

@sampaiodiego
Created August 28, 2023 17:15
Show Gist options
  • Save sampaiodiego/8e1df929a95f60ca4a259e3124631516 to your computer and use it in GitHub Desktop.
Save sampaiodiego/8e1df929a95f60ca4a259e3124631516 to your computer and use it in GitHub Desktop.
nodejs + docker env var weird behavior

Build Docker image:

docker build -t read-env .

Run it with the env var set:

docker run --rm -d -e "foo-bar=ok" --name readenv read-env

Check logs:

$ docker logs readenv
list all env vars: {
  NODE_VERSION: '20.5.1',
  HOSTNAME: '44058092d956',
  YARN_VERSION: '1.22.19',
  HOME: '/root',
  PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  PWD: '/'
}
read env var: foo-bar
undefined

Attach running NodeJS and check env vars:

$ docker exec -it readenv node
Welcome to Node.js v20.5.1.
Type ".help" for more information.
> process.env['foo-bar']
'ok'
FROM node
COPY main.js .
CMD ["node", "main.js"]
console.log('list all env vars:', process.env);
console.log('read env var: foo-bar');
console.log(process.env['foo-bar']);
setInterval(() => {}, 1 << 30);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment