Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments to Python running in Docker container on AWS Fargate

Passing arguments to a Docker container running a Python script can be done like so

docker run my_script:0.1 --arg1 val --arg2 val ...

I can't seem to figure out how to pass those arguments when running the container on AWS Fargate (perhaps it doesn't work?)

like image 962
mjkrause Avatar asked Aug 22 '19 16:08

mjkrause


2 Answers

In ecs, you will run your containers as tasks. So you will first register the task that includes your container definition and then you can run the task passing your arguments as environment variables.

Here is an example task definition:

myscript-task.json: (a sample task definition)

{
    "containerDefinitions": [
        {
            "name": "myscript",
            "image": "12345123456.dkr.ecr.us-west-2.amazonaws.com/myscript:0.1",
            "logConfiguration": { 
                "logDriver": "awslogs",
                "options": { 
                   "awslogs-group" : "/ecs/fargate",
                   "awslogs-region": "us-west-2",
                   "awslogs-stream-prefix": "myscript"
                }
             }
        }

    ],
    "family": "myscript",
    "networkMode": "awsvpc",
    "executionRoleArn": "arn:aws:iam::12345123456:role/ecsTaskExecutionRole",
    "cpu": "256",
    "memory": "512",
    "requiresCompatibilities": [ 
       "FARGATE" 
    ]
}

You will register the task in the console or with the register-task-definition command:

aws ecs register-task-definition --cli-input-json file://myscript-task.json

You can now run the task with ecs run-task command. Using overrides parameter you will be able to run the same task with different values.

aws ecs run-task --cluster testCluster --launch-type FARGATE --task-definition myscript:1 --network-configuration 'awsvpcConfiguration={subnets=[subnet-0abcdec237054abc],assignPublicIp=ENABLED}' --overrides file://overrides.json

A sample Overrides.json:

{
    "containerOverrides": [{
        "name": "myscript",
        "environment": [{
            "name": "VAR1",
            "value": "valueOfVar1"
        }]
    }]
}

Now you can access the variable in your python script.

Python script(sample) printing the passed environment variable.

import os
print(os.environ['VAR1'])

With log driver configured, you will be able to see the output in cloudwatch logs.

like image 182
Shree Avatar answered Oct 18 '22 20:10

Shree


You can use container definitions parameters in ECS task definition to pass runtime arguments.

Command parameter maps to COMMAND parameter in docker run.

"command": [
  "--arg1",
  "val",
  "--arg2",
  "val"
],

It is also possible to pass parameters as environment variables.

"environment": [
  {
    "name": "LOG_LEVEL",
    "value": "debug"
  }
],

like image 5
Vikyol Avatar answered Oct 18 '22 20:10

Vikyol