Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'sh: tsc not found' error in docker container when using Gitlab CI docker-in-docker

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   | > [email protected] 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! [email protected] build: `tsc`
backend_1   | npm ERR! spawn ENOENT
backend_1   | npm ERR! 
backend_1   | npm ERR! Failed at the [email protected] 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.

like image 612
Kevin Damstra Avatar asked Aug 02 '19 05:08

Kevin Damstra


People also ask

Does docker-compose work without GitLab CI?

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:

How to fix the ‘TSC command not found’ error when compiling typescript?

To fix the ‘tsc command not found’ error when compiling TypeScript, we can run tsc without installing it with npx. in our project folder. npx runs tsc directly from the online package source without installing the package. To fix the ‘tsc command not found’ error when compiling TypeScript, we can run tsc without installing it with npx.

What are the common issues with Docker containers?

We just covered the most common issues you may encounter when working with Docker containers, from building images to deploying a network of containers. Docker has a --debug flag which is intended mainly for Docker developers. However, if want to know more about Docker internals, try running Docker commands in debug mode for more verbose output:

How to install Docker on a local machine?

You can visit the Docker web site or follow the official installation documentation to install Docker on your local machine. The most common place you may run into issues is when you’re building your Docker image from a Dockerfile. Before we dive in, let’s clarify the difference between images and containers.


1 Answers

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.

like image 159
Kevin Damstra Avatar answered Oct 25 '22 10:10

Kevin Damstra