Deploying Angular 2 Apps to Bluemix

DZone 's Guide to

Deploying Angular 2 Apps to Bluemix

In this post we take a look at how to deploy your Angular 2 app as a Docker container to IBM's Bluemix and the light configuration you need to do to make it work.

· Cloud Zone ·
Free Resource

Over the last few months, I’ve done quite a lot of Angular 2 development (now Angular 4). Below is a description how to create a new Angular app and deploy it as a Docker container to Bluemix in just a few minutes.

In order to create a new Angular app, you can use the Angular CLI.

npm install -g @angular/cli
ng new angular-app
cd angular-app
ng build --prod

There are several ways to deploy Angular apps to Bluemix. For example, you can build a simple Node.js web server to host the files. Or you can leverage existing HTTP servers like Nginx. You can use Nginx in the Cloud Foundry staticfile-buildpack or in Docker. Since I experienced issues with the Cloud Foundry buildpack (I couldn’t enforce HTTPS), the following steps show how to use Docker.

Copy the following lines in the file ‘Dockerfile’ in your project’s root directory.

FROM nginx:latest
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY dist /usr/share/nginx/html

Copy the following lines into ‘nginx.conf’. There are many other features Nginx provides that can be configured in this file, e.g. GZIP settings and caching.

server {
    listen 80;
    charset utf-8;
    sendfile on;
    root /usr/share/nginx/html;
    location / {
        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
        try_files $uri $uri/ /index.html = 404;
    location /api/v1/namespaces/ {
        proxy_pass https://openwhisk.ng.bluemix.net; 

The location ‘/api/v1/namespaces/’ is an example how to invoke a REST API from your Angular app, in this case, to invoke an OpenWhisk action.

Next, the Docker container needs to be built.

docker build -t angular-app .
docker tag angular-app registry.ng.bluemix.net/nheidloff/angular-app
docker push registry.ng.bluemix.net/nheidloff/angular-app

There are different ways to run the container on Bluemix — for example, as scalable container group or in a Kubernetes cluster. For testing purposes, you can also run the container as single instance with a public IP address:

bx ic run --name angular-app -p 80 -m 128 registry.ng.bluemix.net/nheidloff/angular-app
bx ic ips
bx ic ip-bind angular-app
bx ic inspect angular-app

This is the file structure of the Angular project.


angular 2, cloud, docker containers, ibm bluemix, tutorial

Published at DZone with permission of Niklas Heidloff , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}