Skip to content

🤟 Dockerized node.js app with optional Terraform deploy.

License

Notifications You must be signed in to change notification settings

nickbatts/tf-docker-nodejs-app

Repository files navigation

Node.js App Docker Boilerplate

MIT license Build passing Version 0.5.0

Dockerized node.js app with optional Terraform deploy.

Table of Contents

Overview

This is a boilerplate to get started with deploying a node.js app to a docker cluster using either docker-compose or Terraform.

The index.js is the entry point for the app and

The entire infrastructure can be deployed to a docker host using Terraform.

Other possible Terrraform implementations are:

  • The node.js app can be deployed to a bare EC2 instance on AWS.
  • The node.js app can be deployed to a docker swarm cluster on AWS.

Install tools

Instructions for Terraform (MacOS):

    $ brew update
    $ brew install terraform

For other platforms, follow the links and instructions on the Terraform site. Remember to periodically update these packages.

Quick start

Clone the repo:

$ git clone https://github.com/nickbatts/tf-docker-nodejs-app.git
$ cd tf-docker-nodejs-app

Docker-compose-v3 version:

$ docker stack deploy --compose-file docker-compose.yml nodeapp 

Terraform version:

$ terraform plan terraform-docker-nodejs-server.tf
$ terraform apply terraform-docker-nodejs-server.tf

Run terraform from Docker container (Optional)

Instead of installing terraform on your local machine, there is a Docker build for a container with all the necessary tools installed:

$ docker run -it hashicorp/terraform:light plan terraform-docker-nodejs-server.tf

Customization

The index.js & package.json files can be changed to match your node.js project

To build:

$ docker build -t nodejs-server:latest .
Sending build context to Docker daemon   5.12kB
Step 1/6 : FROM node:alpine
 ---> b5f94997f35f
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> c20d6bbd0a0b
Step 3/6 : COPY . .
 ---> Using cache
 ---> a73ee70d8901
Step 4/6 : EXPOSE 3000
 ---> Using cache
 ---> 628949a45bb1
Step 5/6 : USER node
 ---> Using cache
 ---> 9a0681bee832
Step 6/6 : CMD ["node","index.js"]
 ---> Using cache
 ---> c5c2dbdeafdc
Successfully built c5c2dbdeafdc
Successfully tagged nodejs-server:latest

View containers logs:

$ docker logs <container-id/container-name>
server is listening on 3000

Destroy all resources

$ terraform destroy

This will destroy ALL resources created by this project. You will be asked to confirm before proceeding.

Build multi-node cluster

The number of app worker nodes are defined in docker-compose.yml

Change the replicas in the file to scale cluster. eg, change to 3:

    deploy:
      mode: replicated
      replicas: 3

Technical notes

  • File tree
./
|-- Dockerfile							# docker build file
|-- docker-compose.yml					# docker service definition
|-- nginx.conf							# load balancing server
|-- index.js							# node app entry point
|-- package.json						# app package definitions
`-- terraform-docker-nodejs-server.tf	# deploy with terraform
  • You may need to edit the docker host variable in the terraform template if not using local docker.

Helpful Commands

Docker administration

  • terraform init - initialize terraform and install modules
  • $ docker ps - view running docker processes
  • $ docker service ls - view running docker services if using docker-compose.yml
  • $ docker stop $(docker ps -aq) && docker rm $(docker ps -aq) - stop and remove all docker containers

Authors

  • Nick Batts

License

Copyright (c) 2018 Nick Batts

This project is licensed under the terms of the MIT license.

About

🤟 Dockerized node.js app with optional Terraform deploy.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published