Skip to content

Simple implementation of Docker for development and production #96

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/node_modules
/.pnp
.pnp.js

/docker

npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vercel
.gitignore
.github
.next
.prettierrc
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
build-dev:
docker-compose -f docker/docker-compose.dev.yml build --no-cache

run-dev:
docker-compose -f docker/docker-compose.dev.yml up

stop-dev:
docker-compose -f docker/docker-compose.dev.yml down

rm-dev:
docker-compose -f docker/docker-compose.dev.yml down -v

build-prod:
docker-compose -f docker/docker-compose.prod.yml build --no-cache

run-prod:
docker-compose -f docker/docker-compose.prod.yml up

stop-prod:
docker-compose -f docker/docker-compose.prod.yml down

rm-prod:
docker-compose -f docker/docker-compose.prod.yml down -v
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Start the server
pnpm dev
```

### Using Docker

This project provides a development and production Dockerfiles for your needs and a docker-compose to deploy locally the project with the required configuration. The development Dockerfile creates a volume pointing to /node_modules so you can match the files between your machine and the container, while the production Dockerfile will simply compile the code. There's a Makefile to build, run, stop the containers and remove their volumes if needed, or you can execute the commands directly from the project's root.

### Setup Xata

You will need to sign up with Xata and set up a database for the build. After registering for an account, please proceed with the following steps.
Expand Down
21 changes: 21 additions & 0 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:18-slim AS base

RUN npm i -g pnpm

FROM base AS dependencies

COPY package*.json pnpm-lock.yaml ./

RUN pnpm install

FROM base AS builder

WORKDIR /app

COPY . .

COPY --from=dependencies /node_modules ./node_modules

FROM builder AS dev

CMD ["pnpm", "dev"]
40 changes: 40 additions & 0 deletions docker/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM node:18-alpine AS base

RUN npm i -g pnpm

FROM base AS dependencies

COPY package*.json pnpm-lock.yaml ./

RUN pnpm install

FROM base AS builder

WORKDIR /app

COPY . .

COPY --from=dependencies /node_modules ./node_modules

RUN pnpm build

FROM builder AS prod

ENV NODE_ENV production

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

COPY --from=builder /app/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

CMD ["pnpm", "start"]
17 changes: 17 additions & 0 deletions docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3.8'

services:
app:
container_name: hacktoberfest_projects_dev
build:
context: ../
dockerfile: docker/Dockerfile.dev
target: dev
environment:
- NODE_ENV=development
ports:
- 3000:3000
volumes:
- ../:/app
- /app/node_modules
- /app/.next
12 changes: 12 additions & 0 deletions docker/docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try adding restart: on-failure for cases when it fails

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try adding restart: on-failure for cases when it fails

I've added the restart property for production, before I've added it for development too but now I've removed it

services:
app:
container_name: hacktoberfest_projects_prod
build:
context: ../
dockerfile: docker/Dockerfile.prod
target: prod
restart: on-failure
ports:
- 3000:3000
8 changes: 8 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ await import('./env.mjs');
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
swcMinify: true,
webpack: (config, context) => {
config.watchOptions = {
poll: 1000,
aggregateTimeout: 300
}
return config
},
async redirects() {
return [
{
Expand Down
Loading