fix: SQLite readonly error in Docker container

- Add entrypoint script that ensures /data is owned by node user
  before dropping privileges with su-exec
- Remove USER node from Dockerfile (entrypoint handles it)
- Change client depends_on to service_healthy so nginx waits for
  the server to pass its healthcheck before starting
This commit is contained in:
MayaTheShy
2026-03-22 19:15:04 -04:00
parent d4a9441b54
commit b49574f39b
3 changed files with 18 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ services:
- inventory-network - inventory-network
depends_on: depends_on:
server: server:
condition: service_started condition: service_healthy
restart: unless-stopped restart: unless-stopped
networks: networks:

View File

@@ -2,7 +2,8 @@
FROM node:18-alpine FROM node:18-alpine
# Build tools needed for better-sqlite3 native compilation # Build tools needed for better-sqlite3 native compilation
RUN apk add --no-cache python3 make g++ # su-exec for dropping privileges in entrypoint
RUN apk add --no-cache python3 make g++ su-exec
WORKDIR /app WORKDIR /app
@@ -15,16 +16,18 @@ RUN apk del python3 make g++
COPY . . COPY . .
# Create data directory for SQLite with proper ownership # Create data directory for SQLite
RUN mkdir -p /data && chown node:node /data RUN mkdir -p /data
VOLUME /data VOLUME /data
# Run as non-root user for security # Entrypoint fixes /data permissions then drops to node user
USER node COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
EXPOSE 3001 EXPOSE 3001
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3001/api/health',r=>{process.exit(r.statusCode===200?0:1)}).on('error',()=>process.exit(1))" CMD node -e "require('http').get('http://localhost:3001/api/health',r=>{process.exit(r.statusCode===200?0:1)}).on('error',()=>process.exit(1))"
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node", "server.js"] CMD ["node", "server.js"]

View File

@@ -0,0 +1,9 @@
#!/bin/sh
set -e
# Ensure data directory exists and is writable by the node user
mkdir -p /data
chown node:node /data
# Drop privileges and exec the CMD
exec su-exec node "$@"