9

I'm setting up Gitlab CI docker-in-docker for a project. Unfortunately the job keeps failing because installed NPM packages can't seem to be found when running commands. The error I'm getting:

backend_1   | 
backend_1   | > tacta-backend@0.0.1 build /app
backend_1   | > tsc
backend_1   | 
backend_1   | sh: tsc: not found
backend_1   | npm ERR! file sh
backend_1   | npm ERR! code ELIFECYCLE
backend_1   | npm ERR! errno ENOENT
backend_1   | npm ERR! syscall spawn
backend_1   | npm ERR! tacta-backend@0.0.1 build: `tsc`
backend_1   | npm ERR! spawn ENOENT
backend_1   | npm ERR! 
backend_1   | npm ERR! Failed at the tacta-backend@0.0.1 build script.
backend_1   | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
backend_1   | 
backend_1   | npm ERR! A complete log of this run can be found in:
backend_1   | npm ERR!     /root/.npm/_logs/2019-08-02T04_46_04_881Z-debug.log

The curious thing is that it does work when I run docker-compose manually without using the Gitlab CI. This is what my .gitlab-ci.yml looks like:

build:
  variables:
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  image: docker:18
  stage: build
  services:
    - docker:18-dind
  before_script:
    - docker info
    - apk add python-dev libffi-dev openssl-dev gcc libc-dev make
    - apk add py-pip
    - pip install docker-compose
  script:
    - docker-compose -f docker-compose.yml -f docker-compose.prod.yml up

This is my docker-compose.yml:

version: '3'
services:
  frontend:
    build:
      context: ./frontend
      args:
        NODE_ENV: production
        PGUSER: ${PGUSER}
        PGHOST: ${PGHOST}
        PGPASSWORD: ${PGPASSWORD}
        PGDATABASE: ${PGDATABASE}
        PGPORT: ${PGPORT}
        DATABASE_URL: ${DATABASE_URL}
    command: npm run build
    ports:
      - "9000:9000"
    volumes:
      - /app/node_modules
      - ./frontend:/app
  backend:
    build:
      context: ./backend
      args:
        NODE_ENV: production
    command: npm run build
    ports:
      - "3000:3000"
    volumes:
      - /app/node_modules
      - ./backend:/app

And this is the Dockerfile:

FROM node:11.10.1-alpine
ARG NODE_ENV
ARG PGUSER
ARG PGHOST
ARG PGPASSWORD
ARG PGDATABASE
ARG PGPORT
ARG DATABASE_URL
ENV NODE_ENV ${NODE_ENV}
ENV PGUSER ${PGUSER}
ENV PGHOST ${PGHOST}
ENV PGPASSWORD ${PGPASSWORD}
ENV PGDATABASE ${PGDATABASE}
ENV PGPORT ${PGPORT}
ENV DATABASE_URL ${DATABASE_URL}
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY ./ ./

I expect the installed packages and their commands to be available in the docker container. At some point they worked, and I have no clue what changed in the configuration to cause this issue.

I am not expecting a copy/paste solution from you guys, but I do hope you can point me in the right direction to properly get to the root of this issue.

Kevin Damstra
  • 491
  • 1
  • 4
  • 5
  • https://stackoverflow.com/questions/39404922/tsc-command-not-found-in-compiling-typescript – LinPy Aug 02 '19 at 05:17
  • @LinPy thanks, I've read dozens of those threads but they do not seem to solve the problem I have. As mentioned: it does work when running docker-compose, just not when I execute it through a Gitlab CI runner. – Kevin Damstra Aug 02 '19 at 06:34
  • Your first step in debugging this should be to delete the `volumes:` in the `docker-compose.yml`. They hide everything in the image and replace it with content you have on your local system (including a copy of `node_modules` that will never get updated) and so it's very likely your "it builds fine in my local environment" setup is different from what's built by the `Dockerfile`. – David Maze Aug 02 '19 at 11:01
  • @DavidMaze it works using docker-compose on my local machine, which still uses the volume setup. Anyhow I've given it a try without the volume setup and the issue persists. – Kevin Damstra Aug 03 '19 at 10:00

1 Answers1

40

The problem was that I switched from NODE_ENV: development to NODE_ENV: production. With production enabled devDependencies in my package.json were no longer being installed (duh me).

I added typescript and webpack to the regular dependencies and now it works like a charm again.

Kevin Damstra
  • 491
  • 1
  • 4
  • 5