Skip to content

Commit

Permalink
fix: missing web-app -> nextjs-app refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Apr 9, 2022
1 parent a9d053f commit 139353e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 63 deletions.
26 changes: 13 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ COPY . .
COPY --from=deps /workspace-install ./

# Optional: if the app depends on global /static shared assets like images, locales...
RUN yarn workspace web-app share-static-hardlink && yarn workspace web-app build
RUN yarn workspace nextjs-app share-static-hardlink && yarn workspace nextjs-app build

# Does not play well with buildkit on CI
# https://github.com/moby/buildkit/issues/1673
RUN --mount=type=cache,target=/root/.yarn3-cache,id=yarn3-cache \
SKIP_POSTINSTALL=1 \
YARN_CACHE_FOLDER=/root/.yarn3-cache \
yarn workspaces focus web-app --production
yarn workspaces focus nextjs-app --production

###################################################################
# Stage 3: Extract a minimal image from the build #
Expand All @@ -101,22 +101,22 @@ ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs

COPY --from=builder /app/apps/web-app/next.config.js \
/app/apps/web-app/next-i18next.config.js \
/app/apps/web-app/package.json \
./apps/web-app/
COPY --from=builder /app/apps/web-app/public ./apps/web-app/public
COPY --from=builder --chown=nextjs:nodejs /app/apps/web-app/.next ./apps/web-app/.next
COPY --from=builder /app/apps/nextjs-app/next.config.js \
/app/apps/nextjs-app/next-i18next.config.js \
/app/apps/nextjs-app/package.json \
./apps/nextjs-app/
COPY --from=builder /app/apps/nextjs-app/public ./apps/nextjs-app/public
COPY --from=builder --chown=nextjs:nodejs /app/apps/nextjs-app/.next ./apps/nextjs-app/.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

USER nextjs

EXPOSE ${WEB_APP_PORT:-3000}
EXPOSE ${NEXTJS_APP_PORT:-3000}

ENV NEXT_TELEMETRY_DISABLED 1

CMD ["./node_modules/.bin/next", "start", "apps/web-app/", "-p", "${WEB_APP_PORT:-3000}"]
CMD ["./node_modules/.bin/next", "start", "apps/nextjs-app/", "-p", "${NEXTJS_APP_PORT:-3000}"]


###################################################################
Expand All @@ -130,9 +130,9 @@ WORKDIR /app

COPY --from=deps /workspace-install ./

EXPOSE ${WEB_APP_PORT:-3000}
EXPOSE ${NEXTJS_APP_PORT:-3000}

WORKDIR /app/apps/web-app
WORKDIR /app/apps/nextjs-app

CMD ["yarn", "dev", "-p", "${WEB_APP_PORT:-3000}"]
CMD ["yarn", "dev", "-p", "${NEXTJS_APP_PORT:-3000}"]

24 changes: 12 additions & 12 deletions docker-compose.web-app.yml → docker-compose.nextjs-app.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: "3.9"
version: '3.9'
services:
# Start web-app in development
# docker-compose -f docker-compose.yml -f docker-compose.web-app.yml up main-db develop
develop:
container_name: web-app-develop
container_name: nextjs-app-develop
profiles:
- donotstart
stdin_open: true
Expand All @@ -20,14 +20,14 @@ services:
- .:/app/
# Prevent sharing of node_modules between host and container
# to avoid ownership and/or platform issues
- build_web-app:/app/apps/web-app/.next
- nm_web-app:/app/node_modules
- build_nextjs-app:/app/apps/nextjs-app/.next
- nm_nextjs-app:/app/node_modules
- nm_prisma:/app/packages/db-main-prisma/node_modules

# Convenience production "deps" stage service for debugging
# Build > docker buildx bake -f docker-compose.web-app.yml --progress=tty deps
deps:
container_name: web-app-multistage-deps
container_name: nextjs-app-multistage-deps
profiles:
- donotstart
stdin_open: true
Expand All @@ -37,9 +37,9 @@ services:
target: deps

