Last active
November 12, 2022 03:11
-
-
Save vinicius73/9e8cfa365893639342e79cd4c228a9b7 to your computer and use it in GitHub Desktop.
Node APP - Docker Build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/.vscode | |
/.git | |
/devops | |
/node_modules | |
/coverage | |
/devops | |
/dist | |
/tests | |
/assets | |
.editorconfig | |
.env* | |
*.log | |
*.md | |
*.yml | |
!.yarnrc.yml | |
.pnp.* | |
.yarn/* | |
!.yarn/patches | |
!.yarn/plugins | |
!.yarn/releases | |
!.yarn/sdks | |
!.yarn/versions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
APP_VERSION=${BITBUCKET_TAG} | |
BUILD_NUMBER=${BITBUCKET_BUILD_NUMBER:=unknown} | |
BUILDER=${USER}@$(hostname 2>/dev/null && echo $? | tail -0 || echo 'ci') | |
BUILD_DATE=$(date '+%Y-%m-%d__%H:%M:%S') | |
COMMIT=$(git rev-parse HEAD 2>/dev/null && echo $? | tail -0 || echo $BITBUCKET_COMMIT) | |
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null && echo $? | tail -0 || echo '') | |
if [ -z "$APP_VERSION" ]; then | |
APP_VERSION=$(node -p "require('./package.json').version" 2>/dev/null && echo $? | tail -0 || echo "$BUILD_NUMBER@$BUILD_DATE") | |
fi | |
if [ -z "$BRANCH" ]; then | |
BRANCH=${BITBUCKET_TAG:=$BITBUCKET_BRANCH} | |
fi | |
if [ -z "$DOCKER_BUILDKIT" ]; then | |
DOCKER_BUILDKIT=1 | |
fi | |
if [ -z "$DOCKER_PROGRESS" ]; then | |
DOCKER_PROGRESS=tty | |
fi | |
GIT_HASH=${BRANCH}@${COMMIT} | |
echo '@> Building backend image...' | |
echo "@=> APP_VERSION=${APP_VERSION}" | |
echo "@=> GIT_HASH=${GIT_HASH}" | |
echo "@=> BUILDER=${BUILDER}" | |
echo "@=> BUILD_DATE=${BUILD_DATE}" | |
echo "@=> BUILD_NUMBER=${BUILD_NUMBER}" | |
echo '' | |
DOCKER_BUILDKIT=${DOCKER_BUILDKIT} docker build --progress=${DOCKER_PROGRESS} -t project:bar \ | |
--build-arg APP_VERSION=${APP_VERSION} \ | |
--build-arg GIT_HASH=${GIT_HASH} \ | |
--build-arg BUILDER=${BUILDER} \ | |
--build-arg BUILD_DATE=${BUILD_DATE} \ | |
--build-arg BUILD_NUMBER=${BUILD_NUMBER} \ | |
. | |
echo ">>> FIM" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -o errexit | |
set -o nounset | |
OPTIND=1 | |
ENTRYPOINT_ACTION="$1" | |
banner () { | |
echo " | |
█████╗ ██████╗ ██████╗ | |
██╔══██╗██╔══██╗██╔══██╗ | |
███████║██████╔╝██████╔╝ | |
██╔══██║██╔═══╝ ██╔═══╝ | |
██║ ██║██║ ██║ | |
╚═╝ ╚═╝╚═╝ ╚═╝ | |
NODE_ENV = ${NODE_ENV} | |
APP_WORKDIR = ${APP_WORKDIR} | |
APP_INSTANCES = ${APP_INSTANCES:-} | |
ACTION = ${ENTRYPOINT_ACTION:-} | |
APP_VERSION = ${APP_VERSION:-} | |
GIT_HASH = ${GIT_HASH:-} | |
LOG_LEVEL = ${LOG_LEVEL:-} | |
PORT = ${PORT:-} | |
" | |
} | |
action_app () { | |
cd "$APP_WORKDIR" && \ | |
npx pm2-runtime ecosystem.config.js --env "$NODE_ENV" | |
} | |
case $ENTRYPOINT_ACTION in | |
app) | |
banner && action_app | |
;; | |
*) | |
exec "$@" | |
;; | |
esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Build Stage | |
FROM node:16-alpine as builder | |
RUN apk --update --no-cache add \ | |
python3 gcc g++ make tzdata curl bind-tools | |
WORKDIR /home/node | |
# Dependencies | |
COPY *.json *.js yarn.lock .yarn* ./ | |
COPY .yarn/ .yarn/ | |
RUN yarn install --immutable | |
# Copy source code | |
COPY . . | |
# Build | |
RUN yarn build | |
# Production | |
RUN rm -rf node_modules && \ | |
yarn workspaces focus --all --production && \ | |
yarn cache clean | |
# bitbucket fix | |
RUN chown -R root:root ./dist && \ | |
chown -R root:root ./node_modules | |
FROM node:16-alpine | |
RUN apk add --no-cache dumb-init | |
WORKDIR /home/node | |
# Copy dist files | |
COPY --from=builder /home/node/dist ./dist | |
COPY --from=builder /home/node/node_modules ./node_modules | |
COPY ecosystem.config.js package*.json yarn.lock .yarnclean ./ | |
COPY .yarn/ .yarn/ | |
RUN mkdir -p ./logs && \ | |
chmod 777 ./logs && \ | |
chown -R node:node ./logs && \ | |
ls -lah && du -sh node_modules/ | |
# Entrypoint | |
COPY docker-entrypoint.sh /usr/bin | |
RUN chmod +x /usr/bin/docker-entrypoint.sh | |
ENTRYPOINT ["/usr/bin/dumb-init", "--rewrite", "15:2", "--", "/usr/bin/docker-entrypoint.sh"] | |
USER node | |
# args | |
ARG APP_VERSION=unknown | |
ARG GIT_HASH=unknown | |
ARG BUILDER=unknown | |
ARG BUILD_NUMBER=unknown | |
ARG BUILD_DATE=unknown | |
# Labels. | |
LABEL name="app" \ | |
description="Node APP" \ | |
vcs.url="https://github.com/foo/app" \ | |
vcs.ref=$GIT_HASH \ | |
version=$APP_VERSION \ | |
build.date=$BUILD_DATE \ | |
build.number=$BUILD_NUMBER \ | |
build.builder=$BUILDER | |
# Environment | |
ENV GIT_HASH=$GIT_HASH \ | |
APP_VERSION=$APP_VERSION \ | |
PORT=3088 \ | |
LOG_LEVEL=info \ | |
NODE_ENV=production | |
EXPOSE 3088 | |
CMD ["app"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
apps: [{ | |
name: 'app', | |
script: './bin/index.js', | |
instances: Number(process.env.APP_INSTANCES || 1), | |
ignore_watch: ['node_modules', 'data', '.git', 'src', '*.log', '*.map'], | |
kill_timeout: 10000, | |
cwd: __dirname, | |
env: { }, | |
env_production: { } | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment