From b23fcf2c0654a74d365c624843501f003221ca6f Mon Sep 17 00:00:00 2001 From: Kevyn Bruyere Date: Wed, 19 Aug 2020 10:20:45 +0200 Subject: [PATCH] Add an example project with dockerfile - 1st draft --- sections/examples/dockerfile/.dockerignore | 13 +++++++++ sections/examples/dockerfile/.npmrc | 0 sections/examples/dockerfile/Dockerfile | 31 ++++++++++++++++++++++ sections/examples/dockerfile/package.json | 28 +++++++++++++++++++ sections/examples/dockerfile/src/app.ts | 10 +++++++ 5 files changed, 82 insertions(+) create mode 100644 sections/examples/dockerfile/.dockerignore create mode 100644 sections/examples/dockerfile/.npmrc create mode 100644 sections/examples/dockerfile/Dockerfile create mode 100644 sections/examples/dockerfile/package.json create mode 100644 sections/examples/dockerfile/src/app.ts diff --git a/sections/examples/dockerfile/.dockerignore b/sections/examples/dockerfile/.dockerignore new file mode 100644 index 00000000..9d284936 --- /dev/null +++ b/sections/examples/dockerfile/.dockerignore @@ -0,0 +1,13 @@ +node_modules/ +.git +README.md +LICENSE +.vscode +.idea +npm-debug.log +coverage +.env +.editorconfig +.aws +dist +.npmrc diff --git a/sections/examples/dockerfile/.npmrc b/sections/examples/dockerfile/.npmrc new file mode 100644 index 00000000..e69de29b diff --git a/sections/examples/dockerfile/Dockerfile b/sections/examples/dockerfile/Dockerfile new file mode 100644 index 00000000..941f035e --- /dev/null +++ b/sections/examples/dockerfile/Dockerfile @@ -0,0 +1,31 @@ +FROM node:14.8.0-alpine AS build + +# Copy dependency information and install all dependencies +COPY --chown=node:node package.json package-lock.json ./ + +RUN npm ci + +# Copy source code (and all other relevant files) +COPY --chown=node:node src ./src + +# Build code +RUN npm run build + +# Run-time stage +FROM node:14.8.0-alpine + +# Set non-root user and expose port 3000 +USER node +EXPOSE 3000 + +WORKDIR /home/node/app + +# Copy dependency information and build results from previous stage +COPY --chown=node:node --from=build package.json package-lock.json ./ +COPY --chown=node:node --from=build node_modules ./node_modules +COPY --chown=node:node --from=build dist ./dist + +# Clean dev packages +RUN npm prune --production + +CMD [ "node", "dist/app.js" ] diff --git a/sections/examples/dockerfile/package.json b/sections/examples/dockerfile/package.json new file mode 100644 index 00000000..93944568 --- /dev/null +++ b/sections/examples/dockerfile/package.json @@ -0,0 +1,28 @@ +{ + "name": "node-app-with-docker", + "version": "1.0.0", + "description": "An example node app that uses docker to be built", + "main": "src/app.ts", + "scripts": { + "run": "dist/app.js", + "build": "tsc --outDir dist -m commonjs -t ES2020 src/app.ts", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/goldbergyoni/nodebestpractices.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/goldbergyoni/nodebestpractices/issues" + }, + "homepage": "https://github.com/goldbergyoni/nodebestpractices#readme", + "dependencies": { + "koa": "^2.13.0" + }, + "devDependencies": { + "@types/koa": "^2.11.4", + "typescript": "^3.9.7" + } +} diff --git a/sections/examples/dockerfile/src/app.ts b/sections/examples/dockerfile/src/app.ts new file mode 100644 index 00000000..d51890c1 --- /dev/null +++ b/sections/examples/dockerfile/src/app.ts @@ -0,0 +1,10 @@ +import * as Koa from 'koa'; +const app = new Koa(); + +app.use(async ctx => { + ctx.body = 'Hello World'; +}); + +app.listen(3000, () => { + console.log('Navigate to http://localhost:3000'); +});