【发布时间】:2018-09-30 10:30:30
【问题描述】:
我有一个简单的Hapi.js Node API。由于我使用 TypeScript 编写 API,因此我编写了 Gulp 任务来转译代码。如果我直接在我的主机上运行它,我的 API 可以正常工作,但是当我尝试在 Docker 中运行它时出现以下错误:
Docker 撰写命令:
docker-compose -f docker-compose.dev.yml up -d --build
这是我的代码: ./gulpfile:
'use strict';
const gulp = require('gulp');
const rimraf = require('gulp-rimraf');
const tslint = require('gulp-tslint');
const mocha = require('gulp-mocha');
const shell = require('gulp-shell');
const env = require('gulp-env');
/**
* Remove build directory.
*/
gulp.task('clean', function () {
return gulp.src(outDir, { read: false })
.pipe(rimraf());
});
/**
* Lint all custom TypeScript files.
*/
gulp.task('tslint', () => {
return gulp.src('src/**/*.ts')
.pipe(tslint({
formatter: 'prose'
}))
.pipe(tslint.report());
});
/**
* Compile TypeScript.
*/
function compileTS(args, cb) {
return exec(tscCmd + args, (err, stdout, stderr) => {
console.log(stdout);
if (stderr) {
console.log(stderr);
}
cb(err);
});
}
gulp.task('compile', shell.task([
'npm run tsc',
]))
/**
* Watch for changes in TypeScript
*/
gulp.task('watch', shell.task([
'npm run tsc-watch',
]))
/**
* Copy config files
*/
gulp.task('configs', (cb) => {
return gulp.src("src/configurations/*.json")
.pipe(gulp.dest('./build/src/configurations'));
});
/**
* Build the project.
*/
gulp.task('build', ['tslint', 'compile', 'configs'], () => {
console.log('Building the project ...');
});
/**
* Run tests.
*/
gulp.task('test', ['build'], (cb) => {
const envs = env.set({
NODE_ENV: 'test'
});
gulp.src(['build/test/**/*.js'])
.pipe(envs)
.pipe(mocha({ exit: true }))
.once('error', (error) => {
console.log(error);
process.exit(1);
});
});
gulp.task('default', ['build']);
./.docker/dev.dockerfile:
FROM node:latest
LABEL author="Saurabh Palatkar"
# create a specific user to run this container
# RUN adduser -S -D user-app
# add files to container
ADD . /app
# specify the working directory
WORKDIR app
RUN chmod -R 777 .
RUN npm i gulp --g
# build process
RUN npm install
# RUN ln -s /usr/bin/nodejs /usr/bin/node
RUN npm run build
# RUN npm prune --production
EXPOSE 8080
# run application
CMD ["npm", "start"]
./docker-compose.dev.yml:
version: "3.4"
services:
api:
image: node-api
build:
context: .
dockerfile: .docker/dev.dockerfile
environment:
PORT: 8080
MONGO_URL: mongodb:27017
NODE_ENV: development
ports:
- "8080:8080"
links:
- database
database:
image: mongo:latest
ports:
- "27017:27017"
我在这里缺少什么?
【问题讨论】:
-
这是一个疯狂的猜测,但是如果你使用
gulp-typescript而不是使用gulp-shell执行命令来调用 TypeScript 编译器呢? -
你没有一些涉及到
npm生命周期的shell脚本吗?它没有找到node二进制文件,所以似乎/node/bin/node不存在 -
我认为你需要将你的 RUN npm i gulp --g 切换为 just -g 或 --global 你希望它在容器上全局安装。
-
嗯,愿意分享
package.json或至少在scripts->build键值对下? --edit:从头开始,我看到它是gulp build -
也许你没有在容器中安装 gulp?或者
npm install gulp-cli -g你有npm ... --g,两个——而不是一个。
标签: javascript node.js docker gulp docker-compose