From fbf5e783d3531de5038333f6232896904e979529 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 24 Jun 2021 21:53:15 +0200 Subject: [PATCH] build & start commands dockerfile --- backend/.gitignore | 1 + backend/Dockerfile | 95 +++++++++++++++++++++++++++++++++++++++++++ backend/package.json | 3 ++ backend/tsconfig.json | 2 +- 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 backend/Dockerfile diff --git a/backend/.gitignore b/backend/.gitignore index 66ee735e1..8a3d521fc 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,2 +1,3 @@ /node_modules/ /.env +/build/ diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 000000000..fc853eb63 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,95 @@ +################################################################################## +# BASE ########################################################################### +################################################################################## +FROM node:12.19.0-alpine3.10 as base + +# ENVs (available in production aswell, can be overwritten by commandline or env file) +## DOCKER_WORKDIR would be a classical ARG, but that is not multi layer persistent - shame +ENV DOCKER_WORKDIR="/app" +## We Cannot do `$(date -u +'%Y-%m-%dT%H:%M:%SZ')` here so we use unix timestamp=0 +ENV BUILD_DATE="1970-01-01T00:00:00.00Z" +## We cannot do $(npm run version).${BUILD_NUMBER} here so we default to 0.0.0.0 +ENV BUILD_VERSION="0.0.0.0" +## We cannot do `$(git rev-parse --short HEAD)` here so we default to 0000000 +ENV BUILD_COMMIT="0000000" +## SET NODE_ENV +ENV NODE_ENV="production" +## App relevant Envs +ENV PORT="4000" + +# Labels +LABEL org.label-schema.build-date="${BUILD_DATE}" +LABEL org.label-schema.name="gradido:backend" +LABEL org.label-schema.description="Gradido GraphQL Backend" +LABEL org.label-schema.usage="https://github.com/gradido/gradido/blob/master/README.md" +LABEL org.label-schema.url="https://gradido.net" +LABEL org.label-schema.vcs-url="https://github.com/gradido/gradido/tree/master/backend" +LABEL org.label-schema.vcs-ref="${BUILD_COMMIT}" +LABEL org.label-schema.vendor="Gradido Community" +LABEL org.label-schema.version="${BUILD_VERSION}" +LABEL org.label-schema.schema-version="1.0" +LABEL maintainer="support@gradido.net" + +# Install Additional Software +## install: git +#RUN apk --no-cache add git + +# Settings +## Expose Container Port +EXPOSE ${PORT} + +## Workdir +RUN mkdir -p ${DOCKER_WORKDIR} +WORKDIR ${DOCKER_WORKDIR} + +################################################################################## +# DEVELOPMENT (Connected to the local environment, to reload on demand) ########## +################################################################################## +FROM base as development + +# We don't need to copy or build anything since we gonna bind to the +# local filesystem which will need a rebuild anyway + +# Run command +# (for development we need to execute npm install since the +# node_modules are on another volume and need updating) +CMD /bin/sh -c "yarn install && yarn run dev" + +################################################################################## +# BUILD (Does contain all files and is therefore bloated) ######################## +################################################################################## +FROM base as build + +# Copy everything +COPY . . +# npm install +RUN yarn install --production=false --frozen-lockfile --non-interactive +# npm build +RUN yarn run build + +################################################################################## +# TEST ########################################################################### +################################################################################## +FROM build as test + +# Run command +CMD /bin/sh -c "yarn run dev" + +################################################################################## +# PRODUCTION (Does contain only "binary"- and static-files to reduce image size) # +################################################################################## +FROM base as production + +# Copy "binary"-files from build image +COPY --from=build ${DOCKER_WORKDIR}/build ./build +# We also copy the node_modules express and serve-static for the run script +# COPY --from=build ${DOCKER_WORKDIR}/node_modules ./node_modules +# Copy static files +# COPY --from=build ${DOCKER_WORKDIR}/public ./public +# Copy package.json for script definitions (lock file should not be needed) +COPY --from=build ${DOCKER_WORKDIR}/package.json ./package.json +# Copy run scripts run/ +# COPY --from=build ${DOCKER_WORKDIR}/run ./run + +# Run command +CMD /bin/sh -c "yarn run start" \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 4ab300e13..2ffef3b58 100644 --- a/backend/package.json +++ b/backend/package.json @@ -8,6 +8,9 @@ "license": "MIT", "private": false, "scripts": { + "build": "tsc --build", + "clean": "tsc --build --clean", + "start": "node build/index.js", "dev": "nodemon -w src --ext ts --exec ts-node src/index.ts", "lint": "eslint . --ext .js,.ts" }, diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 422ff8e0a..d48524ba5 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -14,7 +14,7 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - // "outDir": "./", /* Redirect output structure to the directory. */ + "outDir": "./build", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */