Skip to content

Commit 1f5b981

Browse files
committed
feat: add new dockerfile
Signed-off-by: BoHong Li <[email protected]>
1 parent b074b17 commit 1f5b981

9 files changed

+81
-110
lines changed

.dockerignore

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
.git/
1+
.idea
2+
coverage
23
node_modules/
3-
docs/
4-
test/
5-
.sequelizerc.example
6-
config.json.example
4+
5+
# ignore config files
6+
config.json
7+
.sequelizerc
8+
9+
# ignore webpack build
710
public/build
11+
public/views/build
812

13+
.nyc_output
14+
coverage/

.sequelizerc.example

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var path = require('path');
1+
const path = require('path')
2+
const config = require('./lib/config')
23

34
module.exports = {
4-
'config': path.resolve('config.json'),
5-
'migrations-path': path.resolve('lib', 'migrations'),
6-
'models-path': path.resolve('lib', 'models'),
7-
'url': 'change this'
8-
}
5+
config: path.resolve('config.json'),
6+
'migrations-path': path.resolve('lib', 'migrations'),
7+
'models-path': path.resolve('lib', 'models'),
8+
url: process.env['CMD_DB_URL'] || config.dbURL
9+
}

deployments/Dockerfile

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,23 @@
1-
FROM node:8.15.1-jessie AS BUILD
2-
# use multi-stage build to build frontend javascript
3-
WORKDIR /codimd
4-
5-
COPY . ./
6-
7-
RUN yarn install --non-interactive --pure-lockfile && \
8-
yarn build
9-
10-
# ----------------------------------------------------
11-
# Runtime Stage
12-
FROM node:8.15.1 AS RUNTIME
13-
14-
# build for production
15-
ENV NODE_ENV production
16-
ENV PATH="/home/codimd/.npm-global/bin:${PATH}"
17-
18-
# setup isolated user for more security
19-
ARG USER_NAME=codimd
20-
ARG UID=1500
21-
ARG GID=1500
22-
23-
RUN set +x -ue && \
24-
wget https://github.com/hackmdio/portchecker/releases/download/v1.0.1/portchecker-linux-amd64.tar.gz && \
25-
tar xvf portchecker-linux-amd64.tar.gz -C /usr/local/bin && \
26-
mv /usr/local/bin/portchecker-linux-amd64 /usr/local/bin/pcheck && \
27-
# Add user and groupd
28-
groupadd --gid $GID $USER_NAME && \
29-
useradd --uid $UID --gid $USER_NAME --no-log-init --create-home $USER_NAME && \
30-
# setup local npm global directory
31-
mkdir /home/codimd/.npm-global && \
32-
echo "prefix=/home/codimd/.npm-global/" > /home/codimd/.npmrc && \
33-
# setup app dir
34-
mkdir /codimd && \
35-
# adjust permission
36-
chown -R $USER_NAME:$USER_NAME /home/codimd
37-
38-
# Copy build stage file to runtime
39-
COPY --from=BUILD /codimd /codimd
40-
RUN chown -R $USER_NAME:$USER_NAME /codimd
41-
42-
# change running user name
43-
USER $USER_NAME
44-
# build project
45-
WORKDIR /codimd
46-
47-
RUN set +x -ue && \
48-
cliVer=$(cat package.json | grep sequelize-cli | awk '{print substr($1, 2, length($1) - 3)"@"substr($2, 2, length($2) - 3)}') && \
49-
npm -g install "$cliVer" && \
50-
yarn install --production --non-interactive --pure-lockfile && \
51-
yarn cache clean
52-
53-
VOLUME /codimd/public/uploads
1+
FROM hackmdio/buildpack:1.0.4 as BUILD
2+
3+
COPY --chown=hackmd:hackmd . .
4+
5+
RUN set -xe && \
6+
git reset --hard && \
7+
git clean -fx && \
8+
yarn install && \
9+
yarn build && \
10+
yarn install --production=true && \
11+
cp ./deployments/docker-entrypoint.sh ./ && \
12+
cp .sequelizerc.example .sequelizerc && \
13+
rm -rf .git .gitignore .travis.yml .dockerignore .editorconfig .babelrc .mailmap .sequelizerc.example \
14+
test docs contribute \
15+
yarn.lock webpack.prod.js webpack.htmlexport.js webpack.dev.js webpack.common.js \
16+
config.json.example README.md CONTRIBUTING.md AUTHORS
17+
18+
FROM hackmdio/runtime:1.0.4
19+
USER hackmd
20+
WORKDIR /home/hackmd/app
21+
COPY --from=BUILD /home/hackmd/app .
5422
EXPOSE 3000
55-
56-
ENTRYPOINT ["/codimd/docker-entrypoint.sh"]
23+
ENTRYPOINT ["/home/hackmd/app/docker-entrypoint.sh"]

