joephon / blog

极简博客
3 stars 2 forks source link

How to run your Koa app via docker #14

Open joephon opened 4 years ago

joephon commented 4 years ago

Dependencies

Step 1 init project and write your koa code

mkdir koa && cd koa && vim app.js
npm i --save koa
// koa/app.js

const Koa = require('koa')
const app = new Koa()
const PORT = 3000

app.use((ctx) => {
  ctx.body = 'Hello World!'
})

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

Step 2 write your Dockerfile

vim Dockerfile
// koa/Dockerfile

FROM node:latest
COPY . /app
WORKDIRE /app
RUN npm i
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!

这篇文章价值一块钱