Skip to content
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

feat: document how to run postgres in docker #266

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
65 changes: 65 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Setting up Docker

Bison includes a `docker-compose.yml`, `Dockerfile` and `.dockerignore`

The configuration is to run a postgres database locally.

If you are interested in running the entire app, please see the examples below.

## Running the Database

To run postgres with docker, please add the following information to your `env` & `env.local` file.
Copy link
Contributor

Choose a reason for hiding this comment

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

...your .env and .env.local file

```
DATABASE_URL="postgresql://dev:dev@localhost:5432/dev?schema=public"
```
Run `docker-compose up -d` in your terminal. If you prefer not to run in detached mode, you can remove the `-d` flag.

To shut down docker, run `docker-compose down`

## Using NextJS & Postgres with Docker
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be consistent with Next's official name, it's Next.js. Same throughout.


The following configuration will help you run NextJS & Postgres.

Make sure `@prisma/client` is on version: `3.13.0`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this needs to be on version 3.13.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will eliminate this. I was writing this when working on node:alpine Prisma does not work well on < 3.13.0

The error was related to running node:alpine. It will not apply if we are using node:lts

When following NextJS docs they are using node:alpine (https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile). Running Prisma and alpine has some issues and upgrading made it work:

prisma/prisma#13218 (comment)


Dockerfile
```Dockerfile
FROM node:lts
WORKDIR /usr/src/app

COPY package.json yarn.lock ./
RUN yarn install

COPY . .

EXPOSE 3000

CMD ["yarn", "dev"]
```

Add this to your services in `docker-compose.yml` file:
```docker
services:
nextjs:
build:
context: ./
command: bash -c "yarn db:migrate && yarn db:seed && yarn dev"
container_name: nextjs
ports:
- '3000:3000'
volumes:
- .:/app
- /app/node_modules
```

Change your database URL in your `env.local` to:
```
DATABASE_URL="postgres://dev:dev@postgres/dev?schema=public"
```

Somtimes your NextJs will not run due to postgres still booting up. You can add `connect_timeout=300` to the database URL. If you continue to have issues, you can press the `start` button in the docker application.
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be a problem if you use depends_on: https://docs.docker.com/compose/compose-file/compose-file-v3/#depends_on


To run NextJs with Prisma, it is best to use `node:lts`. There are current issues with running NextJs and Prisma using `node:alpine`

Run `docker-compose up -d`
Run `docker-compose down` to shut down.
3 changes: 3 additions & 0 deletions packages/create-bison-app/tasks/copyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,16 @@ async function copyFiles({ variables, targetFolder }) {
"scripts",
"services",
"utils",
".dockerignore",
".eslintrc.js",
".hygen.js",
".nvmrc",
".tool-versions",
"codegen.yml",
"constants.ts",
"cypress.json",
"docker-compose.yml",
"Dockerfile",
"jest.config.js",
"next-env.d.ts",
"prettier.config.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/create-bison-app/template/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/.data
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any more folders that should be added here? Basically you'd want to exclude anything that isn't needed to run the app (e.g. tests). Ideally all of the app code would be in one folder (e.g. src) and we could just mount that folder in docker and not need a .dockerignore file, but Bison's organized a bit differently.

15 changes: 15 additions & 0 deletions packages/create-bison-app/template/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Default configuration to run your NextJs app locally:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to use a Dockerfile for this? Could we just mount the entire ./ folder (excluding .data and likely a few other folders) in docker-compose.yml and run directly from there?


# FROM node:lts

# WORKDIR /usr/src/app

# COPY package.json yarn.lock ./
# # COPY prisma ./prisma/
# RUN yarn install

# COPY . .

# EXPOSE 3000

# CMD ["yarn", "dev"]
15 changes: 15 additions & 0 deletions packages/create-bison-app/template/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.8'

services:
postgres:
container_name: postgres
ports:
- 5432:5432
Copy link
Contributor

@cullylarson cullylarson Jul 5, 2022

Choose a reason for hiding this comment

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

I'm wondering if we should change the port. If the user is running a local version of Postgres, this would fail.

image: postgres:14
environment:
POSTGRES_USER: dev
POSTGRES_PASSWORD: dev
POSTGRES_DB: dev
restart: always
volumes:
- ./.data/postgres:/var/lib/postgresql/data
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the .data/postgres folder idea; that it leaves room for other data folders 👏