Docker is a containerization tool used to streamline application development and deployment workflows across various environments.

This tutorial shows how to Dockerize an Angular app, built with the Angular CLI, using Docker along with Docker Compose and Docker Machine for both development and production. We’ll specifically focus on-

Setting up an image for development with code hot-reloading that includes an instance of Chrome for Karma and Protractor testing Configuring a lean, production-ready image using multistage builds

Updates:

May 2019: Updated to the latest versions of Docker, Node, Angular, and Nginx. Added explanations for various Docker commands and flags. Added a number of notes based on reader comments and feedback. Fixed the running of Protractor e2e tests.



We will be using:

Docker v18.09.2

Angular CLI v7.3.9

Node v12.2.0

Contents

Project Setup

Install the Angular CLI globally:

$ npm install -g @angular/cli@7.3.9

Generate a new app:

$ ng new example $ cd example

Docker

Add a Dockerfile to the project root:

# base image FROM node:12.2.0 # install chrome for protractor tests RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' RUN apt-get update && apt-get install -yq google-chrome-stable # set working directory WORKDIR /app # add `/app/node_modules/.bin` to $PATH ENV PATH /app/node_modules/.bin:$PATH # install and cache app dependencies COPY package.json /app/package.json RUN npm install RUN npm install -g @angular/cli@7.3.9 # add app COPY . /app # start app CMD ng serve --host 0.0.0.0

Add a .dockerignore as well:

node_modules .git .gitignore

This will speed up the Docker build process as our local dependencies and git repo will not be sent to the Docker daemon.

Build and tag the Docker image:

$ docker build -t example:dev .

If RUN npm install -g @angular/cli@7.3.9 results in an infinite loop, you may need to add an --unsafe flag: RUN npm install -g @angular/cli@7.3.9 --unsafe Review this issue for more info.

Then, spin up the container once the build is done:

$ docker run -v ${ PWD } :/app -v /app/node_modules -p 4201:4200 --rm example:dev

What’s happening here?

The docker run command creates a new container instance, from the image we just created, and runs it. -v ${PWD}:/app mounts the code into the container at “/app”. {PWD} may not work on Windows. See this Stack Overflow question for more info. Since we want to use the container version of the “node_modules” folder, we configured another volume: -v /app/node_modules . You should now be able to remove the local “node_modules” flavor. -p 4201:4200 exposes port 4200 to other Docker containers on the same network (for inter-container communication) and port 4201 to the host. For more, review this Stack Overflow question. Finally, --rm removes the container and volumes after the container exits.

Open your browser to http://localhost:4201 and you should see the app. Try making a change to the AppComponent ’s template (src/app/app.component.html) within your code editor. You should see the app hot-reload. Kill the server once done.

What happens when you add -it ? $ docker run -it -v ${ PWD } :/app -v /app/node_modules -p 4201:4200 --rm example:dev Check your understanding and look this up on your own.

Use the -d flag to run the container in the background:

$ docker run -d -v ${ PWD } :/app -v /app/node_modules -p 4201:4200 --name foo --rm example:dev

Once up, update the Karma and Protractor config files to run Chrome in headless mode.

src/karma.conf.js:

