Skip to content

Commit

Permalink
feat(docker-build): added ability to override dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
natusvince committed Dec 17, 2018
1 parent 0b9bdc8 commit 76a3a3a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ if (module.hot) {
try {
await currentServer.stop();

currentServer = server; // импорт из сервера заменется самостоятельно
currentServer = server; // импорт из сервера заменится самостоятельно
await startServer();
} catch (error) {
console.log('Failed to update server. You probably need to restart application', error);
Expand Down Expand Up @@ -255,6 +255,8 @@ docker run -p 8080:8080 container-name:version ./start.sh

На `8080` порту будет поднят nginx, который будет раздавать статику и проксировать все остальные запросы к `nodejs`.

Вы также можете переопределить полностью процесс сборки docker-образа, создав в корневой директории проекта `Dockerfile` содержащий необходимый набор инструкций. Пример [Dockerfile](https://github.com/alfa-laboratory/arui-scripts/blob/master/commands/docker-build/dockerfile.template.js).

archive
---

Expand Down Expand Up @@ -303,7 +305,7 @@ yarn будет использоваться когда в рутовой пап
}
```

По умолчанию TS будет компилироваться через babel, но у этого естьряд ограничений:
По умолчанию TS будет компилироваться через babel, но у этого есть ряд ограничений:
- нельзя использовать namespace
- Нельзя использовать устаревший синтаксис import/export (`import foo = require(...)`, `export = foo`)
- enum merging
Expand Down
18 changes: 11 additions & 7 deletions commands/docker-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const path = require('path');
const shell = require('shelljs');
const fs = require('fs-extra');
const configs = require('../../configs/app-configs');
const dockerfileTemplate = require('./dockerfile.template');
const nginxConfTemplate = require('./nginx.conf.template');
const startScript = require('./start.template');
const dockerfile = require('./dockerfile.template');
const exec = require('../util/exec');

let imageVersion = configs.version;
Expand Down Expand Up @@ -34,7 +34,7 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}

(async () => {
try {
console.log(`Build container ${imageFullName}`);
console.log(`Build docker image ${imageFullName}`);
console.time('Total time');
// create tmp directory for docker related files
// We need to copy it because we will remove this directory during build process
Expand All @@ -45,6 +45,10 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}
? await fs.readFile(configs.localNginxConf, 'utf8')
: nginxConfTemplate;

const dockerfile = configs.localDockerfile
? await fs.readFile(configs.localDockerfile, 'utf8')
: dockerfileTemplate;

await Promise.all([
fs.writeFile(path.join(pathToTempDir, 'Dockerfile'), dockerfile, 'utf8'),
fs.writeFile(path.join(pathToTempDir, 'nginx.conf'), nginxConf, 'utf8'),
Expand All @@ -58,22 +62,22 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}

console.timeEnd('Build application time');

console.time('Remove build dependencies time');
// if yarn is available prunde dev dependencies with yarn, otherwise use npm
console.time('Remove dev dependencies time');
// if yarn is available prune dev dependencies with yarn, otherwise use npm
if (configs.useYarn && shell.which('yarn')) {
await exec('yarn install --production --ignore-optional --frozen-lockfile --ignore-scripts --prefer-offline');
} else {
await exec('npm prune --production');
}

console.timeEnd('Remove build dependencies time');
console.timeEnd('Remove dev dependencies time');

console.time('Build container time');
console.time('Build docker image time');
await exec(`docker build -f "./${tempDirName}/Dockerfile" \\
--build-arg START_SH_LOCATION="./${tempDirName}/start.sh" \\
--build-arg NGINX_CONF_LOCATION="./${tempDirName}/nginx.conf" -t ${imageFullName} .`);

console.timeEnd('Build container time');
console.timeEnd('Build docker image time');

// remove temp directory
await fs.remove(pathToTempDir);
Expand Down
2 changes: 2 additions & 0 deletions configs/app-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const projectTsConfigPath = path.join(CWD, 'tsconfig.json');
const yarnLockFilePath = path.join(CWD, 'yarn.lock');
const overridesPath = path.join(CWD, 'arui-scripts.overrides.js');
const nginxConfFilePath = path.join(CWD, 'nginx.conf');
const dockerfileFilePath = path.join(CWD, 'Dockerfile');

let aruiPolyfills = null;
try {
Expand Down Expand Up @@ -53,6 +54,7 @@ module.exports = {
// compilation configs locations
tsconfig: fs.existsSync(projectTsConfigPath) ? projectTsConfigPath : null,
localNginxConf: fs.existsSync(nginxConfFilePath) ? nginxConfFilePath : null,
localDockerfile: fs.existsSync(dockerfileFilePath) ? dockerfileFilePath : null,

useTscLoader: packageSettings.useTscLoader || false,
useServerHMR: typeof packageSettings.useServerHMR !== 'undefined' ? !!packageSettings.useServerHMR : false,
Expand Down
2 changes: 1 addition & 1 deletion configs/util/apply-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function applyOverrides(overridesKey, config) {
overridesKey.forEach(key => {
if (overrides.hasOwnProperty(key)) {
if (typeof overrides[key] !== 'function') {
console.error('Incorrect override', key);
console.error(`Override ${key} must be a function`);
}
config = overrides[key](config);
}
Expand Down
4 changes: 1 addition & 3 deletions configs/webpack.client.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const applyOverrides = require('./util/apply-overrides');

const noopPath = require.resolve('./util/noop');

// This is the development configuration.
// It is focused on developer experience and fast rebuilds.
// The production configuration is different and lives in a separate file.
// This is the production configuration.
module.exports = applyOverrides(['webpack', 'webpackClient', 'webpackProd', 'webpackClientProd'], {
mode: 'production',
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
Expand Down

0 comments on commit 76a3a3a

Please sign in to comment.