Everything is now containerised and in the docker compose

This commit is contained in:
2025-08-09 17:40:01 +01:00
parent 371f48b42f
commit 22328ae6b1
11 changed files with 375 additions and 51 deletions

32
backend/Dockerfile Normal file
View File

@@ -0,0 +1,32 @@
# Use Node.js 18 LTS as base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy application code
COPY . .
# Create non-root user for security
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
# Change ownership of the app directory
RUN chown -R nodejs:nodejs /app
USER nodejs
# Expose port
EXPOSE 5000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:5000/', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })"
# Start the application
CMD ["npm", "start"]

12
backend/dockerignore Normal file
View File

@@ -0,0 +1,12 @@
node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.git
.gitignore
README.md
.env
.nyc_output
coverage
.DS_Store
*.log

View File

@@ -27,7 +27,7 @@ const sampleRecipes = [
cookTime: 15,
category: "dinner",
difficulty: "medium",
imageUrl: "https://images.unsplash.com/photo-1621996346565-e3dbc353d2e5?w=500"
imageUrl: "https://images.unsplash.com/photo-1692071097529-320eb2b32292?w=500"
},
{
title: "Chocolate Chip Cookies",

View File

@@ -11,17 +11,20 @@ app.use(cors());
app.use(express.json());
// MongoDB connection
const MONGODB_USERNAME = process.env.MONGODB_USERNAME;
const MONGODB_PASSWORD = process.env.MONGODB_PASSWORD;
const MONGODB_HOST = process.env.MONGODB_HOST;
const MONGODB_PORT = process.env.MONGODB_PORT;
const MONGODB_DATABASE = process.env.MONGODB_DATABASE;
const MONGODB_HOST = process.env.MONGODB_HOST || 'mongodb';
const MONGODB_PORT = process.env.MONGODB_PORT || '27017';
const MONGODB_DATABASE = process.env.MONGODB_DATABASE || 'recipe-management';
const MONGODB_URI = `mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}`;
console.log('Connecting to MongoDB with URI:', MONGODB_URI);
const MONGODB_URI = process.env.MONGODB_URI ||
`mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_DATABASE}?authSource=admin`;
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000, // 30 seconds
connectTimeoutMS: 30000, // 30 seconds
socketTimeoutMS: 30000, // 30 seconds
});
const db = mongoose.connection;