19
votes

In the task definition on ECS, I have provided Environment variable as following:

Key as HOST_NAME and Value as something.cloud.com

On my local I use this docker run command and I'm able to pass in my env variables, but through task definition the variables are not being passed to container.

The docker run command below works on local, but how do I set it up in the task definition in AWS ECS?

docker run -e HOST_NAME=something.cloud.com sid:latest

3
Please post the task definition.Andy Shinn
My task def is simple , it has a nginx image ,, host and container port is set to 80 80 ,, and in the field for environment variable I have given Key as HOST_NAME and Value as something.cloud.com. Should I use square braces for key value.governingcloud

3 Answers

29
votes

You should call it name and not key, see example below

 {
  "name": "nginx",
  "image": "",
  "portMappings": [
    {
      "containerPort": 80,
      "hostPort": 80
    }
  ],
  "environment": [
    {
      "name": "HOST_NAME",
      "value": "something.cloud.com"
    }
    ]
 }
0
votes

If you used the new docker compose integration with ECS, then you will need to update the stack.

It is smart enough to update only the parts that changed. For my case, this was the task definition not picking new environment variables set on a .env file and mounted on the docker container

Run the same command you used to create the stack, only that this time round it'll update it(only parts that changed)

docker compose --context you-ecs-context up -f your.docker-compose.yml

For more: https://docs.docker.com/engine/context/ecs-integration/#rolling-update

-3
votes

You can set hostname var at task definition JSON file

hostname
Type: string

Required: no

The hostname to use for your container. This parameter maps to Hostname in the Create a container section of the Docker Remote API and the --hostname option to docker run.