Monday, May 4, 2020

Docker Assignments #13: Node.js, MySQL, Express, Nodemon and Docker Compose

Docker Compose is a tool in the form of YAML configuration to run multi-container docker applications. With a single command, we can start or stop all the services specified in YAML configuration.

Docker Compose is a 3 step process

  1. Define app configuration using Dockerfile
  2. Define the services that app requires in docker-compose.yml so they can be run together in an isolated environment
  3. Run docker-compose up to starts and runs entire app
In the previous article (http://millionvisit.blogspot.com/2020/05/docker-assignments-12-nodejs-mysql.html) we had written docker run command to spin up "mysql" instance. But, In this article we are going to spin up "mysql" instance by using docker-compose.yml file

Create a folder structure as per below image in the host machine
  1. Add "mysql" and "express" npm packages as a dependency in the package.json file
  2. Add "nodemon" npm package as a dev dependency in the package.json file
  3. configure npm start command as "nodemon -L index.js" in package.json
  4. Write required code into index.js file to connect "mysql" container and expose endpoint using      express.js
  5. Configure Dockerfile with below instructions
  • set base image as "mhart/alpine-node"
  • set working directory  as "/usr/app"
  • copy all files from host machine and do npm install
  • set CMD command to execute npm run start
     6. Write following instructions in docker-compose yml file 
  • Configure "testmysql-service" service by pulling "mysql:5.7" image, set "MY_SQL_ROOT_PASSWORD" as "mysql123" and port mapping - 3306
  • Configure "testnodeexpress-service" service by specifying build context as Dockerfile, create bindmount to /usr/app container path, volume mount to /usr/app/node_modules, dependent on "testmysql-service" and link to "testmysql-service"
     7. Spin up containers using docker-compose up command
     8. check http://localhost:9030

Step 1: 
     > npm install mysql
     > npm install express

Step 2: 
     > npm install -D nodemon

Step 3: 
  "scripts": {
    "start""nodemon -L index.js"
  }

Step 4: 
var express = require('express');
var mysql=require('mysql');
var app = express();

app.get('/mysql'function (reqres) {
    var con = mysql.createConnection({
        host"testmysql",
        user"root",
        password"mysql123",
        port:"3306",
      });
      con.connect(function(err) {
        if (errthrow err;
        res.send("mysql connected successfully");      
      });
});

app.listen(9030function () {
    console.log('App listening on port 9030!');
});

Step 5:
FROM mhart/alpine-node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9030
CMD ["npm""run""start"]

Step 6:
version'3'
services:
    testnodeexpress-service:
      build.
      volumes:
        - .:/usr/app 
        - /usr/app/node_modules       
      environment:
        NODE_ENVdevelopment  
      ports:
        - 9030:9030
      commandnpm run start
      depends_on
        - testmysql-service
      links
        - "testmysql-service:testmysql"  
    testmysql-service:
      imagemysql:5.7      
      environment
        MYSQL_ROOT_PASSWORDmysql123  
      ports
        - 3306:3306  

Step 7:
docker-compose up

No comments:

Post a Comment