我已经设置了一个 CI 系统,它可以构建 Docker 镜像并将其推送到 ECR。在Dockerrun.aws.json
文件中,我使用这些镜像(链接)来运行多 Docker 容器 beanstalk 环境。
样本Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"volumes": [
{
"name": "web-app",
"host": {
"sourcePath": "/var/app/current/web-app"
}
},
{
"name": "api-service",
"host": {
"sourcePath": "/var/app/current/api-service"
}
}
],
"containerDefinitions": [
{
"name": "api-service",
"image": "somekey.dkr.ecr.us-west-2.amazonaws.com/api-service",
"essential": true,
"memory": 800,
"privileged": true,
"portMappings": [
{
"hostPort": 8080,
"containerPort": 80
}
],
"command": [
"/bin/bash",
"/root/api-service/before_run.sh"
],
"mountPoints": [
{
"sourceVolume": "api-service",
"containerPath": "/root/api-service"
}
]
},
{
"name": "web-app",
"image": "somekey.dkr.ecr.us-west-2.amazonaws.com/web-app",
"essential": true,
"memory": 800,
"environment": [
{
"name": "ENVIRONMENT",
"value": "staging"
}
],
"command": [
"/bin/bash",
"/root/web-app/before_run.sh"
],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80
}
],
"mountPoints": [
{
"sourceVolume": "web-app",
"containerPath": "/root/web-app"
}
]
}
]
}
问题是 beanstalk 没有使用这些图像。相反,它使用/var/current/app/api-service
文件夹中的代码。
因此,如果我仅上传Dockerrun.aws.json
文件,则会失败,No file or directory
因为 中没有文件/var/current/app/api-service
。同样,如果我添加文件夹api-service
然后Dockerrun.aws.json
部署。它可以工作。
其想法是简单地从 ECR 中提取这些 docker 镜像并运行它们,而无需向容器部署额外的源代码。
PS:我已将 ECR 访问权限添加到 eb 实例配置文件。我可以看到图像被正确提取。我甚至可以在远程实例上手动运行它们而不会出现任何问题。
答案1
我设法通过使用以下方法解决了这个问题。
删除mountpoints
以Dockerrun.aws.json
让 Docker 使用其自己的工作区。
以下是新的Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"name": "api-service",
"image": "somekey.dkr.ecr.us-west-2.amazonaws.com/api-service",
"essential": true,
"memory": 800,
"privileged": true,
"portMappings": [
{
"hostPort": 8080,
"containerPort": 80
}
],
"command": [
"/bin/bash",
"/root/api-service/before_run.sh"
]
},
{
"name": "web-app",
"image": "somekey.dkr.ecr.us-west-2.amazonaws.com/web-app",
"essential": true,
"memory": 800,
"environment": [
{
"name": "ENVIRONMENT",
"value": "staging"
}
],
"command": [
"/bin/bash",
"/root/web-app/before_run.sh"
],
"portMappings": [
{
"hostPort": 80,
"containerPort": 80
}
]
}
]
}