Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

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 169.46.26.176 angular-app
bx ic inspect angular-app


This is the file structure of the Angular project.

angular2bluemix

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

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

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}