Skip to content

Commit

Permalink
Add Dockerfile to run build inside a container
Browse files Browse the repository at this point in the history
Adds commands to start/rebuild/stop a Docker image of a sample Next.js app that loads the latest build of NextAuth.js from the current directory.

* `npm run test:app:start`
* `npm run test:app:rebuild`
* `npm run test:app:stop`

It is intended for further development for automated testing.

### About the build process

* The Dockerfile uses a multi-stage build process to optimise build performance, but the nature of the process is slow.
* Build times vary depending on computer speed and internet connection.
* Inital build times are slow (it may take 10 minutes or more).
* Subsequent builds on the same computer should be faster (1 minute or less).
* To ensure the package.json is valid, modules required in the next-auth package.json file are re-downloaded* on every build.
* A Docker compose file is used to allow us to extend the test app to run it again multiple databases.

Subsequent updates may look to improve performance, but it's important checks like checking package.json is valid and running the build in isolation are performed.
  • Loading branch information
iaincollins committed Sep 3, 2020
1 parent ba83685 commit f1ae26e
Show file tree
Hide file tree
Showing 22 changed files with 5,912 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
@@ -0,0 +1,4 @@
# Exclude directories we don't need from Docker context to improve build time
node_modules
www
src
30 changes: 30 additions & 0 deletions Dockerfile
@@ -0,0 +1,30 @@
# Multi stage build to allow us to improve performance
FROM node:10-alpine as base
WORKDIR /usr/src/app

# Install basic dependancies (Next.js, React)
COPY test/docker/app/package*.json ./
RUN npm ci

FROM node:10-alpine as app
COPY --from=base /usr/src/app ./

# Copy last build of library into the image and install dependences for it.
# This ensures the build is valid and package.json contains everything needed
# to actually run the library.
# Note: You must run `npm run build` first to build a release of the library
RUN mkdir -p node_modules/next-auth
# Copy all entrypoints for the library (if creating a new one, add it here)
COPY index.js providers.js adapters.js client.js jwt.js node_modules/next-auth/
# Copy the dist dir
COPY dist node_modules/next-auth/dist
# Copy the package.json for the library and install it's dependences
COPY package*.json node_modules/next-auth/
RUN cd node_modules/next-auth/ && npm ci

# Copy test pages across
COPY test/docker/app/pages ./pages

RUN npm run build

CMD [ "npm", "start" ]
15 changes: 5 additions & 10 deletions package.json
Expand Up @@ -13,6 +13,9 @@
"watch": "npm run watch:js | npm run watch:css",
"watch:js": "babel --watch src --out-dir dist",
"watch:css": "postcss --watch src/**/*.css --base src --dir dist",
"test:app:start": "docker-compose -f test/docker/app.yml up -d",
"test:app:rebuild": "docker-compose -f test/docker/app.yml up -d --build",
"test:app:stop": "docker-compose -f test/docker/app.yml down",
"test": "npm run lint",
"test:db": "npm run test:db:mysql && npm run test:db:postgres && npm run test:db:mongodb && npm run test:db:mssql",
"test:db:mysql": "node test/mysql.js",
Expand All @@ -21,16 +24,8 @@
"test:db:mssql": "node test/mssql.js",
"test:e2e:run": "cypress run",
"test:e2e:dev": "cypress open",
"db:start": "docker-compose -f test/docker/docker-compose.yml up -d",
"db:start:mongo": "docker-compose -f test/docker/mongo.yml up -d",
"db:start:mysql": "docker-compose -f test/docker/mysql.yml up -d",
"db:start:postgres": "docker-compose -f test/docker/postgres.yml up -d",
"db:start:mssql": "docker-compose -f test/docker/mssql.yml up -d",
"db:stop": "docker-compose -f test/docker/docker-compose.yml down",
"db:stop:mongo": "docker-compose -f test/docker/mongo.yml down",
"db:stop:mysql": "docker-compose -f test/docker/mysql.yml down",
"db:stop:postgres": "docker-compose -f test/docker/postgres.yml down",
"db:stop:mssql": "docker-compose -f test/docker/mssql.yml down",
"db:start": "docker-compose -f test/docker/databases.yml up -d",
"db:stop": "docker-compose -f test/docker/databases.yml down",
"prepublishOnly": "npm run build",
"publish:beta": "npm publish --tag beta",
"publish:canary": "npm publish --tag canary",
Expand Down
34 changes: 34 additions & 0 deletions test/docker/app.yml
@@ -0,0 +1,34 @@
# Start test app with local databases inside the container.
#
# Note: Uses Docker Compose v2 as v3 doesn't currently support extends.
# https://docs.docker.com/compose/compose-file/compose-file-v2/
version: '2.3'

services:

app:
build:
context: ../../
dockerfile: Dockerfile
ports:
- "3000:3000"

# mongo:
# extends:
# file: databases/mongo.yml
# service: mongo

# mssql:
# extends:
# file: databases/mssql.yml
# service: mssql

# mysql:
# extends:
# file: databases/mysql.yml
# service: mysql

# postgres:
# extends:
# file: databases/postgres.yml
# service: postgres

0 comments on commit f1ae26e

Please sign in to comment.