mohammed adbelhadi
mohammed adbelhadi

Reputation: 11

how to dockerize mozilla pdf.js application

am trying to dockerize mozilla pdf.js repo but am facing permission issues with glup. here is my Dockerfile & docker-compose :

# # Use an official Node.js runtime as the base image
FROM node:18

# Create a new user and group
RUN groupadd -r app && useradd -r -g app -G node -m -s /bin/bash app

# Set the working directory in the container
WORKDIR /app

# Assign ownership of the working directory to the app user
RUN chown -R app:app /app

# Copy the local project into the container
COPY . /app

# Switch to the app user
USER app

# Install Gulp globally
RUN npm install gulp-cli
RUN npm install gulp
# Install project dependencies
RUN npm install


# Make port 8888 available to the world outside this container
EXPOSE 8888

# Run the application when the container launches
CMD ["gulp", "server"]
version: '3'
services:
  pdfjs:
    build: .
    ports:
      - "8888:8888"
    volumes:
      - .:/app

when i run docker-compose up --build , i get the below error: 6.703 npm ERR! code EACCES 6.703 npm ERR! syscall mkdir 6.703 npm ERR! path /usr/local/lib/node_modules/gulp-cli 6.703 npm ERR! errno -13 6.705 npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/gulp-cli' 6.705 npm ERR! [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/gulp-cli'] { 6.705 npm ERR! errno: -13, 6.705 npm ERR! code: 'EACCES', 6.705 npm ERR! syscall: 'mkdir', 6.705 npm ERR! path: '/usr/local/lib/node_modules/gulp-cli' 6.705 npm ERR! }

tried a lot of approach but nothing work.

i did some changes to my Dockerfile and now the application is been build without any issue. However, the result not what i was expecting. When i try to access http://localhost:8888/web/viewer.html i don't see anything in the page. Am suppose to view the example pdf file in web. Here is my updated Dockerfile: FROM node:18

WORKDIR /app COPY package*.json ./

RUN npm install --global gulp-cli

RUN npm install

COPY . .

CMD ["gulp", "server"]

EXPOSE 8888

Upvotes: 0

Views: 393

Answers (1)

David Maze
David Maze

Reputation: 159658

The Dockerfile COPY directive normally makes the files it adds owned by the root user, even if the current USER is something else.

One common setup is to do all of the package setup and installation as root, and change to a different USER only at the end of the Dockerfile where you also specify the CMD. Do not chown anything in the image to the non-root user: the code will be owned by root and world-readable but not world-writable, which avoids the possibility of accidentally overwriting it.

FROM node:18
RUN useradd -r app
WORKDIR /app

# here: do not chown the directory; do not switch USER yet

COPY ./ ./
RUN npm install gulp-cli
RUN npm install gulp
RUN npm install

# here: switch USER now
USER app

EXPOSE 8888
CMD ["gulp", "server"]

Make sure to also delete the volumes: block in the Compose file that overwrites the image's /app directory. To the extent that you're trying to work out file permissions in the image, overwriting that with a bind mount will hide all of this; there are potential compatibility problems around the node_modules: directory; and fundamentally you just won't be running anything in the image you've created.

Upvotes: 0

Related Questions