joephon / blog

极简博客
3 stars 2 forks source link

How to run your NodeJS app via docker #13

Open joephon opened 4 years ago

joephon commented 4 years ago

Dependencies

Step 1 write your NodeJS code

mkdir node && cd node && vim app.js
// node/app.js

const app = require('http')
const PORT = 3000

app.createServer((req, res) => {
  res.end('Hello World!')
})

app.listen(PORT, () => console.log(`Server is running  on port: ${PORT}`))

Step 2 write your Dockerfile

vim Dockerfile
// node/Dockerfile

FROM node:latest
COPY . /app
WORKDIRE /app
EXPOSE 3000
CMD node ./app.js

Step 3 build your docker file

docker build . 

Step 4 run your app

docker run -p 3000:3000 -d [your docker image id]

Bingo~

curl -get localhost:3000
// => Hello World!

这篇文章价值一块钱