Docker: Cara Package Application NodeJS

    Fail yang diperlukan:

    - Dockerfile
    - server.js
    - package.json
    - docker-compose.yml

    Copy code dibawah ini pada fail Dockerfile

    FROM node:13-alpine
    
    WORKDIR /app
    
    COPY package.json package-lock.json ./
    
    RUN npm install
    
    COPY . .
    
    EXPOSE 3001
    
    CMD npm run start.dev

    Copy code dibawah ini pada fail server.js

    'use strict'
    
    const express = require('express')
    
    const { PORT = '3001' } = process.env
    const app = express()
    
    app.use((req, res, next) => {
      res.send('Hello guys')
    })
    
    app.listen(PORT)

    Copy code dibawah ini pada fail package.json

    {
      "main": "index.js",
      "scripts": {
        "start": "node index",
        "start.dev": "nodemon"
      },
      "dependencies": {
        "express": "^4.16.2"
      },
      "devDependencies": {
        "nodemon": "^1.14.12"
      }
    }
    

    Kemudian build docker image

    $ docker build -t nodeapp:v1 .

    Jika anda mahu menggunakan Docker Compose:

    version: '3'
    services:
      app:
        image: nodeapp:v1
        volumes:
          - .:/app/
        ports:
          - "3001:3001"

    Copyright © 2020