Skip to content

Commit

Permalink
Fix docker-compose.yml and added BASE_HREF for angular
Browse files Browse the repository at this point in the history
  • Loading branch information
dasito26 committed Nov 12, 2024
1 parent c22eecc commit 0db4e3d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# ID for identifying containers created by docker-compose
# This variable is valid for all projects (debug, hub, nginx, and "production")
ID_PROJECT=mean

# For angular, the base href is the path where the app is hosted, use / if the app is hosted in the root.
# This is used to generate the index.html file with the correct base href and is only valid for production mode.
BASE_HREF=/contacts/
4 changes: 1 addition & 3 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ app.use(
{ url: "/api/user/authenticate", methods: ["POST"] },
{ url: "/index.html", methods: ["GET"] },
{ url: /\.js$/, methods: ["GET"] },
{ url: /\.css$/, methods: ["GET"] },
{ url: /^\/api\/beneficiarios(.*)$/, methods: ["POST"] },
{ url: "/", methods: ["GET"] }
{ url: /\.css$/, methods: ["GET"] }
]
})
);
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ services:
build: # specify the directory of the Dockerfile
context: .
dockerfile: dockerfile
args:
BASE_HREF: ${BASE_HREF:-/}
container_name: ${ID_PROJECT:-mean}_angular_express
ports:
- "3000:3000" #specify ports forewarding
- "4200:3000" #specify ports forewarding
# Below database enviornment variable for api is helpful when you have to use database as managed service
environment:
- SECRET=Thisismysecret
Expand Down
8 changes: 7 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ WORKDIR /app

COPY /frontend .

ARG BASE_HREF=/

## Build the angular app in production mode and store the artifacts in dist folder
RUN npm run build:prod

## Change base href in index.html
RUN sh -c "find . -name \"index*.html\" -exec sed -i 's|<base href=\"/\">|<base href=\"${BASE_HREF}\">|' {} +"

### STAGE 2: Setup ###
FROM node:20-alpine

Expand All @@ -26,7 +31,8 @@ COPY /api/ /app/
RUN npm ci

## From ‘builder’ copy published angular bundles in app/public
COPY --from=builder /app/dist /app/public
COPY --from=builder /app/dist/contacts/browser /app/public

## expose port for express
EXPOSE 3000

Expand Down
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"start": "ng serve",
"serve": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"build:prod": "ng build --configuration production",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"serve:ssr:contacts": "node dist/contacts/server/server.mjs"
Expand Down Expand Up @@ -51,4 +52,4 @@
"prettier-plugin-tailwindcss": "0.6.1",
"typescript": "~5.4.2"
}
}
}

0 comments on commit 0db4e3d

Please sign in to comment.