# Convenience production "builder" stage service for debugging
# Build > docker buildx bake -f docker-compose.web-app.yml --progress=tty runner
# Build > docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty runner
builder:
container_name: web-app-multistage-builder
container_name: nextjs-app-multistage-builder
profiles:
- donotstart
stdin_open: true
Expand All @@ -49,10 +49,10 @@ services:
target: builder

# Convenience production "runner" stage service for debugging
# Build > docker buildx bake -f docker-compose.web-app.yml --progress=tty runner
# Run > docker-compose -f docker-compose.web-app.yml --env-file ./apps/web-app/.env.local up runner
# Build > docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty runner
# Run > docker-compose -f docker-compose.nextjs-app.yml --env-file ./apps/nextjs-app/.env.local up runner
runner:
container_name: web-app-multistage-runner
container_name: nextjs-app-multistage-runner
profiles:
- donotstart
stdin_open: true
Expand All @@ -68,6 +68,6 @@ services:
- PRISMA_DATABASE_URL

volumes:
build_web-app:
nm_web-app:
build_nextjs-app:
nm_nextjs-app:
nm_prisma:
2 changes: 1 addition & 1 deletion docs/about-lint-staged.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and the [linter docs](./about-linters.md).
│ ├── remix-app
│ │ ├── .eslintrc.js
│ │ └── lint-staged.config.js (overwrite global lint-staged.config.js, custom eslint)
│ └── web-app
│ └── nextjs-app
│ ├── .eslintrc.js
│ └── lint-staged.config.js (overwrite global lint-staged.config.js, custom eslint)
├── packages
Expand Down
2 changes: 1 addition & 1 deletion docs/about-linters.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ nested approach:
├── apps
│ ├── remix-app
│ │ └── .eslintrc.js (extends eslint.base: adds react, rtl...)
│ └── web-app
│ └── nextjs-app
│ └── .eslintrc.js (extends eslint.base: adds react, nextjs, rtl...)
├── packages
│ ├── db-main-prisma
Expand Down
61 changes: 30 additions & 31 deletions docs/docker/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ and build time (taking advantage of buildx cache).
```
.
├── apps
│ ├── blog-app
│ └── web-app
│ └── nextjs-app
├── packages
│ ├── core-lib
│ ├── db-main-prisma
Expand All @@ -18,9 +17,9 @@ and build time (taking advantage of buildx cache).
│ ├── assets
│ └── locales
├── .dockerignore
├── docker-compose.web-app.yml (specific for web-app)
├── docker-compose.yml (general services like postgresql...)
└── Dockerfile (multistage build for web-app)
├── docker-compose.nextjs-app.yml (specific for nextjs-app)
├── docker-compose.yml (general services like postgresql...)
└── Dockerfile (multistage build for nextjs-app)
```

## Requirements
Expand All @@ -35,38 +34,38 @@ and build time (taking advantage of buildx cache).
## Ready made commands

| Yarn script | Description |
| ----------------------------- | -------------------------------------------- |
| `yarn docker:web-app:develop` | Run apps/web-app in development mode |
| `yarn docker:web-app:install` | Install dependencies in cache mount |
| `yarn docker:web-app:build` | Create a production build |
| `yarn docker:web-app:serve` | Serve production build on localhost:3000, |
| `yarn docker:prune-cache` | **Run this regularly if using in local !!!** |
| Yarn script | Description |
| -------------------------------- | -------------------------------------------- |
| `yarn docker:nextjs-app:develop` | Run apps/nextjs-app in development mode |
| `yarn docker:nextjs-app:install` | Install dependencies in cache mount |
| `yarn docker:nextjs-app:build` | Create a production build |
| `yarn docker:nextjs-app:serve` | Serve production build on localhost:3000, |
| `yarn docker:prune-cache` | **Run this regularly if using in local !!!** |

> Build and serve commands requires to have a `./apps/web-app/.env.local` present.
> Build and serve commands requires to have a `./apps/nextjs-app/.env.local` present.
## Develop

```bash
yarn docker:web-app:develop
yarn docker:nextjs-app:develop

# Or alternatively
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.yml -f ./docker-compose.web-app.yml up develop main-db
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.yml -f ./docker-compose.nextjs-app.yml up develop main-db
```

