babel & eslint in a docker image. Used to save downloading so much all of the time
618
A docker image to aid in compiling nodejs applications using babel.
The reason for this is that babel is itself pretty large so this image reduces the number of times that babel has to be downloaded from the npm repository.
In this example we have a simple project with three files:
The docker file will then compile the project and install it in to the /opt/node directory of the container.
When the container is run then index.js will be used.
# The default architecture
ARG arch=amd64
FROM area51/${arch}-babel:latest as builder
# The application's final location
WORKDIR /opt/node
# Install our dependencies
ADD package.json .
RUN npm install
# Now compile the source
ADD src /tmp/src/
RUN eslint /tmp/src
RUN babel /tmp/src
# Now build the final image
FROM area51/${arch}-node:latest
LABEL maintainer="Peter Mount <[email protected]>"
COPY --from=builder /opt/node /opt/node/
WORKDIR /opt/node
CMD ["node","index.js"]
This Dockerfile is a multi stage build. Stage 1 does the compilation whilst stage 2 creates the final image.
The ARG arch=amd64 line defines the default architecture for the build, so if you run:
docker build -t mytag .
Then it will build on amd64.
If you are running on a different architecture then you can select this on the build line. e.g.
docker build -t mytag --build-arg arch=arm64v8 .
The supported architectures are:
FROM area51/${arch}-babel:latest as builder
Here we use this image as the underlying image and label it as builder.
WORKDIR /opt/node
This defines where we want the application to be installed within the container.
ADD package.json .
RUN npm install
Next we add the project's package.json and run npm install to install the project's dependencies.
ADD src /tmp/src/
After installing the dependencies we copy the src directory into /tmp/src in the image.
Note: We do this after running npm install so that if the source changes but not package.json we utilise docker's cache and don't install the dependencies every time we run a build - another time saver.
RUN eslint /tmp/src
Next we run eslint against /tmp/src. This is optional but allows for errors to be found before compilation.
RUN babel /tmp/src
Finally we run babel against /tmp/src. This will compile those .js files and place them unto the currect WORKDIR.
FROM area51/${arch}-node:latest
LABEL maintainer="Peter Mount <[email protected]>"
This selects the base image for the final image, here our own node image but you can use any image that includes nodejs.
COPY --from=builder /opt/node /opt/node/
Here we copy the application from stage 1 into this image.
WORKDIR /opt/node
CMD ["node","index.js"]
Finally we set the WORKDIR to the application base and the command to run it when the container starts.
babel source [destination]
This will compile all .js files under source and place the result in the destination.
If destination is not provided then the current directory is used.
eslint source
Runs eslint against the source, checking for errors or warnings. Any errors will fail the build.
usenpmrepository [url]
This will point npm to use an alternate third-pary npm repository rather than the default public one. If no url is provided then the public one is used.
This is trickier but doable. It will modify your source for most warnings generated by eslint.
Perform a backup of your source first!
If you have your source in the src directory, run the following from the parent directory:
docker run -it --rm -v $(pwd)/src:/usr/local/babel/src area51/amd64-babel eslintfix
This will then run eslint --fix against your source.
NB: Change amd64 to the correct architecture you are using.
If you need to override any of those config files then overwrite them in the Dockerfile before running babel or eslint.
If overriding .eslintrc when using eslintfix you need to mount it on the command line:
docker run -it --rm \
-v $(pwd)/.eslintrc:/usr/local/babel/.eslintrc:ro \
-v $(pwd)/src:/usr/local/babel/src area51/amd64-babel \
eslintfix
| Image | Architecture | Build Status |
|---|---|---|
| area51/amd64-babel | amd64 | |
| area51/arm64v8-babel | arm64v8 |
docker pull area51/arm64v8-babel:latest