deployments/build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
CURRENT_DIR=$(dirname "$BASH_SOURCE")
4+
5+
docker build -t hackmdio/codimd -f "$CURRENT_DIR/Dockerfile" "$CURRENT_DIR/.."

deployments/dev-Dockerfile

Lines changed: 0 additions & 7 deletions
This file was deleted.

deployments/dev-docker-compose.yml

Lines changed: 0 additions & 25 deletions
This file was deleted.

deployments/docker-compose.yml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
version: '3'
1+
version: "3"
22
services:
33
database:
4-
image: postgres:11.2
4+
image: postgres:11.5
55
environment:
6-
POSTGRES_USER: codimd
7-
POSTGRES_PASSWORD: password
8-
POSTGRES_DB: codimd
6+
- POSTGRES_USER=codimd
7+
- POSTGRES_PASSWORD=change_password
8+
- POSTGRES_DB=codimd
9+
volumes:
10+
- "database-data:/var/lib/postgresql/data"
11+
restart: always
912
codimd:
1013
build:
14+
context: ..
1115
dockerfile: ./deployments/Dockerfile
12-
context: ../
1316
environment:
14-
CMD_DB_URL: postgres://codimd:password@database/codimd
17+
- CMD_DB_URL=postgres://codimd:change_password@database/codimd
18+
- CMD_USECDN=false
19+
depends_on:
20+
- database
1521
ports:
16-
- 3000:3000
22+
- "3000:3000"
23+
volumes:
24+
- upload-data:/home/hackmd/app/public/uploads
25+
restart: always
26+
volumes:
27+
database-data: {}
28+
upload-data: {}

deployments/docker-entrypoint.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
set -euo pipefail
44

5+
if [[ "$#" -gt 0 ]]; then
6+
exec "$@"
7+
exit $?
8+
fi
9+
10+
# check database and redis is ready
511
pcheck -constr "$CMD_DB_URL"
612

7-
sequelize db:migrate
13+
# run DB migrate
14+
NEED_MIGRATE=${CMD_AUTO_MIGRATE:=true}
15+
16+
if [[ "$NEED_MIGRATE" = "true" ]] && [[ -f .sequelizerc ]] ; then
17+
npx sequelize db:migrate
18+
fi
819

20+
# start application
921
node app.js

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"license": "AGPL-3.0",
1616
"main": "app.js",
1717
"scripts": {
18-
"build": "webpack --config webpack.prod.js --progress --colors --bail",
18+
"build": "webpack --config webpack.prod.js --display errors-only -p",
1919
"dev": "webpack --config webpack.dev.js --progress --colors --watch",
2020
"doctoc": "doctoc --title='# Table of Contents' README.md",
2121
"lint": "standard",
@@ -128,6 +128,7 @@
128128
"scrypt": "~6.0.3",
129129
"select2": "~3.5.2-browserify",
130130
"sequelize": "5.3.5",
131+
"sequelize-cli": "~5.4.0",
131132
"shortid": "~2.2.14",
132133
"socket.io": "~2.2.0",
133134
"socket.io-client": "~2.2.0",
@@ -179,7 +180,6 @@
179180
"optimize-css-assets-webpack-plugin": "~5.0.0",
180181
"power-assert": "~1.6.1",
181182
"script-loader": "~0.7.2",
182-
"sequelize-cli": "~5.4.0",
183183
"sinon": "~7.3.2",
184184
"standard": "~13.1.0",
185185
"string-loader": "~0.0.1",

0 commit comments

Comments
 (0)