Dockerized node.js app with optional Terraform deploy.
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.
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.
$ git clone https://github.com/nickbatts/tf-docker-nodejs-app.git
$ cd tf-docker-nodejs-app
$ docker stack deploy --compose-file docker-compose.yml nodeapp
$ terraform plan terraform-docker-nodejs-server.tf
$ terraform apply terraform-docker-nodejs-server.tf
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
The index.js & package.json files can be changed to match your node.js project
$ 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
$ docker logs <container-id/container-name>
server is listening on 3000
$ terraform destroy
This will destroy ALL resources created by this project. You will be asked to confirm before proceeding.
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
- 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.
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
- Nick Batts
Copyright (c) 2018 Nick Batts
This project is licensed under the terms of the MIT license.