Skip to content
Snippets Groups Projects
Commit 55b56589 authored by Fionn McGoldrick's avatar Fionn McGoldrick
Browse files

Docker file added and changes made within the backend in order to make the...

Docker file added and changes made within the backend in order to make the application work in a docker container. Project finished.
parent 460156b5
No related branches found
No related tags found
No related merge requests found
# Start from the official Node.js image # Use Node image
FROM node:18 FROM node:18
# Set the working directory in the container # Create app directory
WORKDIR /app WORKDIR /app
# Copy backend package.json
COPY backend/package*.json ./ COPY backend/package*.json ./
WORKDIR /app/backend
# Install backend dependencies
RUN npm install RUN npm install
# Copy backend code
COPY backend/ . COPY backend/ .
# Copy the rest of your app (code + frontend files) # Copy frontend files into the backend's public directory
COPY . . COPY frontend/ ./public/
# Expose the port your app runs on # Expose backend port
EXPOSE 3000 EXPOSE 5000
# Start the server # Start backend
CMD ["npm", "start"] CMD ["node", "server.js"]
...@@ -4,7 +4,7 @@ const mongoose = require('mongoose'); ...@@ -4,7 +4,7 @@ const mongoose = require('mongoose');
const app = express(); const app = express();
const path = require('path'); const path = require('path');
app.use(express.static(path.join(__dirname, '../frontend'))); app.use(express.static(path.join(__dirname, 'public')));
const cors = require('cors'); const cors = require('cors');
app.use(cors()); app.use(cors());
...@@ -16,10 +16,9 @@ const PORT = 3000; ...@@ -16,10 +16,9 @@ const PORT = 3000;
app.use(express.json()); app.use(express.json());
// Connect to MongoDB // Connect to MongoDB
mongoose.connect('mongodb://admin:secret@localhost:27017/BookDatabase', { mongoose.connect('mongodb://mongo-dev:27017/BookDatabase', {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
authSource: "admin",
}) })
.then(() => console.log('Connected to MongoDB')) .then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to MongoDB', err)); .catch(err => console.error('Could not connect to MongoDB', err));
......
version: '3.9'
services:
mongo:
image: mongo
container_name: mongo
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: secret
app:
build: .
container_name: bookapp
ports:
- "3000:3000"
depends_on:
- mongo
environment:
- MONGO_URI=mongodb://admin:secret@mongo:27017/BookDatabase?authSource=admin
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment