简体   繁体   English

将参数传递给在AWS Fargate上的Docker容器中运行的Python

[英]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 将参数传递给运行Python脚本的Docker容器可以这样做

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?) 我似乎无法弄清楚如何在AWS Fargate上运行容器时传递这些参数(也许它不起作用?)

You can use container definitions parameters in ECS task definition to pass runtime arguments. 您可以在ECS任务定义中使用容器定义参数来传递运行时参数。

Command parameter maps to COMMAND parameter in docker run. 命令参数映射到docker run中的COMMAND参数。

"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. 在ecs中,您将把容器作为任务运行。 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) myscript-task.json :(一个示例任务定义)

{
    "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: 您将在控制台中或使用register-task-definition命令注册任务

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

You can now run the task with ecs run-task command. 您现在可以使用ecs run-task命令运行该任务。 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: 示例Overrides.json:

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

Now you can access the variable in your python script. 现在,您可以在python脚本中访问该变量。

Python script(sample) printing the passed environment variable. Python脚本(示例)打印传递的环境变量。

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

With log driver configured, you will be able to see the output in cloudwatch logs. 配置日志驱动程序后,您将能够在cloudwatch日志中看到输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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