Better Software Development in Team with JavaScript Part 2 with Docker

in #javascript7 years ago (edited)

For a better software development and deployment with JavaScript, I am using philosophy in The Twelve-Factor App. In this blogs, I will talk about the another 3 rules of 12 factors.

All Parts

Screen Shot 2017-12-23 at 11.01.35 PM.png

Rule #4: Backing Services

Treat backing services as attached resources

using a simple express app. Using express-http-proxy packages.

const path = require('path);
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();

const baseImageUrl = process.env.BASE_IMAGE_URL;
const proxyBaseImageUrl = baseImageUrl 
    ? proxy(baseImageUrl, {
        proxyReqPathResolver: (req) => {
            const newPath = baseImageUrl + req.path;
            console.log(newPath);
            return newPath;
        }
    })

app.use('/images', )
app.listen(4000);

Then add BASE_IMAGE_URL into your environment. This I will normally set to my AWS S3 Bucket.

Rule #5: Build, release, run

Strictly separate build and run stages

To achieve this rules I am using Docker.
Whale Logo332_5.png

Create a Dockerfile

FROM node:9.3.0-alpine
WORKDIR /srv
COPY . .
RUN yarn install --production
CMD [ "node", "index.js" ]

What this Dockerfile did is that it pull images from Docker Hub, and then set up the docker in your local machine.

Install it with docker build -t vendor/name:1.0 where vendor is vendor name, name is the name of the docker, and 1.0 is the version.

In docker-compose.yml file:

version: '3'
services:
  app:
    image: vendor/name:1.0
    env_file:
      - .env
    ports:
      - 8080:8080

This file used compose file version 3. This yml stated that the environment file name is .env and the port will be 8080.

Start docker compose with docker-compose up -d app

in .gitignore and .dockerignore add :

.env
node_modules/

Rule #6: Processes

Execute the app as one or more stateless processes

Docker containers are designed to be stateless. We do not want to lose data when our container is shut off.

In docker-compose.yml, create a new volume:

version: '3'
services:
  app:
    image: vendor/name:1.0
    env_file:
      - .env
    ports:
      - 8080:8080
    volumes:
      - appdata:/srv/uploads
volumes:
    appdata:

About Me

I am Lai Weng Han (Johnson), you can find me on Twitter.

Sort:  
Qurator
Your Quality Content Curator
This post has been upvoted and given the stamp of authenticity by @qurator. To join the quality content creators and receive daily upvotes click here for more info.

Qurator's exclusive support bot is now live. For more info click HERE or send some SBD and your link to @qustodian to get even more support.

The @OriginalWorks bot has determined this post by @superoo7 to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 64176.22
ETH 2624.19
USDT 1.00
SBD 2.78