AWS Fargate 无法运行特定环境的构建?

huangapple go评论67阅读模式
英文:

AWS Fargate can't run ENV specific build?

问题

我有一个Next.js应用程序,我正在将其容器化并部署到AWS Fargate中。

以下是我的Docker文件:

# 基础镜像
FROM node:18-alpine
ARG ENV=$ENV
RUN mkdir -p /usr/app/
WORKDIR /usr/app/
COPY . .
RUN ls -l
RUN npm install
RUN npm run build-qa
EXPOSE 3000
CMD ["npm", "start-qa"]

这是我的ECS任务定义:

{
    "taskDefinitionArn": "arn:aws:ecs:region:**********:task-definition/NextJs-task:6",
    "containerDefinitions": [
        {
            "name": "NextJs-container",
            "image": "**********.dkr.ecr.region.amazonaws.com/next-js-app:latest",
            "cpu": 1024,
            "portMappings": [
                {
                    "containerPort": 3000,
                    "hostPort": 3000,
                    "protocol": "tcp"
                }
            ],
            "essential": true,
            "environment": [
                {
                    "name": "ENV",
                    "value": "qa"
                }
            ],
            "mountPoints": [],
            "volumesFrom": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-create-group": "true",
                    "awslogs-group": "/ecs/NextJs",
                    "awslogs-region": "region",
                    "awslogs-stream-prefix": "ecs"
                }
            }
        }
    ],
    "family": "NextJs-task",
    "taskRoleArn": "arn:aws:iam::**********:role/ecsTaskExecutionRole",
    "executionRoleArn": "arn:aws:iam::**********:role/ecsTaskExecutionRole",
    "networkMode": "awsvpc",
    "revision": 6,
    "volumes": [],
    "status": "ACTIVE",
    "requiresAttributes": [
        {
            "name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
        },
        {
            "name": "ecs.capability.execution-role-awslogs"
        },
        {
            "name": "com.amazonaws.ecs.capability.ecr-auth"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
        },
        {
            "name": "com.amazonaws.ecs.capability.task-iam-role"
        },
        {
            "name": "ecs.capability.execution-role-ecr-pull"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
        },
        {
            "name": "ecs.capability.task-eni"
        },
        {
            "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29"
        }
    ],
    "placementConstraints": [],
    "compatibilities": [
        "EC2",
        "FARGATE"
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "cpu": "2048",
    "memory": "4096",
    "registeredAt": "**************",
    "registeredBy": "arn:aws:sts::**********:assumed-role/aws-sre/-------------"
}

在运行任务时,Fargate报错:

要查看支持的npm命令列表,请运行:
npm help
未知命令:"start-qa"
是否是其中一个?
npm start # 启动一个包
npm run start-qa # 运行 "start-qa" 包脚本
npm run start-uat # 运行 "start-uat" 包脚本

我是使用以下命令构建我的Docker镜像的。我在AWS Apprunner中也遇到了类似的问题。

docker build --build-arg ENV=$ENV -t next-js-app .

我从Jenkins作业参数中传递了ENV。

英文:

I had Next-Js application which I was containerizing and deploying it in AWS Fargate.

Below is my dockerfile

#Base Image
FROM node:18-alpine
ARG ENV=$ENV
RUN mkdir -p /usr/app/
WORKDIR /usr/app/
COPY . .
RUN ls -l
RUN npm install
RUN npm run build-qa
EXPOSE 3000
CMD ["npm", "start-qa"]

And here is my ECS Task Defination

{
"taskDefinitionArn": "arn:aws:ecs:region:**********:task-definition/NextJs-task:6",
"containerDefinitions": [
{
"name": "NextJs-container",
"image": "**********.dkr.ecr.region.amazonaws.com/next-js-app:latest",
"cpu": 1024,
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000,
"protocol": "tcp"
}
],
"essential": true,
"environment": [
{
"name": "ENV",
"value": "qa"
}
],
"mountPoints": [],
"volumesFrom": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/NextJs",
"awslogs-region": "region",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"family": "NextJs-task",
"taskRoleArn": "arn:aws:iam::**********:role/ecsTaskExecutionRole",
"executionRoleArn": "arn:aws:iam::**********:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"revision": 6,
"volumes": [],
"status": "ACTIVE",
"requiresAttributes": [
{
"name": "com.amazonaws.ecs.capability.logging-driver.awslogs"
},
{
"name": "ecs.capability.execution-role-awslogs"
},
{
"name": "com.amazonaws.ecs.capability.ecr-auth"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.19"
},
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"name": "ecs.capability.execution-role-ecr-pull"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.18"
},
{
"name": "ecs.capability.task-eni"
},
{
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.29"
}
],
"placementConstraints": [],
"compatibilities": [
"EC2",
"FARGATE"
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "2048",
"memory": "4096",
"registeredAt": "**************",
"registeredBy": "arn:aws:sts::**********:assumed-role/aws-sre/-------------",
}

When running a task the fargate was giving error as

To see a list of supported npm commands, run:
npm help
Unknown command: "start-qa"
Did you mean one of these?
npm start # Start a package
npm run start-qa # run the "start-qa" package script
npm run start-uat # run the "start-uat" package script

I was building my docker image with following command. I have faced similar issue is AWS Apprunner too.

docker build --build-arg ENV=$ENV -t next-js-app .

Was passing the ENV from Jenkins job parameter

答案1

得分: 1

这个错误与Fargate、ECS或任何与“ENV特定构建”有关的限制无关。如果你使用相同的Docker镜像在本地运行它,你会看到相同的错误消息。

错误提示你不理解 npm start-qa 是什么,甚至建议你应该改为 npm run start-qa

你需要将Dockerfile 中的最后一行更改为这样:

CMD ["npm", "run", "start-qa"]
英文:

This error has nothing to do with Fargate, ECS, or any sort of limitation around "ENV Specific Builds". If you took the same docker image and ran it locally, you would see the same error message.

The error is telling you it doesn't understand what npm start-qa is, and event suggesting to you that you should be doing npm run start-qa instead.

You need to change the last line in your Dockerfile to this:

CMD ["npm", "run", "start-qa"]

huangapple
  • 本文由 发表于 2023年5月24日 18:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定