HUB
Sign inSign up
amd64-babel logo

area51/amd64-babel

By area51

•Updated almost 9 years ago

babel & eslint in a docker image. Used to save downloading so much all of the time

Image
0

5.1K

area51/amd64-babel repository overview

⁠docker image containing babel & eslint

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.

⁠Including in a build

In this example we have a simple project with three files:

  • Dockerfile
  • package.json
  • src/index.js

The docker file will then compile the project and install it in to the /opt/node directory of the container.

  • /opt/node/node_modules/
  • /opt/node/index.js

When the container is run then index.js will be used.

⁠Dockerfile
# 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"]

⁠How this works

This Dockerfile is a multi stage build. Stage 1 does the compilation whilst stage 2 creates the final image.

⁠Multiple architecture support

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:

  • amd64
  • arm64v8
  • arm32v6 when I fix that build server
⁠Stage 1 - compilation
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.

⁠Stage 2 - building the final image
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.

⁠Command reference

⁠babel
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
eslint source

Runs eslint against the source, checking for errors or warnings. Any errors will fail the build.

⁠usenpmrepository
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.

⁠Running eslint --fix

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.

⁠Useful files

  • /usr/local/babel/.babelrc
  • /usr/local/babel/.eslintrc

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

⁠Build status

ImageArchitectureBuild Status
area51/amd64-babel⁠amd64Build Status
area51/arm64v8-babel⁠arm64v8Build Status
Tag summary
Recent tags
Content type
Image
Digest
Size
41 MB
Last updated
almost 9 years ago
docker pull area51/amd64-babel:latest