Mohan
Mohan

Reputation: 741

Running Angular app as docker image using Node js

Trying to build angular application in docker and run as container in my local using Node js.

I have used build image using below Dockerfile, but i am not sure what i am missing while running. Can someone point me out?

Dockerfile:

FROM node:10.15.3
ENV HOME=/home
WORKDIR $HOME
RUN npm config set strict-ssl false \
    && npm config set proxy http://proxy.xxxxxx.com:8080
COPY package.json .
RUN npm install

Image created with below command successfully

docker build -t example .

I am trying to run the image using below command, but it is not helping

docker run -p 4201:4200 example

Upvotes: 3

Views: 5773

Answers (4)

Steffi Keran Rani J
Steffi Keran Rani J

Reputation: 4093

Give a shot for the following Dockerfile as well!

FROM node:alpine

# get the app
WORKDIR /src

# install packages
RUN npm ci
RUN npm install -g @angular/cli
COPY package.json .
RUN npm install
COPY . .

# start app
CMD ["ng", "serve", "-o"]

Upvotes: 0

Just update your Dockerfile to achieve your goal for more options see here:

# base image
FROM node:12.2.0

RUN npm config set strict-ssl false \
    && npm config set proxy http://proxy.xxxxxx.com:8080

# 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 package.json /app/package.json
RUN npm install
RUN npm install -g @angular/[email protected]

# add app
COPY . /app

# start app
CMD ng serve --host 0.0.0.0 

Upvotes: 0

atline
atline

Reputation: 31574

Container need a foreground process running, then it will not exit. If not, the container will directly exit.

For your case, you need to COPY your nodejs project to container when docker build, and also start the project in CMD like CMD [ "npm", "start" ]. As the web server not exit, then your container will not exit.

A good article here for your reference on how to dockerizing a Node.js web app.

Upvotes: 2

Ammar
Ammar

Reputation: 1324

your Dockerfile does not run/serve your application, in order to do that you have to:

  • install angular/cli
  • copy the app
  • run/serve the app
FROM node:10.15.3

RUN npm config set strict-ssl false \
    && npm config set proxy http://proxy.xxxxxx.com:8080

# get the app
WORKDIR /src
COPY . .

# install packages
RUN npm ci
RUN npm install -g @angular/cli

# start app
CMD ng serve --host 0.0.0.0

hope this helps.

Upvotes: 4

Related Questions