Reputation: 491
When trying to console log an environment variable defined in docker-compose the node container returns the value as undefined.
docker-compose.yml
version: "3"
services:
api:
build: ./api
ports:
- "3004:3004"
volumes:
- type: bind
source: ./api
target: /usr/src/api
working_dir: /usr/src/api
environment:
REDIS_HOST: "redis://redis:6379"
TEST: "123"
depends_on:
- redis
networks:
- backend
redis:
image: "redis:latest"
ports:
- "6379:6379"
networks:
- backend
networks:
backend:
driver: bridge
I've tried changing the yaml syntax to - TEST="123" instead of the key value syntax just to make sure. I'm not sure what the issue could be.
The API dockerfile looks like this:
FROM node:12
# create destination directory
RUN mkdir -p /usr/src/api
WORKDIR /usr/src/api
# copy the app, note .dockerignore
COPY . /usr/src/api
# Install packages
RUN npm i yarn --save
RUN yarn
# COPY . /src
EXPOSE 3005
CMD ["yarn", "dev"]
NodeJS Snippet:
console.log("redis host! + test");
console.log(process.env.REDIS_HOST, process.env.TEST)
Console log output:
api_1 | redis host! + test
api_1 | undefined undefined
Upvotes: 5
Views: 4304
Reputation: 23875
You need to additionally reference the environment variables in the Dockerfile
using ARG
ARG REDIS_HOST
ARG TEST
Upvotes: 5