module . exports = function ( config ) { config . set ({ basePath : '' , frameworks : [ 'jasmine' , '@angular-devkit/build-angular' ], plugins : [ require ( 'karma-jasmine' ), require ( 'karma-chrome-launcher' ), require ( 'karma-jasmine-html-reporter' ), require ( 'karma-coverage-istanbul-reporter' ), require ( '@angular-devkit/build-angular/plugins/karma' ) ], client : { clearContext : false }, coverageIstanbulReporter : { dir : require ( 'path' ). join ( __dirname , '../coverage/example' ), reports : [ 'html' , 'lcovonly' , 'text-summary' ], fixWebpackSourcePaths : true }, reporters : [ 'progress' , 'kjhtml' ], port : 9876 , colors : true , logLevel : config . LOG_INFO , autoWatch : true , // updated browsers : [ 'ChromeHeadless' ], // new customLaunchers : { 'ChromeHeadless' : { base : 'Chrome' , flags : [ '--no-sandbox' , '--headless' , '--disable-gpu' , '--remote-debugging-port=9222' ] } }, singleRun : false , restartOnFileChange : true }); };

e2e/protractor.conf.js:

const { SpecReporter } = require ( 'jasmine-spec-reporter' ); exports . config = { allScriptsTimeout : 11000 , specs : [ './src/**/*.e2e-spec.ts' ], capabilities : { 'browserName' : 'chrome' , // new 'chromeOptions' : { 'args' : [ '--no-sandbox' , '--headless' , '--window-size=1024,768' ] } }, directConnect : true , baseUrl : 'http://localhost:4200/' , framework : 'jasmine' , jasmineNodeOpts : { showColors : true , defaultTimeoutInterval : 30000 , print : function () {} }, onPrepare () { require ( 'ts-node' ). register ({ project : require ( 'path' ). join ( __dirname , './tsconfig.e2e.json' ) }); jasmine . getEnv (). addReporter ( new SpecReporter ({ spec : { displayStacktrace : true } })); } };

Run the unit and e2e tests:

$ docker exec -it foo ng test --watch = false $ docker exec -it foo ng e2e --port 4202

Stop the container once done:

$ docker stop foo

Want to use Docker Compose? Add a docker-compose.yml file to the project root:

version : ' 3.7' services : example : container_name : example build : context : . dockerfile : Dockerfile volumes : - ' .:/app' - ' /app/node_modules' ports : - ' 4201:4200'

Take note of the volumes. Without the anonymous volume ( '/app/node_modules' ), the node_modules directory would be overwritten by the mounting of the host directory at runtime. In other words, this would happen:

Build - The node_modules directory is created in the image.

directory is created in the image. Run - The current directory is mounted into the container, overwriting the node_modules that were installed during the build.

Build the image and fire up the container:

$ docker-compose up -d --build

Ensure the app is running in the browser and test hot-reloading again. Try both the unit and e2e tests as well:

$ docker-compose exec example ng test --watch = false $ docker-compose exec example ng e2e --port 4202

Stop the container before moving on:

$ docker-compose stop

Windows Users: Having problems getting the volumes to work properly? Review the following resources: Docker on Windows–Mounting Host Directories Configuring Docker for Windows Shared Drives You also may need to add COMPOSE_CONVERT_WINDOWS_PATHS=1 to the environment portion of your Docker Compose file. Review the Declare default environment variables in file guide for more info.

Docker Machine

To get hot-reloading to work with Docker Machine and VirtualBox you’ll need to enable a polling mechanism via chokidar (which wraps fs.watch , fs.watchFile , and fsevents ).

Create a new Machine:

$ docker-machine create -d virtualbox clever $ docker-machine env clever $ eval $( docker-machine env clever )

Grab the IP address:

$ docker-machine ip clever

Then, build the images:

$ docker build -t example:dev .

And run the container:

$ docker run -it -v ${ PWD } :/app -v /app/node_modules -p 4201:4200 --rm example:dev

Test the app again in the browser at http://DOCKER_MACHINE_IP:4201 (make sure to replace DOCKER_MACHINE_IP with the actual IP address of the Docker Machine). Also, confirm that auto reload is not working. You can try with Docker Compose as well, but the result will be the same.

To get hot-reload working, we need to add an environment variable: CHOKIDAR_USEPOLLING=true .

$ docker run -it -v ${ PWD } :/app -v /app/node_modules -p 4201:4200 -e CHOKIDAR_USEPOLLING = true --rm example:dev

Test it out again. Then, kill the server and add the environment variable to the docker-compose.yml file:

version : ' 3.7' services : example : container_name : example build : context : . dockerfile : Dockerfile volumes : - ' .:/app' - ' /app/node_modules' ports : - ' 4201:4200' environment : - CHOKIDAR_USEPOLLING=true

Spin up the container. Run the unit tests and e2e tests.

Production

Let’s create a separate Dockerfile for use in production called Dockerfile-prod:

############# ### build ### ############# # base image FROM node:12.2.0 as build # install chrome for protractor tests RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' RUN apt-get update && apt-get install -yq google-chrome-stable # set working directory WORKDIR /app # add `/app/node_modules/.bin` to $PATH ENV PATH /app/node_modules/.bin:$PATH # install and cache app dependencies COPY package.json /app/package.json RUN npm install RUN npm install -g @angular/cli@7.3.9 # add app COPY . /app # run tests RUN ng test --watch = false RUN ng e2e --port 4202 # generate build RUN ng build --output-path = dist ############ ### prod ### ############ # base image FROM nginx:1.16.0-alpine # copy artifact build from the 'build environment' COPY --from=build /app/dist /usr/share/nginx/html # expose port 80 EXPOSE 80 # run nginx CMD ["nginx", "-g", "daemon off;"]

Two important things to note:

First, we take advantage of the multistage build pattern to create a temporary image used for building the artifact – the production-ready Angular static files – that is then copied over to the production image. The temporary build image is discarded along with the original files, folders, and dependencies associated with the image. This produces a lean, production-ready image. In other words, the only thing kept from the first image is the compiled distribution code. Check out the Builder pattern vs. Multi-stage builds in Docker blog post for more info on multistage builds. Next, the unit and e2e tests are run in the build process, so the build will fail if the tests do not succeed.

Using the production Dockerfile, build and tag the Docker image:

$ docker build -f Dockerfile-prod -t example:prod .

Spin up the container:

$ docker run -it -p 80:80 --rm example:prod

Assuming you are still using the same Docker Machine, navigate to http://DOCKER_MACHINE_IP/ in your browser.

Test with a new Docker Compose file as well called docker-compose-prod.yml:

version : ' 3.7' services : example-prod : container_name : example-prod build : context : . dockerfile : Dockerfile-prod ports : - ' 80:80'

Fire up the container:

$ docker-compose -f docker-compose-prod.yml up -d --build

Test it out once more in your browser. Then, break a test in src/app/app.component.spec.ts and re-build. It should fail:

ERROR: Service 'example-prod' failed to build: The command '/bin/sh -c ng test --watch=false' returned a non-zero code: 1

If you’re done, go ahead and destroy the Machine:

$ eval $( docker-machine env -u ) $ docker-machine rm clever

Cheers!