A solution to setup Dockerfile to build Bcrypt
Install bcrypt to package.json
...
"dependencies": {
"bcrypt": "^5.0.1",
...
}
index.js
const Fastify = require('fastify')
const bcrypt = require('bcrypt')
const hostname = '0.0.0.0'
const port = 4000
const app = Fastify()
app.listen(port, hostname, () => {
console.log(`inside create server #port= ${port}`)
})
app.get('/', (request, reply) => {
reply.send('OK')
})
app.get('/bcrypt/:password', async (request, reply) => {
const { password } = request.params
// console.log('password ->', password)
const setRounds = 5
const salt = await bcrypt.genSalt(setRounds)
const passwordHashed = await bcrypt.hash(password, salt)
// console.log('passwordHashed ->', passwordHashed)
reply.send(passwordHashed)
})
In your Dockerfile run this:
FROM node:12-alpine
WORKDIR /app
#Entry Point
COPY ./package.json ./
COPY . .
#Rebuild bcrypt After bpm insatll
RUN apk add --no-cache make gcc g++ python && \
npm install && \
npm rebuild bcrypt --build-from-source && \
apk del make gcc g++ python
EXPOSE 4000
CMD ["npm","start"]
Example code :
https://github.com/thana19/node-bcrypt
docker build . -t nodebcrypt
docker run -dp 4000:4000 nodebcrypt
curl http://localhost:4000/bcrypt/:password
E.g.
curl http://localhost:4000/bcrypt/thana
$2b$05$jbQw1.o07kzyZi1/Jh/1pOouHMmtSAYtnvhV.EaQtOGhV5RdtukKa%
ref: https://www.richardkotze.com/top-tips/install-bcrypt-docker-image-exclude-host-node-modules
(Visited 1,046 times, 1 visits today)