typescriptamazon-web-servicesdockeramazon-ecspulumi

When pushing to ECS, Docker image errors out with module not found error


I am trying to get a a Docker image hosted on on AWS ECS. I am able to have a successful local build where the image is able to run without any issues.

When I upload the image to ECS I keep getting this error where the error is Exited with Code 1

Doing more of a deep dive by setting up logs, I found that this is the issue:

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: []
}

Here is my infrastructure code (I am using pulumi)

This is how I am defining my task definition, which I think the issue lies as I am defining the image there.


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" ]

Solution

  • I figured out the issue there is this line of code:

      build: {
        context: ".",
        dockerfile: "./app/Dockerfile",
      },
    

    This issues is with the context, the build context was pointing to . which is all the files including the pulumi project files into the docker image.

    Changing the context to:

      build: {
        context: "./app",
        dockerfile: "./app/Dockerfile",
      },
    

    This points the build folder to just the app folder in my project which caused the image to run successfully