简体   繁体   中英

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?)

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"
  }
],

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM