Setting up `ng serve` to detect changes in a containerized Angular 2 application

Currently, I am 'dockerizing' an existing Angular 2 application that is being run on angular-cli version 1.0.0-beta.31.

I am facing difficulties in configuring ng serve to automatically reload my app whenever a file in the working directory is updated. As of now, I have to manually run docker-compose up --build every time I make changes...

UPDATE: I am considering using a volume for this purpose.

The following is my Dockerfile:

# Dockerizing Angular 2 Client App
# @link: https://scotch.io/tutorials/create-a-mean-app-with-angular-2-and-docker-compose

# Using official Node 7 dockerhub image as base
FROM node:7

# Creating a directory for the app
RUN mkdir -p /usr/src/app

# Changing directory to execute commands within it
WORKDIR /usr/src/app

# Copying dependency definitions
COPY package.json /usr/src/app

# Installing dependencies
RUN npm install

# Commented out code to set-up a volume
# COPY . /usr/src/app

# Exposing the port on which the app runs
EXPOSE 4200

# Running the app
CMD ["npm", "start"]

Below is my docker-compose.yml configuration:

# Specifying docker-compose version
version: '2'

# Defining services/containers to be executed
services:
  angular2app:
    build: ./
    ports:
      - "4200:4200"
    # Adding volume here
    volumes:
      - .:/usr/src/app

UPDATE: The app successfully builds and runs without the volume. However, when the volume is added, the following error is encountered:

Building angular2app
Step 1/7 : FROM node:7
 ---> b3a95b94bd6c
Step 2/7 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 2afb01ffe055
Step 3/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 44d08fdb4a19
Step 4/7 : COPY package.json /usr/src/app
 ---> Using cache
 ---> 87bb4f71c13c
Step 5/7 : RUN npm install
 ---> Using cache
 ---> ba88a0e1e480
Step 6/7 : EXPOSE 4200
 ---> Using cache
 ---> 4fddacae8486
Step 7/7 : CMD npm start
 ---> Using cache
 ---> c5ac29bf85fc
Successfully built c5ac29bf85fc
Creating minterface_angular2app_1

ERROR: for angular2app  Cannot start service angular2app: Mounts denied: closed
ERROR: Encountered errors while bringing up the project.

How can I configure ng serve to detect changes in my current working directory and rebuild my Angular 2 app?

PS: I am using Docker for Mac with docker-compose version 1.11.1 on Mac running Sierra 10.12.3.

Answer №1

Dealing with a comparable problem was challenging for me. My issue stemmed from Webpack not properly monitoring changes in the codebase. For further information, refer to https://webpack.js.org/configuration/watch/

To address this issue within webpack.common.js, consider incorporating the following snippet:

watchOptions: {
   poll: 1000, // denotes a polling interval measured in milliseconds
},

Answer №2

If you're looking to manage node processes efficiently, consider using the supervisor npm package available at this link. By simply running the process with this supervisor tool and specifying the folders to watch for changes with the -w flag, you can ensure smooth operation.

I trust this solution will serve your needs effectively!

Answer №3

We have a unique approach to solving this problem in our development workflow that may differ from what you're expecting:

  1. Avoid running ng serve in docker. Instead, run it on your host machine to prevent issues with file sharing in Docker. There are known problems with file changes on the host not propagating to the Docker VM.

  2. Implement a reverse proxy in your docker-compose setup to route requests to both your backend and Angular project. You probably already have this configured.

    web:
      image: nginx
      ports:
        - "80:80"
      links:
        - backend
      extra_hosts:
        - "frontend:192.168.99.1"
    

    Instead of linking to frontend, we specify extra_hosts to direct to our host's IP address.

  3. Add the host's IP address to the lo0 interface so that it can be accessed from inside the Docker VM.

    sudo ifconfig lo0 inet 192.168.99.1/32 add
    

    Remember that this setting does not persist after a restart, so you'll need to do this again as needed.

Be sure to select a suitable IP address to prevent conflicts with your local network or any VPN software you might be using.

We hope these suggestions prove useful for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you retrieve data in Angular through an API call using the get method?

How can I make an API call to retrieve data from this URL, , using a GET method without passing any parameters in my Angular application? What is the code snippet required for this, and do I need to import any specific libraries or declare specific variabl ...

What are the steps to utilize kendo-fileselect in order to upload files from an Angular application to a C# web API?

We are integrating Kendo for Angular into our current project. In our existing system, we utilize kendo-upload which triggers a server call immediately. However, we cannot follow the same approach for this particular page. https://i.stack.imgur.com/qdn2b. ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

