How can I dockerize a monorepo(built using turborepo) with hot reload enabled?

644 Views Asked by At

This is a monorepo, web and api share the common packages that is the root directory and built using turborepo

I need to dockerize a turborepo with hot reload support. Dockerization is only for local development not for production. The repo uses pnpm.

I referred this but failed as this example in for yarn https://github.com/vercel/turbo/tree/main/examples/with-docker

This is the structure of root directory

├── apps
├── docker-compose.yml
├── docs
├── FUNDING.json
├── LICENSE
├── node_modules
├── package.json
├── packages
├── pnpm-lock.yaml
├── pnpm-workspace.yaml
├── prettier.config.js
├── script
└── turbo.json

This is the structure of apps directory

apps/
├── api
│   ├── dist
│   ├── Dockerfile (yet to create)
│   ├── index.html
│   ├── node_modules
│   ├── package.json
│   ├── src
│   └── tsconfig.json
├── README.md
└── web
    ├── Dockerfile (yet to create)
    ├── next.config.js
    ├── next-env.d.ts
    ├── node_modules
    ├── package.json
    ├── postcss.config.js
    ├── public
    ├── script
    ├── src
    ├── tailwind.config.js
    ├── tsconfig.json
    └── tsconfig.tsbuildinfo

I referred this but failed as this example in for yarn https://github.com/vercel/turbo/tree/main/examples/with-docker

I have create a docker-compose.yml in the root directory

version: '3.9'

services:
  postgres:
    container_name: postgres
    image: postgres:16.1-alpine
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    networks:
      - app_network

  web:
    container_name: web
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
    restart: always
    environment:
      - DATABASE_URL="postgres://postgres:postgres@postgres:5432/postgres"
    ports:
      - 3001:3001
    networks:
      - app_network

  api:
    container_name: api
    build:
      context: .
      dockerfile: ./apps/api/Dockerfile
    restart: always
    ports:
      - 3000:3000
    networks:
      - app_network

networks:
  app_network:
    external: true
1

There are 1 best solutions below

0
On

For hot realoading for local development your write your dockerfiles normally. Then, for local development, typically have a separate dockerfile with mounted volume with the source code so you can edit it on host and changes come up into the container.

  web:
    container_name: web
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
    restart: always
    environment:
      - DATABASE_URL="postgres://postgres:postgres@postgres:5432/postgres"
    ports:
      - 3001:3001
    networks:
      - app_network
    volumes:
      - ./web:/where/you/copied/your/web/in/dockerfile:ro  # or not read-only, up to you

I would remove app_network:, docker-compose starts it's own network.