Getting "The serve command requires to be run in an Angular project, but a project definition could not be found" running Docker Compose

Viewed 2046

Firstly, I inform you that i'm currently student in software development. So I beg for your tolerance regards to my question :)

I have a problem trying to up my docker-compose services. It is 3 services docker-compose.yml: Java Backend, Angular 7 Frontend, Postgres DB.


[EDIT]: the solution has been found by Wassim. I had to change volumes in the service ct-front FROM:

volumes:
    - '.:/app'

TO

volumes:
    - 'contacts-front:/app'

THEN add contacts-front: to my volumes declaration.

volumes:
    db_data:
    contacts-front:

The Back and the DB run fine. But the Angular app doesn't work. I get an error pretty famous:

cont-ct-front | The serve command requires to be run in an Angular project, but a project definition could not be found.

If I run "ng v" in my Angular app, I get the following output:

Angular CLI: 7.3.7
Node: 12.9.0
OS: win32 x64
Angular: 7.2.11
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.7
@angular-devkit/build-angular     0.13.7
@angular-devkit/build-optimizer   0.13.7
@angular-devkit/build-webpack     0.13.7
@angular-devkit/core              7.3.7
@angular-devkit/schematics        7.3.7
@angular/cdk                      7.3.6
@angular/cli                      7.3.7
@angular/material                 7.3.6
@ngtools/webpack                  7.3.7
@schematics/angular               7.3.7
@schematics/update                0.13.7
rxjs                              6.3.3
typescript                        3.2.4
webpack                           4.29.0

Of course, I tried the solution I found on google which is to run

ng update @angular/cli --migrate-only --from=<angular-cli version>

But The error stays.

Here are my docker config files.

docker-compose.yml

version: '3.7'

services:
    ct-db:
        build:
            context: .
            dockerfile: Dockerfile-db
        container_name: cont-ct-db
        restart: unless-stopped
        ports:
            - 5432:5432
        expose:
            - 5432
        environment:
            POSTGRES_USER: admin_ct
            POSTGRES_PASSWORD: 123
            POSTGRES_DB: db_contacts
        volumes:
          - db_data:/var/lib/postgres/data
        networks:
            - ct-network

    ct-back:
        depends_on:
            - ct-db
        build:
            context: .
            dockerfile: Dockerfile-back
        container_name: cont-ct-back
        ports:
            - 8080:8080
        expose:
            - 8080
        environment:
            SPRING_DATASOURCE_USERNAME: admin_ct
            SPRING_DATASOURCE_PASSWORD: 123
            SPRING_DATASOURCE_URL: jdbc:postgresql://ct-db:5432/db_contacts
        networks:
            - ct-network

    ct-front:
        build: 
            context: .
            dockerfile: contacts-front/Dockerfile-front
        container_name: cont-ct-front
        volumes:
            - '.:/app'
            - '/app/node_modules'
        ports:
            - 4200:4200
        networks:
            - ct-network

networks:
    ct-network:

volumes:
    db_data:

Dockerfile-back

FROM openjdk:8
ADD contacts-back/contacts-back.jar contacts-back.jar
ENTRYPOINT ["java", "-jar", "contacts-back.jar"]

Dockerfile-front

# base image
FROM node:latest

# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY contacts-front/package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli@7.3.7

# add app
COPY contacts-front/. /app

# start app
CMD ng serve --host 0.0.0.0

Dockerfile-db

FROM postgres:latest
COPY db/db_data.sql /docker-entrypoint-initdb.d/init.sql
COPY db/db_insert.sql /opt/db_insert.sql
COPY sh/backup-db.sh /opt/backup-db.sh
RUN apt-get update \
&& apt-get install vim -y \
&& export EDITOR=/usr/bin/vim \
&& chmod +x /opt/backup-db.sh \

My directory tree is:

contacts-back       --> directory containing the JAR file
contacts-front      --> directory containing the Angular application
db                  --> directory containing the SQL scripts
sh                  --> directory containing the SH scripts
docker-compose.yml
Dockerfile-back 
Dockerfile-db

Important point: the Angular app works fine if I put all docker config files and scripts in the Angular app directory (contacts-front). But this would be really ugly, right?

Does anyone sees what I'm doing wrong?

As usual, thanks a lot for your help! :)

2 Answers

I think that the problem is with the host path mentioned in the "ct-front" volume:

volumes:
        - 'contacts-front:/app'

It seems that your angular.json package.json ,etc files are not there if exists but still facing same issue then the ng serve command from the proper directory of the angular application. It should be excited from the path where all the files are present of angular project creations.

Related