Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27724a4

Browse files
committed
Add Dockerfile.dev that installs Yarn
- Add Dockerfile.dev that installs Yarn, this helps with startup time - Ensure that Node.js modules are installed during the initial launch (docker-compose up) - Remove named volume for NPM cache
1 parent fee37e3 commit 27724a4

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
build
55
node_modules
66
npm-debug.log
7+
yarn-error.log
78
.env

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
FROM node:7.3.0-alpine
22

3-
# Copy application files
4-
COPY ./build /usr/src/app
3+
# Set a working directory
54
WORKDIR /usr/src/app
65

7-
# Install Node.js dependencies
8-
RUN npm install -g yarn --no-progress --silent \
6+
# Copy application files
7+
COPY build .
8+
9+
# Install Yarn and Node.js dependencies
10+
RUN npm install yarn --global --no-progress --silent --depth 0 \
911
&& yarn install --production --no-progress
1012

1113
CMD [ "node", "server.js" ]

Dockerfile.dev

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM node:7.3.0-alpine
2+
RUN npm install yarn --global --no-progress --silent --depth 0

docker-compose.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ version: '2'
33
services:
44

55
api:
6-
image: node:7.3.0-alpine
6+
build:
7+
context: .
8+
dockerfile: ./Dockerfile.dev
79
restart: always
810
env_file: .env
9-
ports:
10-
- $PORT:$PORT
11-
- '127.0.0.1:9229:9229' # V8 inspector
1211
links:
1312
- postgres
1413
- redis
14+
ports:
15+
- $PORT:$PORT
16+
- '127.0.0.1:9229:9229' # V8 inspector
1517
volumes:
16-
- api-yarn-cache:/root/.cache/yarn
17-
- api-npm-cache:/root/.npm
18+
- yarn:/root/.cache/yarn
1819
- ./:/usr/src/app
1920
working_dir: /usr/src/app
2021
command: node scripts/run.js
@@ -27,18 +28,17 @@ services:
2728
- /run/postgresql
2829
- /tmp
2930
volumes:
30-
- postgres-data:/var/lib/postgresql/data
31+
- postgres:/var/lib/postgresql/data
3132
- ./scripts/postgres-init.sh:/docker-entrypoint-initdb.d/init.sh
3233

3334
redis:
3435
image: redis:3.2.6-alpine
3536
restart: always
3637
read_only: true
3738
volumes:
38-
- redis-data:/data
39+
- redis:/data
3940

4041
volumes:
41-
api-yarn-cache:
42-
api-npm-cache:
43-
postgres-data:
44-
redis-data:
42+
yarn:
43+
postgres:
44+
redis:

scripts/publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/sh -e
22

33
# Read application name from 'package.json'
44
SERVICE_NAME=$(cat package.json \

scripts/run.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
*/
99

1010
const fs = require('fs');
11+
const path = require('path');
1112
const cp = require('child_process');
13+
const task = require('./task');
1214

13-
let task;
1415
let build;
1516
let server;
1617

@@ -36,18 +37,22 @@ process.on('SIGTERM', () => process.emit('cleanup'));
3637
// Ensure that Node.js modules were installed,
3738
// at least those required to build and launch the app
3839
try {
39-
task = require('./task');
4040
build = require('./build');
4141
} catch (err) {
4242
if (err.code !== 'MODULE_NOT_FOUND') throw err;
43-
console.log('Installing Node.js modules...');
44-
// Install Yarn if it's missing. TODO: Use a Node.js image with Yarn pre-installed.
45-
if (!fs.existsSync('/usr/local/bin/yarn')) {
46-
cp.spawnSync('npm', ['install', 'yarn', '-g', '--silent'], { stdio: ['ignore', 'ignore', 'inherit'] });
47-
}
4843
// Install Node.js modules with Yarn
4944
cp.spawnSync('yarn', ['install', '--no-progress'], { stdio: 'inherit' });
50-
process.exit(0);
45+
46+
// Clear Module's internal cache
47+
try {
48+
const Module = require('module');
49+
const m = new Module();
50+
// eslint-disable-next-line
51+
m._compile(fs.readFileSync('./scripts/build.js', 'utf8'), path.resolve('./scripts/build.js'));
52+
} catch (error) { } // eslint-disable-line
53+
54+
// Reload dependencies
55+
build = require('./build');
5156
}
5257

5358
module.exports = task('run', () => Promise.resolve()

0 commit comments

Comments
 (0)