Basically, both services foo and bar depend on a common library.
Let's assume that the common package has already been published to the npm registry.
|
├── packages
| ├── common
| | ├── src
| | ├── package.json
| | ├── tsconfig.build.json
| | ├── tsconfig.json
| ├── foo
| | ├── src
| | ├── Dockerfile
| | ├── package.json
| | ├── tsconfig.build.json
| | ├── tsconfig.json
| ├── bar
| | ├── src
| | ├── Dockerfile
| | ├── package.json
| | ├── tsconfig.build.json
| | ├── tsconfig.json
├── tsconfig.json
├── package.json
├── yarn.lock
├── docker-compose.init.yml
├── docker-compose.yml
├── Dockerfile
├── Dockerfile.init
├── .dockerignore
I've added all devDependencies that are common to all of the packages in the root package.json, as follows:
"scripts": {
"build": "lerna run build --stream",
"setup": "yarn && yarn build",
"docker:bootstrap": "docker-compose --file=docker-compose.init.yml build",
"docker:up": "docker-compose up --build"
},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
"@nestjs/common": "^7.4.4",
"@nestjs/core": "^7.4.4",
"@nestjs/platform-express": "^7.4.4",
"@nestjs/schematics": "^7.1.2",
"@nestjs/testing": "^7.4.4",
"@types/express": "^4.17.8",
"@types/jest": "^26.0.13",
"@types/node": "^14.10.2",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/parser": "^4.1.1",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"express": "^4.17.1",
"husky": "^4.3.0",
"jest": "^26.4.2",
"lerna": "^3.22.1",
"lint-staged": "^10.4.0",
"prettier": "^2.1.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"supertest": "^4.0.2",
"ts-jest": "^26.3.0",
"ts-loader": "^8.0.3",
"ts-node": "^9.0.0",
"typescript": "3.9.5"
}
The foo package needs to use a relational database, so I've installed the following packages independently.
$ yarn workspace foo add @nestjs/typeorm mysql typeorm
To resolve an error message "<package> has unmet peer dependency <package>", I've hit the following command.
$ yarn workspace foo add @nestjs/common @nestjs/core @nestjs/platform-express rxjs
I'm kinda lost here. What's the point of organizing multiple applications in a monorepo manner if I keep repeating myself installing the same packages from one package to another? It makes me even more difficult to write a Dockerfile after all.
My first question is, when a developer working on a monolithic codebase, is this a normal behavior to install libraries to a specific package if necessary?
This is how my Dockerfile looks like:
// docker-compose.init.yml
# This file triggers the initial build
version: "3.8"
services:
pkg_builder:
image: pkg-builder
build:
context: .
dockerfile: Dockerfile.init
First, execute the command below.
$ yarn docker:bootstrap
Dockerfile.init creates an initial builder image from which the “real” builder image can copy the build directory.
// Dockerfile.init
FROM scratch
# Copy files from the root to build directory
COPY package.json lerna.json yarn.lock tsconfig.json /build/
# This line is required to install dependencies from foo's package.json
COPY ./packages/foo/package.json /build/packages/foo/package.json
From then on, build the images using the command:
$ yarn docker:up
// docker-compose.yml
version: "3.8"
services:
pkg_builder:
image: pkg-builder
build: .
mariadb:
image: mariadb:10.3
ports:
- "3306:3306"
environment:
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=tutorial
restart: always
foo:
container_name: foo
build: ./packages/foo
ports:
- "8000:8000"
depends_on:
- mariadb
restart: always
// Dockerfile
FROM node:12-alpine
COPY --from=pkg-builder /build /build
WORKDIR /build
RUN rm -rf node_modules
RUN yarn
CMD ["true"]
The problem is, the size of an image way too big. This is because, all the dev dependencies have been copied from the pkg-builder.
// foo's Dockerfile
FROM node:12-alpine
WORKDIR /app/current
COPY --from=pkg-builder /build/node_modules /app/current/packages/foo/node_modules
COPY --from=pkg-builder /build/tsconfig.json ./tsconfig.json
WORKDIR /app/current/packages/foo
COPY . .
RUN yarn build
EXPOSE 8000
CMD [ "node", "./dist/main" ]
Lastly, how am I supposed to reduce the image? In this case, I believe that multi-stage build is not the right strategy for the size reduction. What am I missing here?