推送Docker镜像至AWS ECS时遭遇module not found错误求助
问题:ECS容器启动失败(Exited with Code 1)- 找不到server.js模块
本地构建并运行Docker镜像完全正常,但上传至AWS ECS后,容器持续出现Exited with Code 1错误。查看日志后发现具体错误:
Error: Cannot find module '/usr/src/app/server.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15) at Function.Module._load (internal/modules/cjs/loader.js:667:27) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }
相关代码
Pulumi基础设施代码
const repo = new aws.ecr.Repository("pulumi-example"); const imageName = repo.repositoryUrl.apply((url) => url.slice(0, 255)); const customImage = "pulumi-example-img"; const imageVersion = "v1.0.0"; const image = new docker.Image(customImage, { build: { context: ".", dockerfile: "./app/Dockerfile", }, imageName: pulumi.interpolate`${imageName}:${imageVersion}`, }); const nodeAppTaskDefinition = new aws.ecs.TaskDefinition( "nodeAppTaskDefinition", { family: "frontend-task-definition-family", cpu: "256", memory: "512", networkMode: "awsvpc", requiresCompatibilities: ["FARGATE"], executionRoleArn: mainExecRole.arn, taskRoleArn: mainTaskRole.arn, runtimePlatform: { operatingSystemFamily: "LINUX", cpuArchitecture: "ARM64", }, containerDefinitions: imageName.apply((url) => JSON.stringify([ { name: "nodeContainer", image: `${url}:v1.0.0`, memory: 512, essential: true, logConfiguration: { logDriver: "awslogs", options: { "awslogs-create-group": "true", "awslogs-group": "ECSLogs", "awslogs-region": "us-east-2", "awslogs-stream-prefix": "ecs", }, }, portMappings: [ { containerPort: 80, hostPort: 80, protocol: "TCP", }, ], }, ]) ), } );
Dockerfile内容
FROM node:12 # Create app directory WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8080 CMD [ "node", "server.js" ]
问题原因分析
- Docker构建上下文错误:Pulumi中
docker.Image的build.context设置为项目根目录(.),而Dockerfile位于./app/Dockerfile。执行COPY . .时,会将根目录的所有文件复制到容器的/usr/src/app,但你的server.js实际在./app目录下,导致容器内找不到该文件。 - 镜像引用不一致:任务定义中手动拼接镜像地址和版本,没有依赖Pulumi构建的
image资源的镜像名称,可能存在镜像未正确推送或版本不匹配的情况。 - 端口配置不匹配:Dockerfile暴露8080端口,但任务定义中容器端口设为80,虽不直接导致模块找不到,但属于配置不一致问题。
解决方法
- 修正Docker构建上下文:将
docker.Image的build.context改为./app,确保COPY命令复制的是app目录下的文件:const image = new docker.Image(customImage, { build: { context: "./app", // 修改此处 dockerfile: "./app/Dockerfile", }, imageName: pulumi.interpolate`${imageName}:${imageVersion}`, }); - 正确引用构建后的镜像:任务定义中使用
image.imageName替代手动拼接,确保使用的是已推送到ECR的正确镜像:containerDefinitions: image.imageName.apply((imgName) => JSON.stringify([ { // ...其他配置 image: imgName, // 修改此处 // ...其他配置 }, ]) ), - 统一端口配置:将任务定义中的
containerPort改为8080,与Dockerfile的EXPOSE保持一致:portMappings: [ { containerPort: 8080, // 修改此处 hostPort: 8080, protocol: "TCP", }, ], - 验证本地构建一致性:本地测试时,使用
docker build -t test-img ./app命令构建镜像(与Pulumi的上下文一致),确认容器能正常启动,避免本地构建上下文与Pulumi配置不一致导致的问题。
内容的提问来源于stack exchange,提问作者Zubair Amjad
相关产品推荐
相关产品推荐

