Jørgen Tvedt Jørgen Tvedt - 14 days ago 15
TypeScript Question

Node and docker - how to handle babel or typescript build?

I have a node application that I want to host in a Docker container, which should be straight forward, as seen in this article:

https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

In my project, however, the sources can not be run directly, they must be compiled from ES6 and/or Typescript. I use gulp to build with babel, browserify and tsify - with different setups for browser and server.

What would be the best workflow for building and automating docker images in this case? Are there any resources on the web that describes such a workflow? Should the Dockerimage do the building after

npm install
or should I create a shell script to do all this and simply have the Dockerfile pack it all together?

If the Dockerfile should do the build - the image would need to contain all the dev-dependencies, which are not ideal?

Note: I have been able to set up a docker container, and run it - but this required all files to be installed and built beforehand.

Answer

One possible solution is to wrap your build procedure in a special docker image. It is often referred as Builder image. It should contain all your build dependencies: nodejs, npm, gulp, babel, tsc and etc. It encapsulates all your build process, removing the need to install these tools on the host.

First you run the builder image, mounting the source code directory as a volume. The same or a separate volume can be used as output directory. The first image takes your code and runs all build commands.

As a first step you take your built code and pack it into production docker image as you do now.

Here is an example of docker builder image for TypeScript: https://hub.docker.com/r/sandrokeil/typescript/

It is ok to have the same docker builder for several projects as it is typically designed to be general purpose wrapper around some common tools. But it is ok to build your own that describes more complicated procedure.

The good thing about builder image is that your host environment remains unpolluted and you are free to try newer versions of compiler/different tools/change order/do tasks in parallel just by modifing Dockerfile of your builder image. And at any time you can rollback your experiment with build procedure.