From 1cdbddf7539fdb596f0e6026d3f3b9ecb697e52f Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:18:13 +0100 Subject: [PATCH 1/3] Dockerize the server + Add Dockerfile + Update 'server' docker-compose service --- .dockerignore | 2 ++ Dockerfile | 29 +++++++++++++++++++++++++++++ docker-compose.yml | 14 ++++++++++---- scripts/docker-entrypoint.sh | 11 +++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 scripts/docker-entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e7b3e6b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +*.env +dist/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..242306b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Dockerfile for the event management server + +FROM node:17-alpine3.12 + +# Create directory for the app +RUN mkdir /home/server + +# Selectively copy the required files and directories +ADD prisma/ /home/app/prisma/ +ADD scripts/ /home/app/scripts/ +ADD src/ /home/app/src/ +COPY package-lock.json /home/app +COPY package.json /home/app +COPY tsconfig.json /home/app + +# Change directory into the container +WORKDIR /home/app + +# Install dependencies +RUN npm i -g typescript +RUN npm i + +# Generate the prisma client +RUN npx prisma generate + +# Compile TypeScript to JavaScript +RUN tsc + +CMD ["sh", "scripts/docker-entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 3edb025..2157a8f 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,13 @@ services: ports: - 5432:5432 container_name: "postgres" - #server: - # build: . - # restart: always - # container_name: "server" + server: + build: . + restart: always + container_name: "server" + ports: + - ${PORT}:${PORT} + environment: + - PORT=${PORT} + - DATABASE_URL=${DATABASE_URL} + - DATABASE_PASSWORD=${DATABASE_PASSWORD} diff --git a/scripts/docker-entrypoint.sh b/scripts/docker-entrypoint.sh new file mode 100644 index 0000000..fd37bb0 --- /dev/null +++ b/scripts/docker-entrypoint.sh @@ -0,0 +1,11 @@ +# Entry point for docker + +if ! test -f INITIALIZED; then + # Migrate the database + # TODO: Update to use prisma migrate + npx prisma db push + touch INITIALIZED +fi + +# Start the server +node ./dist/app.js From 5a62d156ae839b0f7b12eb00e05b128722ccf0a7 Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Fri, 12 Nov 2021 11:55:38 +0100 Subject: [PATCH 2/3] Add defaults for some environment variables + Remove port bindings for the 'postgres' service --- docker-compose.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 2157a8f..72d2e5c 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,17 +14,17 @@ services: image: postgres restart: always environment: + - POSTGRES_USER=server - POSTGRES_PASSWORD=${DATABASE_PASSWORD} - ports: - - 5432:5432 container_name: "postgres" server: build: . restart: always container_name: "server" ports: - - ${PORT}:${PORT} + - ${PORT:-3000}:${PORT:-3000} environment: - - PORT=${PORT} - - DATABASE_URL=${DATABASE_URL} + - PORT=${PORT:-3000} + - DATABASE_URL=postgresql://server:${DATABASE_PASSWORD}@postgres:5432/management?schema=public + - DATABASE_USER=server - DATABASE_PASSWORD=${DATABASE_PASSWORD} From c53e325f06805a8a56b67cf0f696a4c409e5beb8 Mon Sep 17 00:00:00 2001 From: Stephan <57194608+stephan418@users.noreply.github.com> Date: Mon, 15 Nov 2021 08:41:18 +0100 Subject: [PATCH 3/3] Change layer order (make caching more effecient) --- Dockerfile | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 242306b..8916e84 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,23 +3,24 @@ FROM node:17-alpine3.12 # Create directory for the app -RUN mkdir /home/server +RUN mkdir /app -# Selectively copy the required files and directories -ADD prisma/ /home/app/prisma/ -ADD scripts/ /home/app/scripts/ -ADD src/ /home/app/src/ -COPY package-lock.json /home/app -COPY package.json /home/app -COPY tsconfig.json /home/app +# Copy dependency files +COPY package.json package-lock.json /app # Change directory into the container -WORKDIR /home/app +WORKDIR /app # Install dependencies RUN npm i -g typescript RUN npm i +# Selectively copy the required files and directories +ADD prisma/ /app/prisma/ +ADD scripts/ /app/scripts/ +ADD src/ /app/src/ +COPY tsconfig.json /app + # Generate the prisma client RUN npx prisma generate