Tutorial

Create a MEAN app with Angular and Docker Compose

Draft updated on Invalid Date
author

Chris Ganga

Create a MEAN app with Angular and Docker Compose

This tutorial is out of date and no longer maintained.

Introduction

Note: Update: 30/03/2019

This article has been updated based on the updates to both docker and angular since this article was written. The current version of angular is 7, the updates also adds an attached docker volume to the angular client so that you don’t need to run docker-compose build every time.

Docker allows us to run applications inside containers. These containers in most cases communicate with each other.

Docker containers wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries – anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.

We’ll build an angular app in one container, point it to an Express API in another container, which connects to MongoDB in another container.

If you haven’t worked with Docker before, this would be a good starting point as we will explain every step covered, in some detail.

Why Use Docker

  1. Docker images usually include only what your application needs to run. As a result, you don’t have to worry about having a whole operating system with things you will never use. This results in smaller images of your application.
  2. Platform Independent - I bet you’ve heard of the phrase ‘It worked on my machine and doesn’t work on the server’. With Docker, all either environments need to have is the Docker Engine or the Docker Daemon, and when we have a successful build of our image, it should run anywhere.
  3. Once you have an image of your application built, you can easily share the image with anyone who wants to run your application. They need not worry about dependencies, or setting up their individual environments. All they need to have is Docker Engine installed.
  4. Isolation - You’ll see from the article that I try to separate the individual apps to become independent, and only point to each other. The reason behind this is that each part of our entire application should be somewhat independent, and scalable on its own. Docker in this instance would make scaling these individual parts as easy as spinning up another instance of their images. This concept of building isolated, independently scalable parts of an entire system are what is called Microservices Approach. You can read more about it in Introduction to Microservices
  5. Docker images usually have tags, referring to their versions. This means you can have versioned builds of your image, enabling you to roll back to a previous version should something unexpected break.

Prerequisites

You need to have docker and docker-compose installed in your setup. Instructions for installing docker in your given platform can be found here.

Instructions for installing docker-compose can be found here.

Verify your installation by running:

  1. docker -v
Output
Docker version 18.09.2, build 6247962
  1. docker-compose -v
Output
docker-compose version 1.23.2, build 1110ad01
  1. node -v
Output
v11.12.0

Next, you need to know how to build a simple Angular app and an Express App. We’ll be using the Angular CLI to build a simple app.

Single Builds Approach

We’ll now separately build out these three parts of our app. The approach we are going to take is building the app in our local environment, then dockerizing the app.

Once these are running, we’ll connect the three docker containers. Note that we are only building two containers, Angular and the Express/Node API. The third container will be from a MongoDB image that we’ll just pull from the Docker Hub.

Docker Hub is a repository for docker images. It’s where we pull down official docker images such as MongoDB, NodeJs, Ubuntu, and we can also create custom images and push them to Docker Hub for other people to pull and use.

Let’s create a directory for our whole setup, we’ll call it mean-docker.

  1. mkdir mean-docker

Angular Client App

Next, we’ll create an Angular app and make sure it runs in a docker container.

Create a directory called angular-client inside the mean-docker directory we created above, and initialize an Angular App with the Angular CLI.

We’ll use npx, a tool that allows us to run CLI apps without installing them into our system. It comes preinstalled when you install Node.js since version 5.2.0

  1. npx @angular/cli new angular-client
? Would you like to add Angular routing? No
? Which stylesheet format would you like to use? CSS

This scaffolds an Angular app, and npm installs the app’s dependencies. Our directory structure should be like this

└── mean-docker
    └── angular-client
        ├── README.md
        ├── angular.json
        ├── e2e
        ├── node_modules
        ├── package.json
        ├── package-lock.json
        ├── src
        ├── tsconfig.json
        └── tslint.json

Running npm start, inside the angular-client directory should start the angular app at http://localhost:4200.

Dockerizing Angular 2 Client App

To dockerize any app, we usually need to write a Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

To quickly brainstorm on what our angular app needs in order to run,

  1. We need an image with Node.js installed on it
  2. We could have the Angular CLI installed on the image, but the package.json file has it as a dependency, so it’s not a requirement.
  3. We can add our Angular app to the image and install its dependencies.
  4. It needs to expose port 4200 so that we can access it from our host machine through localhost:4200.
  5. If all these requirements are met, we can run npm start in the container, which in turn runs ng serve since it’s a script in the package.json file, created from the image and our app should run.

