1

I'm using docker with AWS ECS and I want to pass env variables using docker to my nodejs application.

My app.js is simple, it have route that tell me the value of SOME variable:

 app.get('/test', (req, res) => res.status(200).send(`${process.env.SOME} OK`));

This is how I update the AWS ECS service:

 docker-compose -f .docker/docker-compose.myweb.ecr.yml build www-app
 docker-compose -f .docker/docker-compose.myweb.ecr.yml push www-app
 aws ecs update-service --cluster web --service www --force-new-deployment

Dockerfile:

FROM node:latest

# ENV SOME=3000 <--- THIS IS WORKS, BUT I WANT TO DO IT IN COMPOSE FILE.

WORKDIR /usr/src/app

COPY ./dist/myapp/package.json .

RUN npm install

COPY ./dist/myapp .

EXPOSE 3000

CMD [ "npm", "start" ]

docker-compose.myweb.ecr.yml:

version: '3'

services:
  www-app:
    # env_file: ../.env
    image: 32934544.dkr.ecr.us-east-2.amazonaws.com/www-app:latest
    environment:
      - SOME=6000
    build:
      context: ../
      dockerfile: ./.docker/Dockerfile

The problem is I got undefined from process.env.SOME. How to fix that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jon Sud
  • 10,211
  • 17
  • 76
  • 174

1 Answers1

0

You need to use the 'args' as specified here:

From [1]

If your service specifies a build option, variables defined in environment are not automatically visible during the build. Use the args sub-option of build to define build-time environment variables.

shariqmaws
  • 8,152
  • 1
  • 16
  • 35