简体   繁体   English

Gulp构建任务在Docker内部失败

[英]Gulp build task failing inside docker

I have a simple Hapi.js Node API . 我有一个简单的Hapi.js Node API Since I have used TypeScript to write the API, I wrote Gulp task for transpiling the code. 由于我使用TypeScript编写API,因此我编写了Gulp任务来编译代码。 My API works fine if I run it directly in my main machine but I get the following error when I try to run it inside Docker: 如果直接在我的主机上运行我的API可以正常工作,但是当我尝试在Docker中运行它时出现以下错误:

Error: 错误: 在此处输入图片说明

Docker compose command: Docker撰写命令:

docker-compose -f docker-compose.dev.yml up -d --build

Here is my code: ./gulpfile: 这是我的代码: ./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: ./.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: ./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"

What I am missing here? 我在这里想念的是什么?

Edit: The problem wasn't what I initially thought. 编辑:问题不是我最初的想法。 The order of the operations in the Dockerfile was simply wrong: you have to install the dependencies first, then copy all the files into the container (so the installed dependencies will also be copied), only then you can use the application and its dependencies. Dockerfile中操作的顺序是完全错误的:您必须先安装依赖项,然后将所有文件复制到容器中(这样也将复制已安装的依赖项),然后才能使用应用程序及其依赖项。 I made a pull request on your repo with those fixes :) 我通过这些修复程序对您的回购请求了一个请求:)

不要使用最新的节点,将docker文件配置为您在DEV环境中使用的确切节点版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM