# Build Dockerfile

* Dockerfile for nodejs app (Size: 250MB)

  ```sh
  FROM node:12-alpine
  
  # Create app directory
  WORKDIR /app
  
  # Install app dependencies
  # A wildcard is used to ensure both package.json AND package-lock.json are copied
  # where available (npm@5+)
  COPY package*.json ./
  
  RUN npm install --only=production
  # If you are building your code for production
  # RUN npm ci --only=production
  
  # Bundle app source
  COPY . .
  
  EXPOSE 5009
  CMD [ "node", "server.js" ]
  ```

  

* use multi-stage (Size: 195MB)

  ```sh
  # in comparison with node:12-alpine image build, this will reduce by further 50MB
  # node:12-alpine : 257MB
  # multistage alpine:3.12 : 195MB
  #
  # ------base node------
  FROM alpine:3.12 AS base
  # install node
  RUN apk add --no-cache nodejs nodejs-npm 
  # Create app directory
  WORKDIR /app
  
  # Install app dependencies
  # A wildcard is used to ensure both package.json AND package-lock.json are copied
  # where available (npm@5+)
  COPY package*.json ./
  
  # ------dependencies-------
  FROM base AS dependencies
  RUN npm install --only=production
  # If you are building your code for production
  # RUN npm ci --only=production
  # copy production node_modules aside
  RUN cp -R node_modules prod_node_modules
  
  # -----test------
  # add npm run test if needed
  
  # -----release--------
  FROM base AS release
  # copy production node_modules
  COPY --from=dependencies /app/prod_node_modules ./node_modules
  # Bundle app source
  COPY . .
  
  EXPOSE 5009
  CMD [ "node", "server.js" ]
  
  ```

  

* build

  ```sh
  $ docker build [-f Dockerfile] . -t razerpay/visa-backend-api
  
  // push image
  $ docker push razerpay/visa-backend-api
  
  ```

  

* run

  ```sh
  $ docker run razerpay/visa-backend-api
  
  // enter shell
  $ docker exec -it <container-name> sh
  ```

  

* docker-compose

  ```yaml
  version: '2'
  services:
    api: 
      image: razerpay/visa-backend-api-multi:latest
      #build: .
      #command: npm server.js
      volumes:
        - ./runconfig/default.json:/app/config/default.json
        - ./runconfig/ca.crt:/app/config/ca.crt
        - ./runconfig/root.crt:/app/config/root.crt
      ports:
        - "5009:5009"
      #depends_on:
      #  - postgres
      #environment:
      #  DATABASE_URL: postgres://todoapp@postgres/todos
    #postgres:
    #  image: postgres:9.6.2-alpine
    #  environment:
    #    POSTGRES_USER: todoapp
    #    POSTGRES_DB: todos
  ```

* run docker-compose

  ```sh
  $ docker-compose up -d
  ```

  

