Using Docker for Node Development
Docker is an open-source project that allows processes/applications to be run in isolated ‘containers,’ very similar to a virtual machine but with far less overhead.
Docker’s primary use is for deployment, but it is increasingly being used during development. There are several benefits to developing with Docker, including:
- providing a consistent development environment for teams to work in
- ensuring that applications run the same during development (local machine) and production (remote machine)
- simplifying the setup of development resources, such as databases
Including Docker in your development process requires a few additional steps. Below I will detail one way to use Docker for development. The order is a bit backward because there are dependencies amongst the steps, but the general idea is (1) create a network for the containers to communicate, (2) run a container with mongodb that is connected to the network in step 1, and (3) run an express app (from the host) via a node container that is also connected to the network in step 1.
Step 1: create a network for your containers to communicate on
docker network create -d bridge example
This command creates a network that your containers will be able to communicate over, making it easy to have multiple containers running isolated services (in our case this will be a separate mongo database and express server).
Step 2: create and run a mongodb container
docker run -d --name db --net=example mongo
Step 3: create and run a Node container
docker run -d -p 8080:3000 — name web — net=example -v $PWD:/app -w /app node npm start
You now have an express/mongo app running in networked containers.