-
Notifications
You must be signed in to change notification settings - Fork 62
feat(docker): add Docker support for frontend and backend with development and production configurations #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
c143fca
b482d19
eb2535c
b0be39e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| backend | ||
| node_modules | ||
| dist | ||
| build | ||
| .gitignore | ||
| .git | ||
| .env.* | ||
| .github | ||
| yarn.lock |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| # Node modules | ||
| node_modules/ | ||
| package-lock.json | ||
| */yarn.lock | ||
| yarn.lock | ||
| # Logs | ||
| logs | ||
| *.log | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Stage 1: Build the frontend | ||
| FROM node:20-alpine AS build | ||
|
|
||
| # Set the working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy package.json | ||
| COPY package.json . | ||
|
|
||
| # Install production dependencies using Yarn | ||
| RUN yarn install --production | ||
| # Copy the rest of the application files | ||
| COPY . . | ||
|
|
||
| # Build the frontend using Yarn | ||
| RUN yarn run build | ||
|
|
||
| # Stage 2: Serve the application with Nginx | ||
| FROM nginx:alpine | ||
|
|
||
| # Copy the build output from the previous stage to Nginx's serve directory | ||
| COPY --from=build /app/dist /usr/share/nginx/html | ||
|
|
||
| # Remove the default Nginx configuration file | ||
| RUN rm /etc/nginx/conf.d/default.conf | ||
|
|
||
| # Create a new Nginx configuration file | ||
| RUN echo $'\ | ||
| server { \n\ | ||
| listen 3000; \n\ | ||
| server_name localhost; \n\ | ||
| root /usr/share/nginx/html; \n\ | ||
| index index.html; \n\ | ||
| \n\ | ||
| location / { \n\ | ||
| try_files $uri $uri/ /index.html; \n\ | ||
| } \n\ | ||
| }' > /etc/nginx/conf.d/default.conf | ||
|
|
||
| # Expose port 3000 | ||
| EXPOSE 3000 | ||
|
|
||
| # Start Nginx | ||
| CMD ["nginx", "-g", "daemon off;"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Stage 1: Build the frontend | ||
| FROM node:20-alpine | ||
|
|
||
| # Set the working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy package.json | ||
|
|
||
| COPY package.json . | ||
|
|
||
| # Install all dependencies | ||
| RUN yarn install | ||
|
|
||
| # Copy the rest of the application files | ||
| COPY . . | ||
|
|
||
| # Expose the port for the application | ||
| EXPOSE 3000 | ||
|
|
||
| # Start Nginx | ||
| CMD ["yarn","dev", "--port", "3000","--host"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| node_modules | ||
| .gitignore | ||
| .git | ||
| .env.* | ||
| yarn.lock |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Use a lightweight Node.js Alpine image | ||
| FROM node:20-alpine | ||
|
|
||
| # Set the working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy package.json and yarn.lock files for dependency installation | ||
| COPY package.json ./ | ||
|
|
||
| # Install production dependencies using Yarn | ||
| RUN yarn install --production | ||
|
|
||
| # Copy the rest of the application files | ||
| COPY . . | ||
|
|
||
| # Expose the port for the application | ||
| EXPOSE 5000 | ||
|
|
||
| # Start the server in production mode using Yarn | ||
| CMD ["yarn", "start"] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change it to 'npm start'
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||||||||||||||||||
| # Use a lightweight Node.js Alpine image | ||||||||||||||||||||||
| FROM node:20-alpine | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Set the working directory | ||||||||||||||||||||||
| WORKDIR /app | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Copy package.json and yarn.lock files for dependency installation | ||||||||||||||||||||||
| COPY package.json ./ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Install all dependencies using Yarn | ||||||||||||||||||||||
| RUN yarn install | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Comment on lines
+7
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Copy the lock-file and switch to
-# Copy package.json and yarn.lock files for dependency installation
-COPY package.json ./
-
-# Install all dependencies using Yarn
-RUN npm install
+# Copy manifest + lock-file for proper layer caching
+COPY package*.json ./
+
+# Faster, deterministic install
+RUN npm ci📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| # Copy the rest of the application files | ||||||||||||||||||||||
| COPY . . | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Expose the port for the application | ||||||||||||||||||||||
| EXPOSE 5000 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Start the server in dev mode using Yarn | ||||||||||||||||||||||
| CMD ["yarn", "dev"] | ||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change it to 'npm run dev'
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| services: | ||
| frontend: # Defines the frontend service | ||
| image: frontend # Names the Docker image for the frontend service | ||
| container_name: frontend-container # Names the container for the frontend service | ||
| build: | ||
| context: . # Uses the current directory as the build context | ||
| dockerfile: Dockerfile.dev # Specifies the Dockerfile to use for building the image | ||
| ports: | ||
| - "3000:3000" # Maps port 3000 on the host machine to port 3000 in the container | ||
| env_file: | ||
| - .env # Loads environment variables from the .env file | ||
| volumes: | ||
| - .:/app # Mounts the entire project directory into the container for live code updates | ||
| - /app/node_modules # Prevents node_modules inside the container from being overwritten | ||
| depends_on: | ||
| - backend # Ensures the backend service starts before the frontend | ||
| networks: | ||
| - app-network # Connects the frontend to a custom Docker network | ||
|
|
||
| backend: # Defines the backend service | ||
| image: backend # Names the Docker image for the backend service | ||
| container_name: backend-container # Names the container for the backend service | ||
| build: | ||
| context: ./backend # Uses the "backend" directory as the build context | ||
| dockerfile: Dockerfile.dev # Specifies the Dockerfile to use for building the image | ||
| ports: | ||
| - "5000:5000" # Maps port 8000 on the host machine to port 8000 in the container | ||
| env_file: | ||
| - ./backend/.env # Loads environment variables from the .env file | ||
| volumes: | ||
| - ./backend:/app # Mounts the backend directory into the container for live code updates | ||
| - /app/node_modules # Prevents node_modules inside the container from being overwritten | ||
| networks: | ||
| - app-network # Connects the backend to a custom Docker network | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have one docker compose for dev and prod?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
| networks: | ||
| app-network: # Defines a shared network so that containers can communicate with each other | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| services: | ||
| frontend: | ||
| image: frontend | ||
| container_name: frontend-container | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| ports: | ||
| - "3000:3000" | ||
| env_file: | ||
| - .env | ||
| depends_on: | ||
| - backend | ||
| networks: | ||
| - app-network | ||
|
|
||
| backend: | ||
| image: backend | ||
| container_name: backend-container | ||
| build: | ||
| context: backend | ||
| dockerfile: Dockerfile | ||
| ports: | ||
| - "5000:5000" | ||
| env_file: | ||
| - backend/.env | ||
| networks: | ||
| - app-network | ||
|
|
||
| networks: | ||
| app-network: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,10 @@ | |
| "react-dom": "^18.3.1", | ||
| "react-hot-toast": "^2.4.1", | ||
| "react-icons": "^5.3.0", | ||
| "react-router-dom": "^6.28.0" | ||
| "react-router-dom": "^6.28.0", | ||
| "postcss": "^8.4.47", | ||
| "autoprefixer": "^10.4.20", | ||
| "tailwindcss": "^3.4.14" | ||
|
Comment on lines
+28
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate
@@
- "postcss": "^8.4.47",
- "autoprefixer": "^10.4.20",
- "tailwindcss": "^3.4.14"
+ "postcss": "^8.4.47",
+ "tailwindcss": "^3.4.14"
@@
- "autoprefixer": "^10.4.20",Move it to one block (usually Also applies to: 41-41 🤖 Prompt for AI Agents |
||
| }, | ||
| "devDependencies": { | ||
| "@eslint/js": "^9.13.0", | ||
|
|
@@ -30,14 +33,11 @@ | |
| "@types/react-dom": "^18.3.1", | ||
| "@types/react-redux": "^7.1.34", | ||
| "@vitejs/plugin-react-swc": "^3.5.0", | ||
| "autoprefixer": "^10.4.20", | ||
| "eslint": "^9.13.0", | ||
| "eslint-plugin-react": "^7.37.2", | ||
| "eslint-plugin-react-hooks": "^5.0.0", | ||
| "eslint-plugin-react-refresh": "^0.4.14", | ||
| "globals": "^15.11.0", | ||
| "postcss": "^8.4.47", | ||
| "tailwindcss": "^3.4.14", | ||
| "vite": "^5.4.10" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Lock-file omitted – builds will be slow and non-reproducible
Identical to the backend dev image, copy the lock-file and prefer
npm ci:🤖 Prompt for AI Agents