Friday, May 1, 2020

Docker Assignments #12: Node.js, MySQL, Express, Nodemon and Dockerfile

nodemon is used to develop node.js based applications by automatically restarting the node application when file changes in the directory.


Create a folder structure as per below image in the host machine
  1. Pull mysql:5.7 docker image into host machine
  2. Write docker run command by setting following options
  • enable detached mode
  • set container name as "testmysql"
  • map host port number to 3500
  • set MYSQL_ROOT_PASSWORD as “mysql123”
      3. Check testmysql container up and running
       4. Enter container using docker exec
       5. Connect to database using mysql CLI client with root user
       6. Create database with name “hr”
       7. Create table with name employee and “id, name” columns under “hr” database
       8. Insert sample data into employee table
       9. Check “HR” database and “Employee” table with data
      10. Add "mysql" and "express" npm packages as a dependency in the package.json file
      11. Add "nodemon" npm package as a dev dependency in the package.json file
      12. configure npm start command as "nodemon -L index.js" in package.json
      13. Write required code into index.js file to connect "testmysql" container and expose endpoint using      express.js
      14. 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
    15. Build docker image with name "testnodemysqlimg"
    16. Write Docker run command by doing bindmount to host directory and linking "testmysql" container

Step 1: 
     > docker pull mysql:5.7
Step 2: 
     > docker run -d -p 3500:3306 -e MYSQL_ROOT_PASSWORD=mysql123 --name testmysql mysql:5.7
Step 3: 
     > docker ps
Step 4: 
     > docker exec -it testmysql mysql -u root -p

Step 6: 
     mysql> create database hr;
     mysql> use hr;
     mysql> create table employee(id int,name varchar(10));
     mysql> insert into employee values(1,'rama');
     mysql> select * from employee;

Step 10: 
     > npm install mysql
     > npm install express

Step 11: 
     > npm install -D nodemon

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

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

app.get('/mysql'function (reqres) {
    var con = mysql.createConnection({
        host"db"
        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 14:
FROM mhart/alpine-node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9030
CMD ["npm""run""start"]

Step 15:
docker build -t testnodemysqlimg .

Step 16:
  • Create volume called "nodeexpressvol" by using 
          >docker create volume nodeexpressvol

>docker run
-d
-p 9030:9030
  --link testmysql:db 
  --mount type=bind,source=/c/Users/rmukkama/node-express,target=/usr/app 
  --mount type=volume,source=nodeexpressvol,target=/usr/app/node_modules
  testnodemysqlimg

We are including two types of mounts here:

The first is a bind mount that mounts our application code on the host to the /usr/app directory on the container.
This will facilitate rapid development, since any changes you make to your host code will be populated immediately in the container.

The second is a named volume, "nodeexpressvol". When Docker runs the npm install instruction listed in the application Dockerfile, npm will create a new node_modules directory on the container that includes the packages required to run the application.
The bind mount we just created will hide this newly created node_modules directory,
however. Since node_modules on the host is empty, the bind will map an empty directory to the container, overriding the new node_modules directory and preventing our application from starting.
The named "nodeexpressvol" volume solves this problem by persisting the contents of the /usr/app/app/node_modules directory and mounting it to the container, hiding the bind.


Happy Coding :)

No comments:

Post a Comment