我正在使用AWS Beanstalk将我的项目部署在"在64位Amazon Linux上运行的多容器Docker"中
根据文档,这是我的Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "child",
"image": "nithinsgowda/child",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 9000,
"containerPort": 9000
}
],
"links": [
"master"
]
},
{
"name": "master",
"image": "nithinsgowda/master",
"essential": true,
"memory": 512,
"portMappings": [
{
"hostPort": 80,
"containerPort": 8080
}
],
"links": [
"child"
]
}
]
}
我可以从公共互联网访问我在80端口的主集装箱
在我的主容器中,我要对子容器进行API调用我尝试了以下选项:它们都不起作用
fetch('http://child/api')
fetch('http://child:9000/api')
fetch('http://15.14.13.12:9000/api') //My public DNS for the beanstalk application (Example)
如果是在本地docker compose环境中http://child/api"效果非常好。但这对Beanstalk不起作用。
如何从主容器与子容器通信
我甚至尝试了bindIP属性,并分配了一个本地IP,并尝试使用本地IP访问它,但它仍然不起作用
当查看服务器日志时,docker ps由环境执行,并且两个容器都已启动并运行,端口映射也显示正确。
以下是您需要在Dockerrun.aws.json 中指定的内容
"containerDefinitions": [
{
"name": "child",
"image": "nithinsgowda/child",
"essential": true,
"memory": 256,
"portMappings": [
{
"hostPort": 9000,
"containerPort": 9000
}
],
"links": [
"master"
],
"environment": [
{
"name": "Container",
"value": "child"
}
]
},
名为Container的环境变量将是网络中容器的名称。
"environment": [
{
"name": "Container",
"value": "child" //Any custom name accepted
}
]
因此,在指定环境变量后,我现在可以访问子容器作为fetch('http://child:9000/api'(
以下是指定上述内容的AWS官方文档链接https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_docker_v2config.html#create_deploy_docker_v2config_dockerrun