I am trying to run an angular application in development mode inside a docker container, but when i run it with docker-compose build it works correctly but when i try to put up the container i obtain the below error:
ERROR: for sypgod Cannot start service sypgod: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"npm\": executable file not found in $PATH
The real problem is that it doesn't recognize the command npm serve, but why??
The setup would be below:
Docker container (Nginx Reverse proxy -> Angular running in port 4000)
I know that there are better ways of deploying this but at this moment I need this setup for some personals reasons
Dockerfile:
FROM node:10.9
COPY package.json package-lock.json ./
RUN npm ci && mkdir /angular && mv ./node_modules ./angular
WORKDIR /angular
RUN npm install -g @angular/cli
COPY . .
FROM nginx:alpine
COPY toborFront.conf /etc/nginx/conf.d/
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]
CMD ["npm", "serve", "--port 4000"]
NginxServerSite
server{
listen 80;
server_name sypgod;
location / {
proxy_read_timeout 5m;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:4000/;
}
}
Docker Compose file(the important part where I have the problem)
sypgod: # The name of the service
container_name: sypgod # Container name
build:
context: ../angular
dockerfile: Dockerfile # Location of our Dockerfile
The image that is finally running is this:
FROM=nginx:alpine COPY toborFront.conf /etc/nginx/conf.d/ EXPOSE 8080 CMD ["npm", "serve", "--port 4000"] The first stage has no effect (could COPY --from=... files), and if there are multiple CMD , only the last one has an effect. Since you are running this on a plain nginx image, there is no npm command, which results in the error you see.
I would recommend using Node on the host for a live development environment. When you've built and tested your app and are looking to deploy it, use Docker if appropriate. In your Dockerfile, run ng build in the first stage to build the app into static files, add COPY --from=... in the second stage to get the app built into the Nginx image, and remove all CMD lines ( nginx has an appropriate default CMD ). @VikramJakhar's answer has a more complete Dockerfile that shows this.
It sounds like you might be trying to run Nginx and the Angular development server on Docker. If that's your goal, you should run them in two separate containers. To do this:
Split this Dockerfile in two. Put the CMD ["npm", "serve"] at the end of the first Dockerfile (Angular only).
Add a second block in the docker-compose.yml file to run the second container. The npm serve backend container does not need to publish ports: .
Change the hostname of the backend server in the Nginx configuration from localhost to the Docker Compose name of the other container.
It would appear the npm can't be accessed from the container. Try defining where it tries to execute it from:
docker run -v "$PWD":/usr/src/app -w /usr/src/app node:10.9 npm serve --port 4000
source: https://gist.github.com/ArtemGordinsky/b79ea473e8bc6f67943b
Also make sure that npm is installed on the computer running the docker container.
You can do something like below
### STAGE 1: Build ###
# We label our stage as ‘builder’
FROM node:alpine as builder
RUN apk --no-cache --virtual build-dependencies add \
git \
python \
make \
g++
RUN mkdir -p /ng-app/dist
WORKDIR /ng-app
COPY package.json package-lock.json ./
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm install
COPY . .
## Build the angular app in production mode and store the artifacts in dist folder
RUN npm run ng build -- --prod --output-path=dist
### STAGE 2: Setup ###
FROM nginx:1.14.1-alpine
## Copy our default nginx config
COPY toborFront.conf /etc/nginx/conf.d/
## Remove default nginx website
RUN rm -rf "/usr/share/nginx/html/*"
## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"]