mkdir nodejs
cd nodejs
npm init -y // 初始化项目
npm i koa -S // 安装KOA
项目中添加APP.js文件
const Koa = require('koa')
const app = new Koa()
app.use(ctx => {
ctx.body = "Hello nodejs in Docker"
})
app.listen(3000, () => {
console.log('app started at port 3000')
})
添加Dockerfile文件
FROM node:10-alpine
ADD . /app/
WORKDIR /app
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]
构建和启动
docker build -t mynode .
docker run -p 3000:3000 mynode