1

I want to deploy my angular6 project to docker but I have a problem. I created Dockerfile:

# stage 1
    FROM node:latest as node
    WORKDIR /app
    COPY . .
    RUN npm install
    RUN npm run build --prod
    RUN npm install -g @angular/cli

    # stage 2
    FROM nginx:alpine
    COPY --from=node /app/dist/ClientAp /usr/share/nginx/html

And when I did it I wrote :

docker build -t cltapp .

And run:

docker run --rm -d -p 4200:4200 cltapp

I don't have error but when I try open my application localhost:4200/ I have blank page. I thought, that it was a problem with angular cli but I added this line RUN npm install -g @angular/cli to my dockerfile but it didn't help me. If you can please help me. Thank's for your answer.

1
  • 1
    I'm not a docker specialist, but that runs the nginx container, right? I don't think nginx runs on port 4200 by default. And installing angular cli globally, after you've already used the local install to build the app, does seem completely useless to me. Commented Sep 15, 2018 at 17:42

1 Answer 1

2

Usually, after running ng build --prod, index.html will be placed directly under dist forlder (dist/index.html). I assume that this is you case, and not dist/ClientAp/index.html. You can check it by running ng build --prod in your project and inspecting created dist folder.

--prod flag should be placed within your package.json => scripts section:

"scripts": {
  ...
  "build": "ng build --prod",
  ...
}

if it is not there and you want to add it after npm run build, it should be added after --, like:

npm run build -- --prod

so, try this:

Dockerfile

# stage 1
FROM node:alpine as node

# Create app directory
WORKDIR /usr/src/app

# Bundle app source (make sure you have package.json copied)
COPY . .

# install dependencies, @angular cli will be installed here as well
RUN npm install

# build your project into dist folder
RUN npm run build

# stage 2
FROM nginx:alpine

WORKDIR /usr/share/nginx/html
COPY --from=node /usr/src/app/dist/ .

Nginx will run at port 80. If you want to use port 4200, run this:

docker run --rm -d -p 4200:80 cltapp

Now, you should see you angular app at localhost:4200

My code is in a dry run mode (I did not test it), so please test it and modify if needed.

Sources:

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

5 Comments

I got : Welcome to nginx! If you see this page, the nginx web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using nginx. . Where I should do that?
Where I should do that?
probably your index.html was not copied. Did you check where it is located exactly after running npm run build --prod? is it dist/index.html?
In dist/ClientAp/index.html
I change: COPY --from=node /usr/src/app/dist/ . for this zCOPY --from=node /usr/src/app/dist/ClientAp .` works fine thank you very much :)

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.