0

I have project structure that look like this:

apps
--services1
--- here all the file of service1
--services2
--- here all the file of service2
libs
-- Here will have some files, needed by services1 and services2
packages.json
Dockerfile.service1
Dockerfile.service2
docker-compose.yml 

in my Dockerfile.service1 then I do like this:

FROM node:12.17-alpine as builder

WORKDIR /build
COPY package.json yarn.lock ./
RUN yarn

COPY . .
RUN  yarn build:services1

EXPOSE 3000

CMD [ "yarn", "start:services1"]

Dockerfile.service2 also having the same command

So right now my problem is:

Dockerfile.services1 and Dockerfile.services2 both will COPY all the project file into the image, means that Service1Image will contain files of apps/services2 file.

Soon, the project will have 50 services, inside apps folder, therefore in order to build 1 docker image for 1 service, the image will contain files of 50 microservices.

Question:

Is it anyway to exclude folder in apps folder excepts apps/services1 in Service1Image? But it need to include libs, packages.json inside Service1Image, and the same way to Service2Image. Is it possible?

My attempt

FROM node:12.17-alpine as builder

WORKDIR /build

// I only copy `libs` `apps/services1`, is this right??
COPY package.json yarn.lock libs apps/services1 ./
RUN yarn

COPY . .
RUN  yarn build:app-one

EXPOSE 3000

CMD [ "yarn", "start:app-one"]
ken
  • 2,426
  • 5
  • 43
  • 98
  • Checkout multi-stage builds https://docs.docker.com/develop/develop-images/multistage-build/ – D.B.K Dec 28 '21 at 16:52
  • why not just copy in the things you do want instead of copying everything and trying to specify what you dont want. it sounds like you only need to copy the libs and services 1 stuff. so just copy those. othewise when you have 50 services instead of just specifying 2 things to copy you will end up specifying 50 things to not copy and this will be bulky and unmanagable over time – Chris Doyle Dec 28 '21 at 16:54
  • @D.B.K so I should by for `libs` in `stage1`, then in `stage2` by for `apps/services1` like this right?? – ken Dec 28 '21 at 16:54
  • @ChrisDoyle YES, that what I want. But I just havent figure it out – ken Dec 28 '21 at 16:55
  • are you using webpack? if yes, i would suggest running `yarn build` in stage1 and then copy over JUST the artifact, i.e the output of webpack build , probably `build` folder to stage2 and include just that in your final docker image – D.B.K Dec 28 '21 at 16:56
  • @D.B.K I'm not using webpack for this project so far, just a Nestjs monorepo which have the structure above. I have an attempt in the question, am I doing it right?? – ken Dec 28 '21 at 16:59
  • https://stackoverflow.com/questions/30256386/how-to-copy-multiple-files-in-one-layer-using-a-dockerfile – Chris Doyle Dec 28 '21 at 18:27
  • @ChrisDoyle tq sir, seen that – ken Dec 28 '21 at 18:40

0 Answers0