Better Software Development in Team with JavaScript Part 2 with Docker
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
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.
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.
@originalworks
The @OriginalWorks bot has determined this post by @superoo7 to be original material and upvoted it!
To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!