Search code examples
dockerdocker-composenext.jswindows-subsystem-for-linuxblitz.js

Permission error at WSL2 files mounted into Docker container


I want to make a development setup of a Blitz.js app with Docker (because it will be deployed and tested with it, too). I am developing on Windows, the code resides within WSL2.

After starting up, the container exits with:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
Environment variables loaded from .env
Prisma schema loaded from db/schema.prisma
Prisma Studio is up on http://localhost:5555
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
[Error: EACCES: permission denied, unlink '/home/node/app/.next/server/blitz-db.js'] {
  errno: -13,
  code: 'EACCES',
  syscall: 'unlink',
  path: '/home/node/app/.next/server/blitz-db.js'
}
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.

This is what my Dockerfile looks like:

# Create a standard base image that has all the defaults
FROM node:16-slim as base
ENV NODE_ENV=production
ENV PATH /home/node/app/node_modules/.bin:$PATH
ENV TINI_VERSION v0.19.0
WORKDIR /home/node/app
RUN apt-get update && apt-get install -y openssl --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && chown -R node:node /home/node/app
# Blitz.js recommends using tini, see why: https://github.com/krallin/tini/issues/8
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
USER node
COPY --chown=node:node package*.json yarn.lock* ./
RUN yarn config list && yarn install --frozen-lockfile && yarn cache clean --force

# Create a development image
FROM base as dev
ENV NODE_ENV=development
USER node
COPY --chown=node:node . .
RUN yarn config list && yarn install && yarn cache clean --force
CMD ["bash", "-c", "yarn dev"]

Within WSL2, I run docker-compose up -d to make use of the following docker-compose.yml:

version: "3.8"

services:
  app:
    container_name: itb_app
    build: .
    image: itb_app:dev
    ports:
      - 3000:3000
    volumes:
      # Only needed during development: Container gets access to app files on local development machine.
      # Without access, changes made during development would only be reflected
      # every time the container's image is built (hence on every `docker-compose up`).
      - ./:/home/node/app/

The file in question (blitz-db.js) is generated by yarn dev (see Dockerfile). I checked the owner of it within WSL2: It seems to be root. But I wouldn't know how to change it under these circumstances, let alone know to which user.

I wonder how I can mount the WSL2 directory into my container for Blitz.js to use it.


Solution

  • The issue is that the .next directory and its content (the code of the compiled Blitz.js app) was created by the host system before the docker container was introduced. So, the host system user was owner of the directory and its files. Thus, the container's user did not have write permissions and couldn't compile its own app version into the .next directory, raising the error above.

    The solution is to delete the .next folder from the host system and restarting the container, giving it the ability to compile the app.