I have many micro-frontend (MFE) apps written in Reactjs.
And finally my webapp consumes all those MFEs.
docker-compose.yml
version: '3.8'
services:
mfe1:
build:
context: ../packages/mfe1
dockerfile: Dockerfile
image: mfe1:1.1
container_name: mfe1_container
ports:
- 4001:80
mfe2:
build:
context: ../packages/mfe2
dockerfile: Dockerfile
image: mfe2:1.1
container_name: mfe2_container
ports:
- 4002:80
web-app:
build:
context: ../packages/webapp
dockerfile: Dockerfile
image: webapp:1.1
container_name: webapp_container
ports:
- 4000:80
depends_on:
- mfe1
- mfe2
This will create different containers for each MFE.
is there a way I can run all MFEs in a single container ? is it even possible ?
is it also possible to create single docker image ?
DockerFile (for MFE1) and it is almost same for other MFEs)
# stage-1
FROM node:18.17-bookworm-slim as mf1-build
WORKDIR /app
COPY package*.json ./
ENV NODE_OPTIONS="--max-old-space-size=1536"
RUN npm install --no-package-lock
COPY . ./
RUN npm run docker:build
RUN npm prune --production
EXPOSE 4001
# stage-2
FROM node:18.17-bookworm-slim
WORKDIR /app
COPY --from=mfe1-build /app/dist ./dist
COPY --from=mfe1-build /app/node_modules ./node_modules
## nginx stage
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=mfe1-build /app/dist /usr/share/nginx/html
2
Answers
You might want to use a custom entrypoint script to achieve your goals?
https://www.warp.dev/terminus/dockerfile-run-sh
To deploy multiple React micro-frontends (MFEs, as seen in this article from Rahul Sharma ) in a single Docker container, you will need to consolidate them in a way that they can be served from a single HTTP server.
You can still keep a two-stage build process for each MFE: that optimizes the build by creating smaller, more efficient final images.
The
nginx.conf
will serve each MFE from its own path: