hapipal / boilerplate

A friendly, proven starting place for your next hapi plugin or deployment
https://hapipal.com
184 stars 26 forks source link

Production ready Docker flavor with docker-compose for local development #83

Closed timcosta closed 4 years ago

timcosta commented 4 years ago

@devinivy I made this PR in place of #78 as it is now up to date and based on the deployment flavor. This has all of the changes from comments on #78, as well as the changes you and I discussed regarding targets in DMs the other day.

timcosta commented 4 years ago
hpal-boilerplate git:(flavor-docker) ➜  dc up -d --build --remove-orphans web
Creating network "hpal-boilerplate_default" with the default driver
Building web
Step 1/18 : FROM node:erbium-alpine AS base
erbium-alpine: Pulling from library/node
c9b1b535fdd9: Pull complete
6ad7c27326e7: Pull complete
968d8b4948da: Pull complete
a1ec837a6331: Pull complete
Digest: sha256:bba77d0ca8820b43af898b3c50d4e8b68dc703ebbd958319af2f21f2d3c309f5
Status: Downloaded newer image for node:erbium-alpine
 ---> 927d03058714
Step 2/18 : RUN apk add --update dumb-init
 ---> Running in 1cafb8e30c15
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/1) Installing dumb-init (1.2.2-r1)
Executing busybox-1.31.1-r9.trigger
OK: 7 MiB in 17 packages
Removing intermediate container 1cafb8e30c15
 ---> e00e72a93f25
Step 3/18 : ENTRYPOINT ["dumb-init", "--"]
 ---> Running in 5932c5d90cfb
Removing intermediate container 5932c5d90cfb
 ---> bdebb4987176
Step 4/18 : WORKDIR /app
 ---> Running in f526b8a12e02
Removing intermediate container f526b8a12e02
 ---> 2f943ec49d87

Step 5/18 : FROM base AS dependencies
 ---> 2f943ec49d87
Step 6/18 : COPY package*.json ./
 ---> b0c5c6ef76c2
Step 7/18 : RUN npm set progress=false     && npm config set depth 0     && npm i
 ---> Running in a91c72cf2bc8
npm WARN deprecated @hapi/hoek@6.2.4: This version has been deprecated in accordance with the hapi support policy (hapi.im/support). Please upgrade to the latest version to get the best features, bug fixes, and security patches. If you are unable to upgrade at this time, paid support is available for older versions (hapi.im/commercial).

> nodemon@2.0.2 postinstall /app/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN hapipal-boilerplate@2.4.1 No description
npm WARN hapipal-boilerplate@2.4.1 No repository field.
npm WARN hapipal-boilerplate@2.4.1 No license field.

added 403 packages from 232 contributors and audited 1165 packages in 15.132s

10 packages are looking for funding
  run `npm fund` for details

found 1 low severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container a91c72cf2bc8
 ---> 190e110cf78f
Step 8/18 : COPY .eslintrc.json .
 ---> 36d501c8c13e
Step 9/18 : COPY ./server ./server
 ---> ac658c9beba7
Step 10/18 : COPY ./test ./test
 ---> 17f524b58972
Step 11/18 : COPY ./lib ./lib
 ---> d2252b5dee56
Step 12/18 : USER node
 ---> Running in 00a67f0d16e2
Removing intermediate container 00a67f0d16e2
 ---> 28df1d5237aa

Step 13/18 : FROM dependencies AS test
 ---> 28df1d5237aa
Step 14/18 : ENV NODE_ENV=test
 ---> Running in d11d68f91884
Removing intermediate container d11d68f91884
 ---> 4285a904c391
Step 15/18 : CMD ["npm", "test"]
 ---> Running in 2e7046b7fbaf
Removing intermediate container 2e7046b7fbaf
 ---> fe853200bb45

Step 16/18 : FROM dependencies AS development
 ---> 28df1d5237aa
Step 17/18 : EXPOSE 3000
 ---> Running in fab69b8adb9a
Removing intermediate container fab69b8adb9a
 ---> 6dc01d121eb7
Step 18/18 : CMD ["npm", "start"]
 ---> Running in d232f17d0e23
Removing intermediate container d232f17d0e23
 ---> fa5f97715a15

Successfully built fa5f97715a15
Successfully tagged hpal-boilerplate_web:latest
Creating hpal-boilerplate_web_1 ... done
hpal-boilerplate git:(flavor-docker) ➜  dc ps
         Name                       Command               State           Ports
----------------------------------------------------------------------------------------
hpal-boilerplate_web_1   dumb-init -- ./node_module ...   Up      0.0.0.0:3000->3000/tcp
hpal-boilerplate git:(flavor-docker) ➜  dc logs
Attaching to hpal-boilerplate_web_1
web_1        | [nodemon] 2.0.2
web_1        | [nodemon] to restart at any time, enter `rs`
web_1        | [nodemon] watching dir(s): lib/**/* server/**/*
web_1        | [nodemon] watching extensions: js,mjs,json
web_1        | [nodemon] starting `node server/index.js`
web_1        | Server started at http://0.0.0.0:3000

Output of a fresh build building and coming up listening on port 3000 using docker compose without any permissions errors while running as the node user

devinivy commented 4 years ago

Alright, cool! It must have been related to the way I rigged it up. This is gorgeous, and I'm basically ready to publish it as a flavor. Just one final question: is the .npmrc file critical to this setup? Just wondering your thoughts there. Usually I do end-up checking in a package-lock with my deployments but not my libraries.

timcosta commented 4 years ago

Great question Devin - I generally also use package-lock files, but noticed that pal doesn't come with one in any flavor, so i added the .npmrc so local would stop generating one i then had to remove/ignore. Happy to remove the file though, no attachments either way.

devinivy commented 4 years ago

Ah, that makes sense. Yeah, developing the boilerplate is weird because it's used so differently from a package. My intent is for the user to generate their own package-lock on initial install if they'd like, so users get the latest versions of all the packages in their lock file. Mind just removing that .npmrc file?

Second order of business: I was thinking I could push up a branch off the base branch hapipal:flavor-docker, and we could repoint this PR to that, then actually merge it. Sound good?

timcosta commented 4 years ago

.npmrc killed.

I think it would be great to use another branch for the merge, I wasnt sure how you wanted to handle that so I pointed at the branch that would provide the cleanest diff for reviewing.

timcosta commented 4 years ago

Okay, @devinivy the commit I just pushed does two things:

  1. Adds the NODE_ENV=production declaration in the release target
  2. Moves the USER node line to last so that all file modifications are done as the root user, and then for running the app the node user will be used. This helps enforce docker best practices as it ensure files won't be written to the file system. Only the release target has this declaration now, which will also make debugging in development and test easier.
devinivy commented 4 years ago

@timcosta 🎉this puppy is done, and I am very grateful for all the effort you put into this, thanks for bearing with me. I will merge and update the readme today :)