A bare bones containerized (Docker) development environment for Node.js.
A simple hapi.js-based web application and a MongoDB make up a multi-container Docker application.
The setup is aimed at providing both, a local development enviroment and optimized developer experience as well as support for building deployment artifacts for a Continuous Integration and Continuos Deployment pipeline.
We`re using docker-compose to define and run a multi-container/ multi-service Docker applications:
web
Node.js hapi web applicationdb
MongoDB database
*nix environment, i.e. Linux or macOS.
Docker installed, check w/ docker --version
.
docker-compose installed, check w/ docker-compose --version
.
The multi-container environment is composed as follows:
-
The individual services are configured in the file
docker-compose-services.yml
, looked down, bare-minimal configuration. For convenience the defaultdocker-compose
file name is used. -
For the development enviroment the services in 1. are extended in the file
docker-compose.yml
, basically adding additional development juice: mapping the local directory, addingnodemon
w/ debug option, etc. -
For a 2nd enviroment, here integration the services in 1. are extended in the file
docker-compose-integration.yml
, using pre-build images.
nodemon
NODE_ENV=development
- Ports exposed to host:
- Web:
8000
- Node.js debug:
5858
- MongoDB:
27017
- Web:
Build the container for the web application, onetime... or if you make changes to the Dockerfile
(e.g. installing system packages or global NPM modules):
docker-compose build
Start the development enviroment, w/ logs from all services on stdout.
docker-compose up
... to terminate: Ctrl+C
.
Start the development enviroment as a daemon:
docker-compose up -d
... and point your browser to the url http://localhost:8000.
To tail the log files for the web application:
docker-compose logs -f web
To install a NPM module. e.g. hapi, into the RUNNING web container:
docker-compose exec web npm install hapi --save
To install a NPM module. e.g. mongodb, into the STOPPED web container:
docker-compose run web npm install mongodb --save
To stop the enviroment:
docker-compose stop
pm2
NODE_ENV=production
- Ports exposed to host:
- Web:
80
- Web:
Assuming there is a automated build from Git commits set up, similiar to Docker Hub's Automated Builds.
... or even manually:
cd app/web; \
docker build -t matthiasg/nodejsdockerstarter-web:latest .; \
docker push matthiasg/nodejsdockerstarter-web:latest
Start the integration enviroment, w/ logs from all services on stdout.
docker-compose -f docker-compose-integration.yml up
... to terminate: Ctrl+C
.
Start the integration enviroment as a daemon:
docker-compose -f docker-compose-integration.yml up -d
... and point your browser to the url http://localhost.
To tail the log files for the web application:
docker-compose -f docker-compose-integration.yml logs -f web