问题将字典传递到Fargate Docker容器中
我有一个容器,该容器在本地运行良好,该容器由一个简单的Python脚本组成,该脚本接受一个字典,该字典转换为系统参数,并打印出键/值对:
Calc.py:
import sys
import json
if __name__ == "__main__":
print("Entered in main")
print(f"Function name: {sys.argv[0]}")
#load
print(sys.argv[1])
payload = json.loads(sys.argv[1])
for k,v in payload.items():
print(f"Key: {k},Value: {v}")
我使用带有docker容器的入门点,使用带有docker的键盘,环境变化:
ENTRYPOINT python3 calc.py "${LAMBDA_PAYLOAD}"
这使我可以运行一个测试Docker脚本
docker run -e LAMBDA_PAYLOAD="{\"names\":\"J.J. Robichard\",\"Age\":25}" calc
,该脚本成功地打印出键/值对。
当我使用boto3将其放入AWS ECS FARGATE任务中时,并通过使用以下方式覆盖环境变量:
'environment': [
{
'name': 'LAMBDA_PAYLOAD',
'value': "{\"names\":[\"J.J. Robichard\",\"April\"],\"years\":[25,29]}"
},
],
我将print(sys.argv [1])返回:
{"names":["J.J.
一切都会崩溃。我如何获得接受带有空间的字符串的任务?我已经尝试了“ \”和f“”“ {}”“”,但是什么都没有起作用。
I have a container that runs fine locally consisting of a simple python script that accepts a dictionary converted to a string as a system argument and prints out the key/value pairs:
calc.py:
import sys
import json
if __name__ == "__main__":
print("Entered in main")
print(f"Function name: {sys.argv[0]}")
#load
print(sys.argv[1])
payload = json.loads(sys.argv[1])
for k,v in payload.items():
print(f"Key: {k},Value: {v}")
I build this as a docker container using an entrypoint with an environmental varible:
ENTRYPOINT python3 calc.py "${LAMBDA_PAYLOAD}"
This allows me to run a test docker script of
docker run -e LAMBDA_PAYLOAD="{\"names\":\"J.J. Robichard\",\"Age\":25}" calc
which successfully prints out the key/value pairs.
When I put this into an AWS ECS Fargate task using boto3 and overriding the environmental variable by using:
'environment': [
{
'name': 'LAMBDA_PAYLOAD',
'value': "{\"names\":[\"J.J. Robichard\",\"April\"],\"years\":[25,29]}"
},
],
I get the print(sys.argv[1]) returns:
{"names":["J.J.
and everything breaks down. How do I get the task to accept a string with space? I've tried "\ " and f"""{}""", but nothing is working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为解决方法,我能够通过以下编码来传递字典:
在另一侧,我必须这样做:
As a workaround, I was able to pass the dictionary through by encoding it as follows:
on the other side, I had to do this: