GaryO
GaryO

Reputation: 6338

Back-end typescript in monorepo: how to get dependencies for docker?

I have a project in a yarn-workspaces monorepo, containing front-end, back-end, and common packages. All in typescript.

Building and running the front end is fine; it uses webpack to create a dist/ dir that I can deploy. The back end should be easier, because it's just typescript -> node.js. I can just tsc and package the resulting dist dir into my docker image... except for the node_modules dependencies. How can I get just the needed dependencies into dist? I don't want a full-on bundler with all the transpiling and fancy stuff.

Some people use multi-stage docker to create the build (by running yarn install --production in the fresh docker build image, so it's not part of the monorepo); that seems like overkill to me, and it seems harder to debug that way because you don't get to run the actual build from your dev machine.

I'd like a tool that produces a working dist folder that I can package with docker or any other method. Is there a simple way to get to that?

Upvotes: 1

Views: 1250

Answers (1)

David Maze
David Maze

Reputation: 159722

You can COPY whatever you'd like in your Dockerfile. If your application is already built on your host, then you can just copy in the build artifacts and its runtime dependencies. For a Node package this might look like

FROM node
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY dist dist/
CMD ["node", "/app/dist/index.js"]

Of note, yarn install --production avoids copying in anything in the "devDependencies" section of your package.json file, which would skip things like tsc and webpack.

(This technique is more common for compiled languages – especially Java Dockerfiles tend to just copy in a built .jar or .war file – and in very old Docker that pre-dates multi-stage builds.)

Upvotes: 1

Related Questions