Dieses Blog durchsuchen

Sonntag, 4. September 2016

docker: create a docker-compose(v2) node.js-mongodb-docker-network with a litte sampleapp


In our last tutorial we have created a network with 2 containers in it,
One node.js app container with a pet app and one with our mongo db.

That worked very well, but it was a hard fight on the console. We dont want to do that every time.  Now we want to automate that with a docker-compose.yml file.

Let us start.

Prerequisits

We need some things installed on the local machine.
  • docker installed
  • git installed 
  • docker compose installed in min version 1.8.x  

If you are on ubuntu16.04 , you will need to install docker compose manually, because the official version 1.5.x is too old for us.

If you are on windows and mac, just install docker toolbox
 

Install docker-compose 1.8.0 on ubuntu

Open a terminal
$ curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Now you should be able to run:
$ docker-compose -v


Checkout our sample app

We want to use our little pet app with node.js from the last tutorials too. It starts an express server and connects to a mongodb server "my-mongodb". The app simply lists some docs pics and some describing texts

Clone the app repository in your projectroot
$ git clone https://github.com/pboethig/dockerlessons.git
$ cd dockerlessons/docker-compose

 

Inspect the docker-compose.yml

In the projectroot you find a "docker-compose.yml". This is our central file to create our container enviroment. I hope that xml or json will come on stage it the future again.

This is the content
version: '2'

services:
  mongodb:
    image: mongo
    ports:
      - "mongodb:27017:27017"
    networks:
       - nodeapp
  node:
    build: 
       context: .
       dockerfile: node.dockerfile
    ports: 
      - "8080:3000"    
    networks:
       - nodeapp

  
networks:
  nodeapp:
    driver: bridge
To explain that
  • Line 1 defines the version of docker-compose to use. At the moment v2 is brand new and you need docker-compose 1.6.0 minimum to read it
  • Line2 contains the services object. Here are all servicenodes attached. In our case "node" and mongodb. These are the names of the images
  • As you can see mongo is build from an "image" named "mongo"
  • The mongo-portforwarding is more or less self explaining.
  • As you can see, the "node" container is build by a a dockerfile. The dockerfile resides in context "." which means, that the buildfunction looks in the current directory for it   
  • Interresting is the network definition. Both, mongodb and node are added to a network "nodeapp" which is defined at the end of the file.
  • The network "nodeapp" itself  is defined as a bridged network.
  • So both machines are encapsolated in its own network

    That is great!


If you whish, you can have a look in the node.dockerfile, to see how the nodeapp is build from.

Compose your containernetwork


Now we want to start the rocket.

Open a console and type
docker-compose build

After some seconds you should see a success message

Up your container network
docker-compose up -d

This will start your enviroment directly.

Inspect you container
docker ps


Shows your running container prefixed with "dockercompose"

docker network ls 

shows your network "dockercompose_nodeapp"

At last we want to populate some data again.

Populate data
docker exec dockercompose_nodeapp node dbSeeder.js


Now you can reach your app under http://localhost:8080







Keine Kommentare:

Kommentar veröffentlichen