<details>
<summary>Want to open a shell to debug ?</summary>

```bash
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.web-app.yml run --rm develop sh
DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.nextjs-app.yml run --rm develop sh
```

</details>

## Multistage in details

See the latest [./docker-compose.web-app.yml](https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docker-compose.web-app.yml)
and [./Dockerfile](https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docker-compose.web-app.yml).
See the latest [./docker-compose.nextjs-app.yml](https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docker-compose.nextjs-app.yml)
and [./Dockerfile](https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docker-compose.nextjs-app.yml).

PS: The goal of multistage is mainly to reduce the size of the resulting image, it also allows to skip deps stage (ie: install deps) when no changes are detected in your deps (lock file).

Expand All @@ -83,20 +82,20 @@ stages.
To build it independently

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --progress=tty deps
# docker buildx bake -f docker-compose.web-app.yml --progress=tty deps
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty deps
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty deps
```

To force a rebuild

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --no-cache --force-rm --progress=tty deps
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty deps
```

Want to open a shell into it ?

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml run --rm deps sh
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm deps sh
```

</details>
Expand All @@ -113,20 +112,20 @@ Then build the thing and remove devDependencies.
To build it independently

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --progress=tty builder
# docker buildx bake -f docker-compose.web-app.yml --progress=tty builder
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty builder
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty builder
```

To force a rebuild

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --no-cache --force-rm --progress=tty builder
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty builder
```

Want to open a shell into it ?

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml run --rm builder sh
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm builder sh
```

</details>
Expand All @@ -136,7 +135,7 @@ Then build the thing and remove devDependencies.
Launch a production build and listen by default to http://localhost:3000.

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml --env-file .env.secret up runner
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml --env-file .env.secret up runner
```

> PS: you'll have to provide your own .env with required runtime variables.
Expand All @@ -146,20 +145,20 @@ DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml --env-file .env.s
To build it independently

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --progress=tty runner
# docker buildx bake -f docker-compose.web-app.yml --progress=tty runner
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty runner
# docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty runner
```

To force a rebuild

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --no-cache --force-rm --progress=tty runner
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --no-cache --force-rm --progress=tty runner
```

Want to open a shell into it ?

```bash
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml run --rm runner sh
DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml run --rm runner sh
```

</details>
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
"packages:typecheck": "yarn workspaces foreach -ptv --include '@your-org/*' run typecheck",
"packages:clean": "yarn workspaces foreach -ptv --include '@your-org/*' run clean",
"docker:prune-cache": "docker buildx prune",
"docker:web-app:install": "docker buildx bake -f ./docker-compose.web-app.yml --progress=tty deps",
"docker:web-app:develop": "cross-env DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.yml -f ./docker-compose.web-app.yml up develop main-db",
"docker:web-app:develop:reinstall": "cross-env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --progress=tty --no-cache --force-rm develop",
"docker:web-app:build": "cross-env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.web-app.yml build --progress=tty runner",
"docker:web-app:serve": "docker-compose -f ./docker-compose.web-app.yml --env-file ./apps/web-app/.env.local up runner",
"docker:nextjs-app:install": "docker buildx bake -f docker-compose.nextjs-app.yml --progress=tty deps",
"docker:nextjs-app:develop": "cross-env DOCKER_BUILDKIT=1 docker-compose -f ./docker-compose.yml -f docker-compose.nextjs-app.yml up develop main-db",
"docker:nextjs-app:develop:reinstall": "cross-env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty --no-cache --force-rm develop",
"docker:nextjs-app:build": "cross-env DOCKER_BUILDKIT=1 docker-compose -f docker-compose.nextjs-app.yml build --progress=tty runner",
"docker:nextjs-app:serve": "docker-compose -f docker-compose.nextjs-app.yml --env-file ./apps/nextjs-app/.env.local up runner",
"docker:up:main-db": "docker-compose up -d main-db",
"docker:down": "docker-compose down",
"docker:nuke-all": "npm-run-all --sequential --continue-on-error --print-label \"docker:nuke:*\"",
Expand Down

0 comments on commit 139353e

Please sign in to comment.