Those are the exact instructions we are going to write in our Dockerfile.

mean-docker/angular-client/Dockerfile
# Create image based on the official Node 10 image from dockerhub
FROM node:10

# Create a directory where our app will be placed
RUN mkdir -p /app

# Change directory so that our commands run inside this new directory
WORKDIR /app

# Copy dependency definitions
COPY package*.json /app/

# Install dependencies
RUN npm install

# Get all the code needed to run the app
COPY . /app/

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "start"]

I’ve commented on the file to show what each instruction clearly does.

Note: Before we build the image, if you are keen, you may have noticed that the line COPY . /app/ copies our whole directory into the container, including node_modules. To ignore this, and other files that are irrelevant to our container, we can add a .dockerignore file and list what is to be ignored. This file is usually sometimes identical to the .gitignore file.

Create a .dockerignore file.

mean-docker/angular-client/.dockerignore
node_modules/

One last thing we have to do before building the image is to ensure that the app is served from the host created by the docker image. To ensure this, go into your package.json and change the start script to:

mean-docker/angular-client/package.json
{
 ...
  "scripts": {
    "start": "ng serve --host 0.0.0.0",
    ...
  },
  ...
}

To build the image we will use docker build command. The syntax is

  1. docker build -t <image_tag>:<tag> <directory_with_Dockerfile>

Make sure you are in the mean_docker/angular-client directory, then build your image.

  1. cd angular-client
  1. docker build -t angular-client:dev .

-t is a shortform of --tag, and refers to the name or tag given to the image to be built. In this case the tag will be angular-client:dev.

The . (dot) at the end refers to the current directory. Docker will look for the Dockerfile in our current directory and use it to build an image.

This could take a while depending on your internet connection.

Now that the image is built, we can run a container based on that image, using this syntax

  1. docker run -d --name <container_name> -p <host-port:exposed-port> <image-name>

The -d flag tells docker to run the container in detached mode. Meaning, it will run and get you back to your host, without going into the container.

  1. docker run -d --name angular-client -p 4200:4200 angular-client:dev 8310253fe80373627b2c274c5a9de930dc7559b3dc8eef4abe4cb09aa1828a22

--name refers to the name that will be assigned to the container.

-p or --port refers to which port our host machine should point to in the docker container. In this case, localhost:4200 should point to dockerhost:4200, and thus the syntax 4200:4200.

Visit localhost:4200 in your host browser should be serving the angular app from the container.

You can stop the container running with:

  1. docker stop angular-client

Dockerize the Express Server API

We’ve containerized the angular app, we are now two steps away from our complete setup.

Containerizing an express app should now be straightforward. Create a directory in the mean-docker directory called express-server.

  1. mkdir express-server

Add the following package.json file inside the app.

mean-docker/express-server/package.json
{
  "name": "express-server",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "~1.15.2",
    "express": "~4.14.0"
  }
}

Then, we’ll create a simple express app inside it. Create a file server.js

  1. cd express-serve
  1. touch server.js
  1. mkdir routes && cd routes
  1. touch api.js
mean-docker/express-server/server.js
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');

// Get our API routes
const api = require('./routes/api');

const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Set our api routes
app.use('/', api);

// Get port from environment and store in Express.
const port = process.env.PORT || '3000';
app.set('port', port);

// Create HTTP server.
const server = http.createServer(app);

// Listen on provided port, on all network interfaces.
server.listen(port, () => console.log(`API running on localhost:${port}`));
[labe mean-docker/express-server/routes/api.js]
const express = require('express');
const router = express.Router();

// GET api listing.
router.get('/', (req, res) => {
    res.send('api works');
});

module.exports = router;

This is a simple express app, install the dependencies and start the app.

  1. npm install
  1. npm start

Going to localhost:3000 in your browser should serve the app.

To run this app inside a Docker container, we’ll also create a Dockerfile for it. It should be pretty similar to what we already have for the angular-client.

mean-docker/express-server/Dockerfile
# Create image based on the official Node 6 image from the dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependencies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 3000

# Serve the app
CMD ["npm", "start"]

You can see the file is pretty much the same as the angular-client Dockerfile, except for the exposed port.

You could also add a .dockerignore file to ignore files we do not need.

mean-docker/express-server/.dockerignore
node_modules/

We can then build the image and run a container based on the image with:

  1. docker build -t express-server:dev .
  1. docker run -d --name express-server -p 3000:3000 express-server:dev

Going to localhost:3000 in your browser should serve the API.