Guide on displaying tooltip messages for a custom directive in Visual Studio Code

I have developed a custom directive called app-subscriber. When I hover the mouse over it, I want to display a tooltip message saying "This is for subscribers and requires an email address". Is this achievable? Do I need to create a new VS Code extension f ...

Converting milliseconds into days, hours, minutes, and seconds using Angular

Currently, I am attempting to convert milliseconds to the format dd:hh:mm:ss. For example, given 206000 milliseconds. The desired format for this value would be: 00:00:03:26. However, when utilizing the following code: showTimeWithHour(milliSeconds: numb ...

Encountering a 'ng serve' error while trying to include a new SCSS style in an

I recently created a fresh Angular application using the CLI. For my stylesheet, I opted for SCSS. Upon running the application with ng serve, everything was running smoothly. However, when I added some styles to the stylesheet, such as: body { backgr ...

Filter through the array of objects using the title key

I'm attempting to extract specific data by filtering the 'page_title' key. Below is a snippet of my JSON object: { "page_components": [ { "page_title": "My Account", "row_block": [ { "heading": "", "sub_headi ...

Zero's JSON Journey

When I make an HTTP request to a JSON server and store the value in a variable, using console.log() displays all the information from the JSON. However, when I try to use interpolation to display this information in the template, it throws the following er ...

The outcome of the .then function is not defined following another .then function

I am struggling with handling async requests in Angular within Ionic4. I have successfully loaded a .json file and saved it into an array, but when trying to edit the array within a loop, my promise resolves before the loop finishes. As a newcomer to Angul ...

Encountering a problem while trying to incorporate Mapbox GL JS into an Angular 8 web application

I'm currently working on incorporating mapbox into my simple web application, but I'm encountering difficulties when attempting to add it. At this point, I've already created a mapbox service and a map component. My approach involved using ...

I am looking to update my table once I have closed the modal in Angular

I am facing an issue with refreshing the table in my component using the following function: this._empresaService.getAllEnterprisePaginated(1);. This function is located in my service, specifically in the modal service of the enterprise component. CODE fo ...

Encountering issues with obtaining tokens silently using Angular and Microsoft MSAL, resulting in errors AADB2C90077 and AADB2C90205

Seeking assistance in retrieving a token from AAD B2C configuration using Angular9 and microsoft/msal My module setup is as follows; MsalModule.forRoot( { auth: { clientId: "xxxxxxxxxxxxxxxxx", ...

What could be the reason my mat-form-field is not displaying the label?

I'm currently working on a project using Angular Material to build a web page. I am facing an issue with the mat-form-field component as the label is not displaying, and I can't figure out why. Any assistance would be greatly appreciated. Thank y ...

When you call Subscribe(), you will receive the output "complete: () => void; 'complete' is defined in this context. What exactly does this signify?

Currently, I am following a tutorial and after meticulously checking and rechecking, I can confidently say that my code matches exactly with the one the professor is using. The tutorial involves creating a simple Single Page Application in Angular using Ty ...

The performance of the NextJs app API is unpredictable when deployed on Vercel

After deploying my NextJs app to Vercel, I encountered a strange issue. The static pages are functioning perfectly fine, but the API part (which pulls data from an Azure SQL database using async mssql) is behaving inconsistently. Sometimes it returns the e ...

Nested ControlGroup in Angular2's ControlArray

I've hit a roadblock trying to iterate through a ControlArray that has Controlgroups in a template. In TypeScript, I successfully created the ControlArray and added some ControlGroups by looping over data fetched from an API. The console displays the ...

What purpose does the # symbol serve in Angular when interpolating values?

Here is an example provided from the following link: https://angular.io/guide/lifecycle-hooks export class PeekABoo implements OnInit { constructor(private logger: LoggerService) { } // Implementing OnInit's `ngOnInit` method ngOnInit() { this ...

Angular - calculate the total of numerical values within a nested *ngFor loop

Looking to extract numerical values from an array of objects, where each object contains specific data within arrays. I attempted using *ngFor to iterate through the values, but instead of summing them up, they are concatenated together. Here's a brie ...

No updates found (Angular)

When a button is clicked, a test method is triggered with i as the index of an element in an array. The test method then changes the value of the URL (located inside the sMediaData object) to null or '' and sends the entire sMediaData to the pare ...

Tips for streamlining interface initialization and adding items to it

I have designed an interface that includes another interface: export interface Parent { children: Child[]; } export interface Child { identifier: string; data: string; } Is there a more efficient way to initialize and add items to the array? Curren ...