0

i have this docker file

FROM node:14-slim AS ui-build
WORKDIR /usr/src
COPY ui/ ./ui/
RUN cd ui && npm install && npm run build


FROM node:14-slim AS api-build
WORKDIR /usr/src
COPY api/ ./api/
ENV ENVIRONMENT test
ENV URI test

RUN cd api && npm install && npm run build
RUN ls

FROM node:14-slim
WORKDIR /root/
COPY --from=ui-build /usr/src/ui/build ./ui/build
COPY --from=api-build /usr/src/api/dist .
RUN ls

EXPOSE 80

CMD ["node", "api.bundle.js"]

and this task definition in terraform

resource "aws_ecs_task_definition" "main" {
  family                   = var.task_name
  network_mode             = var.net_mode
  requires_compatibilities = [var.ecs_type]
  cpu                      = var.container_cpu
  memory                   = var.container_memory
  execution_role_arn       = aws_iam_role.ecs_task_execution_role.arn
  container_definitions = jsonencode([{
    name        = var.container_name
    image       = var.container_image
    essential   = var.essential
    environment = [{"name": "ENVIRONMENT", "value": "${var.environment}"}, {"name": "URI", "value": "${var.uridb}"}] //this envs will be pass to the container to select deploy enviroment
    portMappings = [{
      protocol      = var.protocol
      containerPort = tonumber(var.container_port)
      hostPort      = tonumber(var.container_host_port)
    }]
    logConfiguration = {
      logDriver = var.log_driver
      options = {
        awslogs-group         = aws_cloudwatch_log_group.main_lgr.name
        awslogs-stream-prefix = "ecs"
        //awslogs-create-group = "true" // creates new log group with awslogs-grou
        awslogs-region        = var.region
      }
    }
  }])

  tags = {
    Environment = var.environment
  }

  depends_on = [aws_iam_role.ecs_task_execution_role]
}

taking a look inside my container it would seen that the envs in my docker file have presedence over the ones in the task definition

container log

task defnition on aws

how can i make my task definition ENVS have priority over the ones in the container once i run my service?

Marko E
  • 13,362
  • 2
  • 19
  • 28
randomuser
  • 19
  • 2
  • 2
    Please post any errors or code blocks as text not screenshots. – Marko E Nov 03 '22 at 07:08
  • Are you using Fargate or ECS? – Marko E Nov 03 '22 at 09:15
  • 1
    I'm guessing this is the same problem as your other question: https://stackoverflow.com/questions/74296939/i-cant-read-the-env-variables-on-my-ecs-task where you are reading environment variables at build time, instead of run time, so it's going to pick up the variables in your Dockerfile and then use those as hard-coded values in the build output. – Mark B Nov 03 '22 at 13:39

0 Answers0