Pass arguments to Python running in Docker container on AWS Fargate

為{幸葍}努か 提交于 2020-05-29 05:12:08

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/57613932/pass-arguments-to-python-running-in-docker-container-on-aws-fargate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!