BATMAN_2008
BATMAN_2008

Reputation: 3520

Start the Nodejs and Docker then pass parameter to index.js in single statement

I have written a program using the Node.js it uses the MySQL from the docker. Also, my node.js has the Index.js file which requires the input from the user based on which it will continue with different types of operation. In order to execute this, I need to run the following commands.

1. docker-compose up --build -d which will start the various containers.

2. docker-compose exec web sh -c "npm start" which will start the Node.js application.

3. one parameter provided by the user which will be taken by index.js and based on the parameter different database operation can be performed.

Now I wanted to remove these 3 different steps and merge them to make one so that users can provide just a single line query and it can be read and execution be started. Is there a way to merge these things and execute them all together.

My index.js file looks something like this:

const   express     =   require('express');
const   http        =   require("http");
const   bodyParser  =   require('body-parser');
const   app         =   express();
const   basePath    =   __dirname;
const   path        =   require('path');
const   reqPath     =   path.join(__dirname, './');
const   fs          =   require('fs');
const   port        =   process.env.PORT || 9000;
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true }));

//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){
getInput(function(data){
if(data.trim() === "Player")
{
 console.log("ONE");
}
});
    });

//function to take the input from the user and return it to calling function
function getInput(callback)
{
    var standard_input = process.stdin;
    standard_input.setEncoding('utf-8');
    standard_input.on('data', function (data) {
        callback(data);
    });
}

Here is my docker-compose.yaml file with all the services:

version: '3'

services:
  db:
    container_name: db
    image: mysql:5.7
    volumes:
      - "./data/mysql:/var/lib/mysql:rw"
    environment:
      MYSQL_DATABASE: database
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: rootuser
      MYSQL_PASSWORD: password
      DATABASE_HOST: 127.0.01
    restart: always

  web:
    container_name: web
    image: node:8
    volumes:
      - ./web:/usr/src/app
    working_dir: /usr/src/app
    depends_on:
      - db
    restart: on-failure
    command: "tail -f /dev/null"
    environment:
      project: ${project}
      type: ${type}

Upvotes: 0

Views: 1468

Answers (1)

NicolasB
NicolasB

Reputation: 1071

To avoid having to type the docker-compose exec command, you can look into defining a command or an entrypoint for that container (web) in your docker-compose.yaml file. See the Compose file documentation. This will tell your container which command to run on start-up.

Then you could for instance make your index.js accept parameters from somewhere else than user input. I would make it read environment variables instead, and bind it via docker-compose.yaml. So, Node-side, instead of getInput(), you'd use process.env.MY_INPUT_VARIABLE. And Compose-side, you could add an environment variable to your web service by using the environment: key, looking like:

services:
  web:
    environment:
      MY_INPUT_VARIABLE: ${MY_INPUT_VARIABLE_HOST}

Then run docker-compose up in a small bash script that uses its first argument ("$1") to define and to export an environment variable "MY_INPUT_VARIABLE_HOST", then runs your docker-compose up command. When docker-compose runs, it will read ${MY_INPUT_VARIABLE_HOST} and replace it with the value of the variable. It will assign it to MY_INPUT_VARIABLE inside the container, and Node will be able to read it in its process.env.

Assuming you call the bash script something like run-containers, the single command ./run-containers Player will do the same as running your docker-compose up command, followed by your docker-compose exec command, followed by entering Player when prompted by Node.

Upvotes: 1

Related Questions