Skip to content

Implement production-ready Dockerfile and Docker Compose #24

@Nitya-003

Description

@Nitya-003

🧩 Problem

"It works on my machine" is a risk for a complex blockchain project like CropChain. Between different Node versions, OS-specific dependencies, and environment variables, setting up the project can be slow for new contributors.

Impact: Dockerization ensures that the environment in development, testing, and production (Ethereum/Polygon deployment) is 100% identical.

✨ Proposed Change

Create a multi-stage Dockerfile to optimize image size and a docker-compose.yml to orchestrate the Frontend, Backend, and a local MongoDB instance.

Technical Requirements:

  1. Dockerfile (Root/Production):
    • Stage 1 (Base): Use node:18-alpine as the base image.
    • Stage 2 (Builder): Install dependencies, copy source code, and run npm run build for the React frontend.
    • Stage 3 (Runner): A slim production-ready image that only contains the necessary /dist files and Node.js runtime.
  2. Docker Compose:
    • Define a db service using the official mongo:latest image.
    • Define a backend service linked to the database.
    • Define a frontend service (Vite/React).
  3. Environment Management:
    • Ensure the Docker setup can read from a .env file for sensitive data (Infura URLs, Private Keys, MongoDB URI).
  4. Optimization:
    • Add a .dockerignore file to exclude node_modules, build, and .git folders to keep the build context light.

✅ Acceptance Criteria

  • A functional Dockerfile exists that builds both the client and server.
  • Running docker-compose up starts the DB, Backend, and Frontend simultaneously.
  • The Docker image is optimized (under 400MB).
  • Documentation added to README.md under a new "Docker Setup" section.
  • The app is accessible at localhost:3000 (Frontend) and localhost:3001 (Backend) within the container network.

🛠️ Implementation Hint

For the Frontend build in Docker, remember to pass your VITE_ or REACT_APP_ environment variables during the build stage, otherwise, they won't be baked into the static files!

# Example Snippet
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions