Skip to content Skip to sidebar Skip to footer

Help Center

< All Topics
Print

Docker Compose for Containerizing A MEAN Stack Application

This article describes how to build an Angular 5 MEAN stack application that runs in Docker and uses MongoDB, NodeJS, Express, and Mongo. It is based on a Github demonstration I made, which is available here.

Overview of Docker

Docker is used to run applications in containers, which contain all of the necessary runtimes, system tools, libraries, OS, and other components that you would typically need to install yourself in order to run the application.

Containers vs. VMs

Running Docker containers differ from virtual machines (VMs) in that they can share resources from the host system, such as the kernel and networking, whereas VMs are segregated (with HyperV), including everything inside its own guest OS. Containers can be significantly smaller than virtual machines and start up much faster because they can share the resources of the host system.

Docker Concepts

1.     Images

A read-only description of how to build a container that can be used as a container.

2.     Containers

Containers are instantiated images that have all of the libraries and runtime dependencies needed to run an application.

3.     DOCKERFILE

A file used to generate Docker images and specify the image’s dependencies, settings, and operating instructions. When using the “docker build” and “docker run” commands, this file is read.

4.     Docker-compose

A file that runs on top of the “docker build” and “docker run” commands to indicate how images should be built and executed, such as an image for the client application, a server image, and a database image.

Advantages of Docker Over Conventional Methods

Because we can easily build our application as images and run these images as containers on machines with Docker installed, using Docker makes it simple to get an application running on various environments. The developer no longer has to deal with the inconvenience of manually creating environments and managing dependencies.

Setting up the Components of the Application

By using DOCKERFILEs to create a Docker image for each component of the application, we will now set up each component individually and Dockerize it. By doing this, we can subsequently use the various images in docker-compose.yml to create the mean stack app.

In this docker-compose file, a service for the client, server, and database is specified:\

  • Client: We map its port from 4200 to 80 on the host and specify the DOCKERFILE to build as client/DOCKERFILE.
  • Server: We map the DOCKERFILE’s port from 8080 to 8080 on the host and specify the DOCKERFILE for a build as server/DOCKERFILE. The database and server are linked via links and depends_on as well.
  • Database: This instance uses the Mongo image, with the container’s volumes for the database being mapped to the host and the container being exposed at port 27017 of the host.

Wrapping Up

In this tutorial, we learned how to install and operate a basic MEAN stack app by building Docker images from DOCKERFILEs and using docker-compose to build and execute them.

You might also think about making a dev docker-compose.yml for simpler development, where you mount local files to enable automatic reloading of the application on code change without having to install local dependencies like Node.

Being a subsidiary of Sambodhi Research and Communications Pvt. Ltd., Education Nest is a global knowledge exchange platform that empowers learners with data-driven decision making skills.

Our comprehensive set of courses helps you gain mastery in the various programming languages. Connect with our expert teams to learn more about our services today!

Table of Contents