Builder

The first stage is builder. We need a lightweight build environment that can ensure consistent builds across the board. For this purpose, I've created a sample Alpine-based Node build environment complete with npm, bash, and git tools. For more information on why we're using Alpine and Node, refer to Chapter 3, Prepare Angular App for Production Release, in the Containerizing the App using Docker section:

  1. Implement a new npm script to build your Angular app:
"scripts": {
"build:prod": "ng build --prod",
}
  1. Inherit from a Node.js based build environment like node:10.1  or duluca/minimal-node-build-env:8.11.2
  2. Implement your environment specific build script, as shown here:
Note that at the time of publishing a bug in low-level npm tooling is preventing node based images  from successfully installing Angular dependencies. This means that the sample Dockerfile below is based on an older version of Node and npm with duluca/minimal-node-build-env:8.9.4. In the future, when the bugs are sorted out an updated build environment will be able to leverage npm ci to install dependencies, which brings significant speed gains over the npm install command.
Dockerfile.integration
FROM duluca/minimal-node-build-env:8.9.4 as builder

# project variables
ENV SRC_DIR /usr/src
ENV GIT_REPO https://github.com/duluca/lemon-mart.git
ENV SRC_CODE_LOCATION .
ENV BUILD_SCRIPT build:prod

# get source code
RUN mkdir -p $SRC_DIR
WORKDIR $SRC_DIR
# if necessary, do SSH setup here or copy source code from local or CI environment
RUN git clone $GIT_REPO .
# COPY $SRC_CODE_LOCATION .

RUN npm install
RUN npm run $BUILD_SCRIPT

In the preceding example, the source code is being pulled from GitHub by the container. I have chosen to do that for the sake of keeping the sample simple, because it works the same way in both local and remote continuous integration environments. However, your CI server will already have a copy of the source code, which you'll want to copy from your CI environment and then into the container.

Instead of the RUN git clone $GIT_REPO . command, you can copy source code with the COPY $SRC_CODE_LOCATION . command from your CI server or your local machine. If you do this, you will have to implement a .dockerignore file that somewhat resembles your .gitignore file to ensure that secrets aren't leaked, node_modules is not copied and the configuration is repeatable in other environments. In a CI environment, you will want to override the environment variable $SRC_CODE_LOCATION so that the source directory of the COPY command is correct. Feel free to create multiple versions of the Dockerfile that may fit your various needs.

In addition, I have built a minimal Node build environment duluca/minimal-node-build-env based on node-alpine, which you can observe on Docker Hub at https://hub.docker.com/r/duluca/minimal-node-build-env. This image is about ten times smaller than node. The size of Docker images have a real impact on build times, since the CI server or your team members will spend extra time pulling a larger image. Choose the environment that best fits your needs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.118.14