4

I need to run a docker container in AWS ECS. I do NOT have access to the source code for the image. This is a private image, from a private repo that I have uploaded to AWS ECR. I have created an AWS ECS Task Definition to run the container inside a service, inside a cluster. The image shows as being up and running but I cannot hit it via my browser. I know that all the network settings are correct because I can hit a simple hello world app that I also deployed to test.

There is also a command I need to run before: docker run --env-file <environment_variables_file> <image>:<tag> rake db:reset && rake db:seed.

According to the instructions for this docker image, the run command for it is: docker run -d --name <my_image_name> --env-file <environment_variables_file> -p 8080:80 <image>:<tag>.

I can run this image locally on my laptop with no issues, deploying it to AWS is that problem. My question is how do I provide the environment_variables_file to the image? Where do I upload the file and how do I pass it? How do I run the command to init the DB before the image runs?

El Moreno
  • 405
  • 3
  • 8
  • 18

2 Answers2

13

Since Nov 2020, ECS does support env files (blog post), but they must be hosted on S3:

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html


Pasting the essentials for reference. Under container definition:

"environmentFiles": [
  {
    "value": "arn:aws:s3:::s3_bucket_name/envfile_object_name.env",
    "type": "s3"
  }
]

The task execution role also needs the following permission:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject"
      ],
      "Resource": [
        "arn:aws:s3:::s3_bucket_name/envfile_object_name.env"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetBucketLocation"
      ],
      "Resource": [
        "arn:aws:s3:::s3_bucket_name"
      ]
    }
  ]
}
Martti Laine
  • 12,655
  • 22
  • 68
  • 102
1

Amazon ECS doesn't support environment variable files. You can set environment variables inside task definition. For example:

"environment" : [
    { "name" : "string", "value" : "string" },
    { "name" : "string", "value" : "string" }
]

Please read following instructions for more details.

Update:

AWS now provides a way -

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html

Nandesh
  • 4,437
  • 2
  • 20
  • 26