3

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
3
  • What are you trying to achieve? your build image will just install dependencies for your angular app. Commented Jun 21, 2019 at 12:56
  • I don't see docker container running after executing docker run command as above. I have verified using docker ps. Commented Jun 21, 2019 at 13:14
  • If I were you, I would add details about if this is for a production or development use case. You might get different answer based on this. Basically the ng cli is not suitable to serve apps in production. You should use a proper server like nginx or something similar. Commented Jun 21, 2019 at 13:58

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

JFYI, It's not a best practice to run an angular app on the production environment using ng serve.
2

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.

1 Comment

The article was very good to know some base point about each step.
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 

Comments

0

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.