Once you are done, you can stop the container with

  1. docker stop express-server

MongoDB container

The last part of our MEAN setup, before we connect them all together is the MongoDB. Now, we can’t have a Dockerfile to build a MongoDB image, because one already exists from the Docker Hub. We only need to know how to run it.

Assuming we had a MongoDB image already, we’d run a container based on the image with

  1. docker run -d --name mongodb -p 27017:27017 mongo

The image name in this instance is mongo, the last parameter, and the container name will be mongodb.

Docker will check to see if you have a mongo image already downloaded, or built. If not, it will look for the image in the Dockerhub. If you run the above command, you should have a mongodb instance running inside a container.

To check if MongoDB is running, simply go to http://localhost:27017 in your browser, and you should see this message. It looks like you are trying to access MongoDB over HTTP on the native driver port.

Alternatively, if you have mongo installed in your host machine, simply run mongo in the terminal. And it should run and give you the mongo shell, without any errors.

Docker Compose

To connect and run multiple containers with docker, we use Docker Compose.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

docker-compose is usually installed when you install docker. So to simply check if you have it installed, run:

  1. docker-compose

You should see a list of commands from docker-compose. If not, you can go through the installation here

Note: Ensure that you have docker-compose version 1.6 and above by running docker-compose -v

Create a docker-compose.yml file at the root of our setup.

  1. touch docker-compose.yml

Our directory tree should now look like this.

.
├── angular-client
├── docker-compose.yml
└── express-server

Then edit the docker-compose.yml file

mean-docker/docker-compose.yml
version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build: angular-client # specify the directory of the Dockerfile
    ports:
      - "4200:4200" # specify port forewarding

  express: #name of the second service
    build: express-server # specify the directory of the Dockerfile
    ports:
      - "3000:3000" #specify ports forwarding

  database: # name of the third service
    image: mongo # specify image to build container from
    ports:
      - "27017:27017" # specify port forwarding

The docker-compose.yml file is a simple configuration file telling docker-compose which containers to build. That’s pretty much it.

Now, to run containers based on the three images, simply run

  1. docker-compose up

This will build the images if not already built, and run them. Once it’s running, and your terminal looks something like this.

You can visit all three apps: http://localhost:4200, http://localhost:3000, or mongodb://localhost:27017. And you’ll see that all three containers are running.

Connecting the 3 Docker containers

Finally, the fun part.

Express and MongoDB

We now finally need to connect the three containers. We’ll first create a simple CRUD feature in our API using mongoose. You can go through Easily Develop Node.js and MongoDB Apps with Mongoose to get a more detailed explanation of mongoose.

First of all, add mongoose to your express server package.json

mean-docker/express-server/package.json
{
  "name": "express-server",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "~1.15.2",
    "express": "~4.14.0",
    "mongoose": "^4.7.0"
  }
}

We need to update our API to use MongoDB:

mean-docker/express-server/routes/api.js
// Import dependencies
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();

// MongoDB URL from the docker-compose file
const dbHost = 'mongodb://database/mean-docker';

// Connect to mongodb
mongoose.connect(dbHost);

// create mongoose schema
const userSchema = new mongoose.Schema({
  name: String,
  age: Number
});

// create mongoose model
const User = mongoose.model('User', userSchema);


// GET api listing.
router.get('/', (req, res) => {
        res.send('api works');
});

// GET all users.
router.get('/users', (req, res) => {
    User.find({}, (err, users) => {
        if (err) res.status(500).send(error)

        res.status(200).json(users);
    });
});

// GET one users.
router.get('/users/:id', (req, res) => {
    User.findById(req.param.id, (err, users) => {
        if (err) res.status(500).send(error)

        res.status(200).json(users);
    });
});

// Create a user.
router.post('/users', (req, res) => {
    let user = new User({
        name: req.body.name,
        age: req.body.age
    });

    user.save(error => {
        if (error) res.status(500).send(error);

        res.status(201).json({
            message: 'User created successfully'
        });
    });
});

module.exports = router;

Two main differences, first of all, our connection to MongoDB is in the line const dbHost = 'mongodb://database/mean-docker';. This database is the same as the database service we created in the docker-compose file.

We’ve also added rest routes GET /users, GET /users/:id and POST /user.

Update the docker-compose file, telling the express service to link to the database service.

