Setting up a new Angular 17 project (Node 18) as follows:
$ npm install -g @angular/cli@17
$ ng update @angular/core@17 @angular/cli@17
$ npm update
$ ng new example
stylesheets? $ SCSS
SSR? $ yes
$ cd example
$ ng serve
Connects just fine on localhost:4200.
Deploying it with Docker does not work, because all the example Dockerfiles expect older Angular versions without SSR.
Seriously, guys. Before giving an answer try actually creating an Angular 17 SSR project and try if it works with your Dockerfile.
I tried following these suggestions (remember to replace as necessary e.g.the node version node:12-slim by node:18-slim, the project name sample-angular-app by the project name example, doing the ports correctly etc.):
server.js)DockerfileFROM node:18-alpine as build
WORKDIR /app/src
COPY package*.json ./
RUN npm ci
COPY . ./
RUN npm run build
FROM node:18-alpine
WORKDIR /usr/app
COPY --from=build /app/src/dist/PROJECT_NAME ./
CMD node server/server.mjs
EXPOSE 4000
docker build . -t PROJECT_NAME -f Dockerfile
docker run -d PROJECT_NAME -p 8080:4000
PROJECT_NAME by the project name.node:18 to support Angular 17.dist/PROJECT_NAME/server to get the version that can be served.server.mjs, there is no server.js.4000 is the default port set in server.ts, see the line const port = process.env['PORT'] || 4000; in there. Of course one can define another port either by editing the server.ts or setting the PORT environment variable, but I kept it simple here.8080 is the port where one can see the project in the browser at http://localhost:8080/.Starting from the accepted answer, I provide another solution which instead uses nginx to serve the application:
FROM node:18-alpine as build
WORKDIR /app/src
COPY package*.json ./
RUN npm ci
COPY . ./
RUN npm run build
FROM nginx:1.23
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
COPY --from=build /app/src/dist/$PROJECT_NAME/server /var/www/html
CMD ["nginx", "-g", "daemon off;"]
Now an example of nginx.conf (which has to be found on the same folder as Dockerfile) can be:
server {
listen $YOUR_PORT;
server_name localhost;
# Serve static files
location / {
root /var/www/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}
Note that it's preferable to use docker compose to run docker containers, instead of the Dockerfile.
I also provide an example of docker-compose.yml (to be put in the same folder as Dockerfile).
version: '3.2'
services:
ui:
build:
context: .
dockerfile: Dockerfile
ports:
- "$YOUR_PORT:$YOUR_PORT"
now you can run:
docker compose build
docker compose up
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With