Dockerizing Node
- Part 1: Running Node Application in Docker Container on Raspberry Pi
- Part 2: Dockerfile
- Part 3: Dockerizing Mongo and Express
- Part 4: docker-compose
- Part 5: Rancher–First Application
When using Docker for some real world application often multiple Containers are required and to build and run them along with their Dockerfiles we need the scripts for building and running them; as realized in Dockerizing Mongo and Express. This becomes hassle and Docker has docker-compose utility that solves exactly this. We can create a “Compose file” (docker-compose.yml file) which is a YAML file; a human readable data serialization format; we configure the application services and its requirements in this file and then using the tool we can create and start all the services using this “compose file”. We define the container environment in a Dockerfile and how they relate to each other and run together in the compose file and then using the docker-compose we can build / run / stop etc them in the single go together.
- You can download docker-compose binary from https://github.com/docker/compose/releases and place it somewhere that’s in your PATH
Lets make a docker-compose.yml file for our Mongo / Express application; our application needs two data volumnes, a docker volume for MongoDB data and the host directory where our Express JS application files are (mounted through CIFS). We need to declare the MongoDB data volume in the compose file. We need two services; one for Mongo and the other for Express (Node); we will define these along with the build entries along with dockerfile entries as we are using alternate file names. We can define image names in there as well. For the HelloExpress; we need to expose the ports and this container also “depends on” the mongo db; with this entry in the compose file; the tool will take care to run it first; we also need to define the links with proper target name as its required given the Express JS application needs a known host name for MongoDB container hard coded in the “connection string” If we don’t define the target name; docker-compose names the container with its own scheme; we can define known names using container_name entries if we want to. Here’s the docker-compose.yml file
version: '2'
volumes:
mongo-data:
driver: local
services:
mongodb:
build:
context: .
dockerfile: Dockerfile.mongodb
image: khurram/mongo
#container_name: mongodb
volumes:
- mongo-data:/data/db
helloexpress:
build:
context: .
dockerfile: Dockerfile.node
image: khurram/node
#container_name: helloexpress
volumes:
- /mnt/srcshare/HelloExpress:/app
entrypoint: nodejs /app/bin/www
ports:
- "3000:3000"
depends_on:
- mongodb
links:
- mongodb:mongodb
Once the compose file is in place; we can use docker-compose up and it will build + run + attach the required volume and services as defined. We can use –d parameter with docker-compose up to detach
C:\khurram\src\HelloExpress>docker-compose.exe up –d
Creating network "helloexpress_default" with the default driver
Creating helloexpress_mongodb_1
Creating helloexpress_helloexpress_1C:\khurram\src\HelloExpress>rem Test http://DockerVM:3000
C:\khurram\src\HelloExpress>docker-compose.exe down
Stopping helloexpress_helloexpress_1 ... done
Stopping helloexpress_mongodb_1 ... done
Removing helloexpress_helloexpress_1 ... done
Removing helloexpress_mongodb_1 ... done
Removing network helloexpress_default
Code @ https://github.com/khurram-aziz/HelloExpress is updated accordingly having the docker-compose.yml file; DockerBuild.bat and DockerRun.bat are no longer needed; but I am leaving them there as well so you can compare and see how docker-compose.yml is made using those two scripts!