diff --git a/Dockerfile b/Dockerfile index 348981a..96bb2ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,35 @@ # Use an official Node runtime as the parent image -FROM node:18 +FROM node:18-slim -# Set the working directory in the container +# Create app directory WORKDIR /usr/src/app -# Copy package.json and package-lock.json +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied COPY package*.json ./ # Install dependencies -RUN npm install +RUN npm ci --only=production -# Copy the rest of your app's source code +# Bundle app source COPY . . -# Your app binds to port 3000 so you'll use the EXPOSE instruction to have it mapped by the docker daemon +# Create a non-root user +RUN addgroup --system --gid 1001 nodejs && \ + adduser --system --uid 1001 --ingroup nodejs nodejs && \ + chown -R nodejs:nodejs /usr/src/app + +USER nodejs + +# Your app binds to port 3000 EXPOSE 3000 -# Define the command to run your app +# Add healthcheck +HEALTHCHECK --interval=30s --timeout=3s \ + CMD curl -f http://localhost:3000/health || exit 1 + +# Define environment variable +ENV NODE_ENV=production + +# Start the app CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/index.js b/index.js index 2e55ad5..0e7506e 100644 --- a/index.js +++ b/index.js @@ -54,4 +54,9 @@ process.on('SIGTERM', async () => { console.error('Error during shutdown:', error); process.exit(1); } +}); + +// Add health check endpoint +app.get('/health', (req, res) => { + res.status(200).json({ status: 'healthy' }); }); \ No newline at end of file