Search code examples
dockervue.jsdockerfile

Docker not finding package.json when building Vue app container


I am trying to build a container image for a Vue app. The Dockerfile is very simple and contains a build, post checkout, and post build hook for the respective stages. post checkout runs successfully but for some reason I am having an error where the package.json file is not being seen.

I tried running ls -al to see the contents of the image right before the build command runs and here is the output:

0.496 drwxr-xr-x   1 root root 4096 Jul 22 13:58 .
0.496 drwxr-xr-x   1 root root 4096 Jul 22 13:58 ..
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 bin
0.496 drwxr-xr-x   2 root root 4096 Jun 19 21:20 boot
0.496 drwxr-xr-x   5 root root  340 Jul 22 13:58 dev
0.496 drwxr-xr-x   1 root root 4096 Jul 22 13:58 etc
0.496 drwxr-xr-x   1 root root 4096 Jul  9 16:49 home
0.496 drwxr-xr-x   8 root root 4096 Jul  1 00:00 lib
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 lib64
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 media
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 mnt
0.496 drwxr-xr-x   1 root root 4096 Jul  9 16:50 opt
0.496 dr-xr-xr-x 183 root root    0 Jul 22 13:58 proc
0.496 drwx------   2 root root 4096 Jul  1 00:00 root
0.496 drwxr-xr-x   3 root root 4096 Jul  1 00:00 run
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 sbin
0.496 drwxr-xr-x   2 root root 4096 Jul  1 00:00 srv
0.496 dr-xr-xr-x  13 root root    0 Jul 22 13:58 sys
0.496 drwxrwxrwt   1 root root 4096 Jul 22 13:58 tmp
0.496 drwxr-xr-x   1 root root 4096 Jul  1 00:00 usr
0.496 drwxr-xr-x   1 root root 4096 Jul  1 00:00 var

I tried running ls -al on /usr and saw it contained an src directory so I ran ls -al on it and got this:

0.465 drwxr-xr-x 2 root root 4096 Jun 19 21:20 .
0.465 drwxr-xr-x 1 root root 4096 Jul  1 00:00 ..

The Dockerfile in question is:

# base node image
FROM node:20-bullseye-slim as base

ARG NODE_ENV=staging
ARG DOTENV_KEY
ARG COMMIT_SHA
ARG RELEASE_SEMVER

# set for base and all layer that inherit from it
ENV LOG_LEVEL=debug
ENV DEBUG_API=true
ENV NODE_ENV ${NODE_ENV}
ENV DOTENV_KEY ${DOTENV_KEY}
ENV COMMIT_SHA ${COMMIT_SHA}
ENV RELEASE_SEMVER ${RELEASE_SEMVER}

RUN apt-get update && apt-get install -y curl tini

# Install all node_modules, including dev dependencies
FROM base as deps

WORKDIR /app

COPY package*.json ./

RUN npm install --include=dev

# Copy the rest of the application code
COPY . .

# Build the production image
FROM base as build

# WORKDIR /app
RUN ls -al /usr
ENV NODE_ENV staging

RUN npm run build

# Final image
FROM build as final

WORKDIR /app

EXPOSE 8080

CMD ["npm", "start"]

I'm admittedly extremely new to containerizing applications so I'll appreciate any assistance you can give. The dockerfile is creating an image on the huub that our CI/CD then builds and runs

I tried running ls -al a couple of times t see what structure the docker container sees and it soed not show the code in any of the steps before the failed build.


Solution

  • So it turns out I made a mistake and did not stack the layers correctly. I was using the base layer (where the package.json doesnt exist) instead of the deps layer that came before it and has all previous commands' output. fixed Dockerfile:

    # base node image
    FROM node:20-bullseye-slim as base
    
    ARG NODE_ENV=staging
    ARG DOTENV_KEY
    ARG COMMIT_SHA
    ARG RELEASE_SEMVER
    
    # set for base and all layer that inherit from it
    ENV LOG_LEVEL=debug
    ENV DEBUG_API=true
    ENV NODE_ENV ${NODE_ENV}
    ENV DOTENV_KEY ${DOTENV_KEY}
    ENV COMMIT_SHA ${COMMIT_SHA}
    ENV RELEASE_SEMVER ${RELEASE_SEMVER}
    
    RUN apt-get update && apt-get install -y curl tini
    
    # Install all node_modules, including dev dependencies
    FROM base as deps
    
    WORKDIR /app
    
    COPY package*.json ./
    
    RUN npm install --include=dev
    
    # Copy the rest of the application code
    COPY . .
    
    # Build the production image
    FROM deps as build
    
    # WORKDIR /app
    RUN ls -al /usr
    ENV NODE_ENV staging
    
    RUN npm run build
    
    # Final image
    FROM build as final
    
    WORKDIR /app
    
    EXPOSE 8080
    
    CMD ["npm", "start"]
    

    Edit: So basically the fix was a one line edit, FROM deps as build instead of FROM base as build.