Search code examples
amazon-web-servicesdockeramazon-ecs

ECS: Environment variables set in task but not present in container


I have a task definition that looks like this:

> aws ecs describe-task-definition --task-definition ruby-on-rails-test
{
    "taskDefinition": {
        "status": "ACTIVE",
        "networkMode": "bridge",
        "family": "ruby-on-rails-test",
        "placementConstraints": [],
        "requiresAttributes": [
            {
                "name": "com.amazonaws.ecs.capability.ecr-auth"
            },
            {
                "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
            }
        ],
        "volumes": [
            {
                "host": {
                    "sourcePath": "/opt/cf/rails-app/public/"
                },
                "name": "ruby-on-rails-public-volume"
            }
        ],
        "taskDefinitionArn": "arn:aws:ecs:us-east-1:accountId:task-definition/ruby-on-rails-test:5",
        "containerDefinitions": [
            {
                "memoryReservation": 1024,
                "environment": [
                    {
                        "name": "DATABASE_HOSTNAME",
                        "value": "hostname"
                    },
                    {
                        "name": "PUMA_WORKERS",
                        "value": "2"
                    },
                    {
                        "name": "RAILS_ENV",
                        "value": "staging"
                    },
                    {
                        "name": "DATABASE_NAME",
                        "value": "ruby-on-rails"
                    },
                    {
                        "name": "DEBIAN_FRONTEND",
                        "value": "noninteractive"
                    },
                    {
                        "name": "PORT",
                        "value": "8080"
                    },
                    {
                        "name": "LANG",
                        "value": "en_US.UTF-8"
                    },
                    {
                        "name": "DATABASE_PASSWORD",
                        "value": "cf"
                    },
                    {
                        "name": "DATABASE_USER",
                        "value": "cf"
                    },
                    {
                        "name": "PUMA_MAX_THREADS",
                        "value": "6"
                    }
                ],
                "name": "ruby-on-rails-test",
                "mountPoints": [
                    {
                        "sourceVolume": "ruby-on-rails-public-volume",
                        "containerPath": "/opt/cf/rails-app/public/"
                    }
                ],
                "image": "accountId.dkr.ecr.us-east-1.amazonaws.com/cf/rails:latest",
                "cpu": 1024,
                "portMappings": [
                    {
                        "protocol": "tcp",
                        "containerPort": 8080,
                        "hostPort": 8080
                    }
                ],
                "command": [
                    "puma",
                    "-C",
                    "config/puma.rb"
                ],
                "essential": true,
                "volumesFrom": []
            }
        ],
        "revision": 5
    }
}

When the container boots, the environment variables are not set. I ran the following command from the ecs container machine, from a container that failed to start

[root@ip-ip docker]# docker run --name 46485594ffc4 accountId.dkr.ecr.us-east-1.amazonaws.com/    cf/rails:latest env
PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=0e0656e4e84c
RUBY_MAJOR=2.3
RUBY_VERSION=2.3.4
RUBY_DOWNLOAD_SHA256=341cd9032e9fd17c452ed8562a8d43f7e45bfe05e411d0d7d627751dd82c578c
RUBYGEMS_VERSION=2.6.12
BUNDLER_VERSION=1.15.0
GEM_HOME=/usr/local/bundle
BUNDLE_PATH=/usr/local/bundle
BUNDLE_BIN=/usr/local/bundle/bin
BUNDLE_SILENCE_ROOT_WARNING=1
BUNDLE_APP_CONFIG=/usr/local/bundle
APP_HOME=/opt/cf/rails-app
HOME=/root

Any idea why the variables are not being set?

Thanks in advance.


Solution

  • When you docker run an image by yourself, you are missing the whole thing that AWS does to run a container. It sets the env vars just in its docker run.

    You do:

    docker run --name 46485594ffc4 accountId.dkr.ecr.us-east-1.amazonaws.com/cf/rails:latest env

    There, you have nothing about the task that you configured. You have only the image that you've pushed.

    So, to check the environment variables from a stopped container, you should find it first:

    docker ps -a
    

    Then with the container ID, do:

    docker inspect ID
    

    And there you should see the env vars.

    PS: To see what happens with that container, check its logs: docker logs ID