.env not available in Elastic Beanstalk with Docker

2

Hello,

I am trying to use the Elastic Beanstalk environment variables in a container on the Docker platform for EB with docker compose.

Aaccording to the documentation, the EB environment variables can be passed into the container by including .env as an environment file (https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker.container.console.html#docker-env-cfg.env-variables). However, this does not work for me as the .env file can't be found (and EB fails). I logged into the EC2 instance and could in fact not find the file. Upon researching this issue, I found the suggestion to use /opt/elasticbeanstalk/deployment/env.list instead. However, this file only seems to be created after the first launch of EB (EB fails with an error message that the file cannot be found but file exists when logging into the EC2 instance).

This is my docker-compose.yml:

version: '3.7'

services:
  gui-app:
    image: xxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/gui-app:latest
    ports:
      - 80:8080
    depends_on:
      - xray-daemon
    environment:
      - AWS_XRAY_DAEMON_ADDRESS=xray-daemon:2000
    env_file:
      - .env
    links:
      - xray-daemon

  xray-daemon:
    image: xxxxxxxxxx.dkr.ecr.eu-central-1.amazonaws.com/aws-xray-daemon:latest
    ports:
      - 2000/udp

Any help is appreciated. Thank you!

sblanc
asked 2 years ago1900 views
2 Answers
0

I was having the same issue as you, but using the absolute path /opt/elasticbeanstalk/deployment/env.list does work for me. Here's my docker-compose.yml:

version: '3.8'
services:
  beanstalk-example-dotnet-api:
    image: "xxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/example-dotnet-api:latest"
    ports:
      - "80:80"
    env_file:
      - /opt/elasticbeanstalk/deployment/env.list

A big issue, however, is that when I update the EB environment variables, the env.list file does not get updated to reflect the updated environment variables values. From what I can tell, I need to rebuild the EB environment to get an updated env.list file. This is definitely far from ideal.

answered 9 months ago
-2

Hello,

From your question it seems that the Beanstalk is unable to find the .env file in your application bundle. The document specified mentions it is a must to follow one or both of these configuration approaches.

  1. Add the .env file generated by Elastic Beanstalk to the env_file configuration option in the docker- compose.yml file.

  2. Directly define the environment variables in the docker-compose.yml file.

However I can see that you have you have chosen to follow the first approach. In that case I would refer to please check the file where it has been placed as the file ".env" must be placed in the same directory from where you are calling the "docker-compose.yml" file. This issue mainly occurs due to ".env" file. not being recognised/found as you are including a .env file in your application bundle, hence, not allowing Elastic Beanstalk to generate an ".env" file. Please note to be careful to not leave spaces between the equal sign (=) and the value assigned to your variable in order to prevent spaces from being added to the string. This can also result in issues while creating the environment.

I would suggest you to directly define the environment variables in the "docker-compose.yml file" as mentioned in the Second approach. Please refer to the following document for any help in performing the above mentioned task of defining environment variables.

[+] https://docs.docker.com/compose/environment-variables/

AWS
SUPPORT ENGINEER
answered 2 years ago
  • So, what should I put into docker-compose.yaml to point to ".env file generated by Elastic Beanstalk"? Where does beanstalk generate .env file? The problem is that beanstalk runs "Running command /bin/sh -c docker-compose -f /opt/elasticbeanstalk/deployment/app_source_bundle config -q" before generating environment file.

    which fails with "Couldn't find env file: /opt/elasticbeanstalk/deployment/.env and then proceeds "Executing instruction: Generate environment file", which does not generate .env file, but does generate /opt/elasticbeanstalk/deployment/env.list.

  • docker-compose config fails because environment file is not generated yet and because of that failure the file is not recognized as docker-compose file, so beanstalk tries to use it as Dockerfile and fails. It looks like a bug and there is no way to use docker-compose with the environment file generated by Beanstalk.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions