Skip to content

Commit f1ae26e

Browse files
committed
Add Dockerfile to run build inside a container
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.
1 parent ba83685 commit f1ae26e

22 files changed

+5912
-47
lines changed

.dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Exclude directories we don't need from Docker context to improve build time
2+
node_modules
3+
www
4+
src

Dockerfile

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Multi stage build to allow us to improve performance
2+
FROM node:10-alpine as base
3+
WORKDIR /usr/src/app
4+
5+
# Install basic dependancies (Next.js, React)
6+
COPY test/docker/app/package*.json ./
7+
RUN npm ci
8+
9+
FROM node:10-alpine as app
10+
COPY --from=base /usr/src/app ./
11+
12+
# Copy last build of library into the image and install dependences for it.
13+
# This ensures the build is valid and package.json contains everything needed
14+
# to actually run the library.
15+
# Note: You must run `npm run build` first to build a release of the library
16+
RUN mkdir -p node_modules/next-auth
17+
# Copy all entrypoints for the library (if creating a new one, add it here)
18+
COPY index.js providers.js adapters.js client.js jwt.js node_modules/next-auth/
19+
# Copy the dist dir
20+
COPY dist node_modules/next-auth/dist
21+
# Copy the package.json for the library and install it's dependences
22+
COPY package*.json node_modules/next-auth/
23+
RUN cd node_modules/next-auth/ && npm ci
24+
25+
# Copy test pages across
26+
COPY test/docker/app/pages ./pages
27+
28+
RUN npm run build
29+
30+
CMD [ "npm", "start" ]

package.json

+5-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"watch": "npm run watch:js | npm run watch:css",
1414
"watch:js": "babel --watch src --out-dir dist",
1515
"watch:css": "postcss --watch src/**/*.css --base src --dir dist",
16+
"test:app:start": "docker-compose -f test/docker/app.yml up -d",
17+
"test:app:rebuild": "docker-compose -f test/docker/app.yml up -d --build",
18+
"test:app:stop": "docker-compose -f test/docker/app.yml down",
1619
"test": "npm run lint",
1720
"test:db": "npm run test:db:mysql && npm run test:db:postgres && npm run test:db:mongodb && npm run test:db:mssql",
1821
"test:db:mysql": "node test/mysql.js",
@@ -21,16 +24,8 @@
2124
"test:db:mssql": "node test/mssql.js",
2225
"test:e2e:run": "cypress run",
2326
"test:e2e:dev": "cypress open",
24-
"db:start": "docker-compose -f test/docker/docker-compose.yml up -d",
25-
"db:start:mongo": "docker-compose -f test/docker/mongo.yml up -d",
26-
"db:start:mysql": "docker-compose -f test/docker/mysql.yml up -d",
27-
"db:start:postgres": "docker-compose -f test/docker/postgres.yml up -d",
28-
"db:start:mssql": "docker-compose -f test/docker/mssql.yml up -d",
29-
"db:stop": "docker-compose -f test/docker/docker-compose.yml down",
30-
"db:stop:mongo": "docker-compose -f test/docker/mongo.yml down",
31-
"db:stop:mysql": "docker-compose -f test/docker/mysql.yml down",
32-
"db:stop:postgres": "docker-compose -f test/docker/postgres.yml down",
33-
"db:stop:mssql": "docker-compose -f test/docker/mssql.yml down",
27+
"db:start": "docker-compose -f test/docker/databases.yml up -d",
28+
"db:stop": "docker-compose -f test/docker/databases.yml down",
3429
"prepublishOnly": "npm run build",
3530
"publish:beta": "npm publish --tag beta",
3631
"publish:canary": "npm publish --tag canary",

test/docker/app.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Start test app with local databases inside the container.
2+
#
3+
# Note: Uses Docker Compose v2 as v3 doesn't currently support extends.
4+
# https://docs.docker.com/compose/compose-file/compose-file-v2/
5+
version: '2.3'
6+
7+
services:
8+
9+
app:
10+
build:
11+
context: ../../
12+
dockerfile: Dockerfile
13+
ports:
14+
- "3000:3000"
15+
16+
# mongo:
17+
# extends:
18+
# file: databases/mongo.yml
19+
# service: mongo
20+
21+
# mssql:
22+
# extends:
23+
# file: databases/mssql.yml
24+
# service: mssql
25+
26+
# mysql:
27+
# extends:
28+
# file: databases/mysql.yml
29+
# service: mysql
30+
31+
# postgres:
32+
# extends:
33+
# file: databases/postgres.yml
34+
# service: postgres

0 commit comments

Comments
 (0)