I have a docker-composer file and a Dockerfile for angular project, but it only works, when the node packages are already installed.
As node packages are excluded of the git repo, when someone clones it and tries to start it, it always fails. Dockerfile runs, it runs the command “npm install”, but the node_modules directory is created only inside the container and when the container starts, the volume overrides the container’s content, so the node_modules directory gets removed.
I have a workaround for that, I run a conteiner in interactive (docker run -it –name temp-container angular-image bash), run the npm install manually inside that container, then I exit the container and run “docker cp temp-container:/home/node/node_modules ./”.
So I’m not interested in workarounds, I want to know, if there is any solution for this to install dependencies with npm inside the container for the first time it’s built and copy the node_modules out from the container automatically when it’s up.
Here’s the docker-compose.yml (dummy version) and the Dockerfile:
version: '3'
services:
node:
build:
context: .
dockerfile: Dockerfile
image: 'angular'
container_name: angular
networks:
- docker_infra
volumes:
- .:/home/node
command: 'npm start'
labels:
- "traefik.enable=true"
- "traefik.http.routers.angular.rule=(Host(`angular.lh`))"
- "traefik.http.routers.angular.entrypoints=web"
- "traefik.http.services.angular.loadbalancer.server.port=4200"
- "traefik.docker.network=docker_infra"
restart: unless-stopped
networks:
docker_infra_infra:
external: true
FROM node:20.9.0
ENV NODE_VERSION 20.9.0
RUN npm install -g @angular/cli
WORKDIR /home/node
COPY . /home/node/
RUN npm install
I need to copy all the other content into the container, otherwise npm tells, it’s not an angular project. I have the package.json, as it’s included by the git repo.