mean-docker/docker-compose.yml
version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build: angular-client # specify the directory of the Dockerfile
    ports:
      - "4200:4200" # specify port forewarding
    volumes:
      - ./angular-client:/app # this will enable changes made to the angular app reflect in the container

  express: #name of the second service
    build: express-server # specify the directory of the Dockerfile
    ports:
      - "3000:3000" #specify ports forewarding
    links:
      - database

  database: # name of the third service
    image: mongo # specify image to build container from
    ports:
      - "27017:27017" # specify port forwarding

The links property of the docker-compose file creates a connection to the other service with the name of the service as the hostname. In this case database will be the hostname. Meaning, to connect to it from the express service, we should use database:27017. That’s why we made the dbHost equal to mongodb://database/mean-docker.

Also, I’ve added a volume to the angular service. This will enable changes we make to the Angular App to automatically trigger recompilation in the container

Angular and Express

The last part is to connect the Angular app to the express server. To do this, we’ll need to make some modifications to our angular app to consume the express API.

Add the Angular HTTP Client.

mean-docker/angular-client/src/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http'; // add http client module

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule // import http client module
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
mean-docker/angular-client/src/app/app.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app works!';

  // Link to our api, pointing to localhost
  API = 'http://localhost:3000';

  // Declare empty list of people
  people: any[] = [];

  constructor(private http: HttpClient) {}

  // Angular 2 Life Cycle event when component has been initialized
  ngOnInit() {
    this.getAllPeople();
  }

  // Add one person to the API
  addPerson(name, age) {
    this.http.post(`${this.API}/users`, {name, age})
      .subscribe(() => {
        this.getAllPeople();
      })
  }

  // Get all users from the API
  getAllPeople() {
    this.http.get(`${this.API}/users`)
      .subscribe((people: any) => {
        console.log(people)
        this.people = people
      })
  }
}

Angular best practices guides usually recommend separating most logic into a service/provider. We’ve placed all the code in the component here for brevity.

We’ve imported the OnInit interface, to call events when the component is initialized, then added two methods AddPerson and getAllPeople, that call the API.

Notice that this time around, our API is pointing to localhost. This is because while the Angular 2 app will be running inside the container, it’s served to the browser. And the browser is the one that makes requests. It will thus make a request to the exposed Express API. As a result, we don’t need to link Angular and Express in the docker-compose.yml file.

Next, we need to make some changes to the template. I first added bootstrap via CDN to the index.html

mean-docker/angular-client/src/app/index.html
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Angular Client</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CDN -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css">

  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

Then update the app.component.html template

mean-docker/angular-client/src/app/app.component.html
<!-- Bootstrap Navbar -->
<nav class="navbar navbar-light bg-faded">
  <div class="container">
    <a class="navbar-brand" href="#">Mean Docker</a>
  </div>
</nav>

<div class="container">
    <h3>Add new person</h3>
    <form>
      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" #name>
      </div>
      <div class="form-group">
        <label for="age">Age</label>
        <input type="number" class="form-control" id="age" #age>
      </div>
      <button type="button" (click)="addPerson(name.value, age.value)" class="btn btn-primary">Add person</button>
    </form>

    <h3>People</h3>
    <!-- Bootstrap Card -->
    <div class="card card-block col-md-3" *ngFor="let person of people">
      <h4 class="card-title">{{person.name}}  {{person.age}}</h4>
    </div>
</div>

The above template shows the components’ properties and bindings. We are almost done.

Since we’ve made changes to our code, we need to do a build for our Docker Compose

  1. docker-compose up --build

The --build flag tells docker compose that we’ve made changes and it needs to do a clean build of our images.

Once this is done, go to localhost:4200 in your browser,

We are getting a No 'Access-Control-Allow-Origin' error. To quickly fix this, we need to enable Cross-Origin in our express app. We’ll do this with a simple middleware.

mean-docker/express-server/server.js
// Code commented out for brevity

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Cross Origin middleware
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
  next()
})

// Set our api routes
app.use('/', api);

// Code commented out for brevity

We can now run docker-compose again with the build flag. You should be in the mean-docker directory.

  1. docker-compose up --build

Going to localhost:4200 on the browser.

Conclusion

Note: I added an attached volume to the docker-compose file, and we now no longer need to rebuild the service every time we make a change.

I bet you’ve learned a thing or two about MEAN or docker and docker-compose.

The problem with our set up however is that any time we make changes to either the angular app or the express API, we need to run docker-compose up --build.

This can get tedious or even boring over time. We’ll look at this in another article.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Chris Ganga

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more