diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..df7062d4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git/ +node_modules/ +docs/ +test/ +.sequelizerc.example +config.json.example +public/build + diff --git a/deployments/Dockerfile b/deployments/Dockerfile new file mode 100644 index 00000000..344b2b6f --- /dev/null +++ b/deployments/Dockerfile @@ -0,0 +1,56 @@ +FROM node:8.15.1-jessie AS BUILD +# use multi-stage build to build frontend javascript +WORKDIR /codimd + +COPY . ./ + +RUN yarn install --non-interactive --pure-lockfile && \ + yarn build + +# ---------------------------------------------------- +# Runtime Stage +FROM node:8.15.1 AS RUNTIME + +# build for production +ENV NODE_ENV production +ENV PATH="/home/codimd/.npm-global/bin:${PATH}" + +# setup isolated user for more security +ARG USER_NAME=codimd +ARG UID=1500 +ARG GID=1500 + +RUN set +x -ue && \ + wget https://github.com/hackmdio/portchecker/releases/download/v1.0.1/portchecker-linux-amd64.tar.gz && \ + tar xvf portchecker-linux-amd64.tar.gz -C /usr/local/bin && \ + mv /usr/local/bin/portchecker-linux-amd64 /usr/local/bin/pcheck && \ + # Add user and groupd + groupadd --gid $GID $USER_NAME && \ + useradd --uid $UID --gid $USER_NAME --no-log-init --create-home $USER_NAME && \ + # setup local npm global directory + mkdir /home/codimd/.npm-global && \ + echo "prefix=/home/codimd/.npm-global/" > /home/codimd/.npmrc && \ + # setup app dir + mkdir /codimd && \ + # adjust permission + chown -R $USER_NAME:$USER_NAME /home/codimd + +# Copy build stage file to runtime +COPY --from=BUILD /codimd /codimd +RUN chown -R $USER_NAME:$USER_NAME /codimd + +# change running user name +USER $USER_NAME +# build project +WORKDIR /codimd + +RUN set +x -ue && \ + cliVer=$(cat package.json | grep sequelize-cli | awk '{print substr($1, 2, length($1) - 3)"@"substr($2, 2, length($2) - 3)}') && \ + npm -g install "$cliVer" && \ + yarn install --production --non-interactive --pure-lockfile && \ + yarn cache clean + +VOLUME /codimd/public/uploads +EXPOSE 3000 + +ENTRYPOINT ["/codimd/docker-entrypoint.sh"] diff --git a/deployments/dev-Dockerfile b/deployments/dev-Dockerfile new file mode 100644 index 00000000..fe9e2c21 --- /dev/null +++ b/deployments/dev-Dockerfile @@ -0,0 +1,7 @@ +FROM node:8.15.1-jessie + +WORKDIR /codimd + +EXPOSE 3000 + +VOLUME ['/codimd/node_modules'] diff --git a/deployments/dev-docker-compose.yml b/deployments/dev-docker-compose.yml new file mode 100644 index 00000000..68982c8e --- /dev/null +++ b/deployments/dev-docker-compose.yml @@ -0,0 +1,25 @@ +version: '3' +services: + dev-database: + image: postgres:11.2 + environment: + POSTGRES_USER: codimd + POSTGRES_PASSWORD: password + POSTGRES_DB: codimd + dev-codimd: + build: + dockerfile: ./deployments/dev-Dockerfile + context: ../ + environment: + CMD_DB_URL: postgres://codimd:password@dev-database/codimd + volumes: + - ../:/codimd + - node_modules:/codimd/node_modules + - public_build:/codimd/public/build + - public_view_build:/codimd/public/views/build + ports: + - 3000:3000 +volumes: + node_modules: + public_build: + public_view_build: diff --git a/deployments/docker-compose.yml b/deployments/docker-compose.yml new file mode 100644 index 00000000..12c202be --- /dev/null +++ b/deployments/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' +services: + database: + image: postgres:11.2 + environment: + POSTGRES_USER: codimd + POSTGRES_PASSWORD: password + POSTGRES_DB: codimd + codimd: + build: + dockerfile: ./deployments/Dockerfile + context: ../ + environment: + CMD_DB_URL: postgres://codimd:password@database/codimd + ports: + - 3000:3000 diff --git a/deployments/docker-entrypoint.sh b/deployments/docker-entrypoint.sh new file mode 100755 index 00000000..f25fb984 --- /dev/null +++ b/deployments/docker-entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -euo pipefail + +pcheck -constr "$CMD_DB_URL" + +sequelize db:migrate + +node app.js diff --git a/lib/realtimeClientConnection.js b/lib/realtimeClientConnection.js index 44c42bf3..b8aaa054 100644 --- a/lib/realtimeClientConnection.js +++ b/lib/realtimeClientConnection.js @@ -82,7 +82,7 @@ class RealtimeClientConnection { } async changeNotePermission (newPermission) { - const changedRows = await models.Note.update({ + const [changedRows] = await models.Note.update({ permission: newPermission }, { where: {