How can I use the docker-compose
to create container and pass the parameter to my Node.js application during the start?
I am trying to write a program where I can connect MYSQL using the NODEJS and using the DOCKER-COMPOSE. I need to run the application in the terminal so that I can pass some parameters to my Node.js application during the start hence I need to run the DOCKER-COMPOSE UP to bring up the application and MYSQL then I want to run the application using the CLI in another window.
Here is my index.js
file present within web
folder which will take the input from the user when I run with following command nodemon start New
.
//Make NodeJS to Listen to a particular Port in Localhost
const port = 9010;
app.listen(port, function(){
// Start the server and read the parameter passed by User
console.log("Node js is Running on : "+port);
// Get process.stdin as the standard input object.
var standard_input = process.stdin;
// Set input character encoding.
standard_input.setEncoding('utf-8');
// Prompt user to input data in console.
console.log("Please input text in command line.");
// When user input data and click enter key.
standard_input.on('data', function (data) {
console.log(" DATA ENTERED BY USER IS :"+data);
});
});
I want to achieve this using the docker-compose
hence I have the following code for docker-compose.yml
file.
version: '3'
services:
db:
build: ./db
environment:
MYSQL_DATABASE: mydb
MYSQL_ROOT_PASSWORD: mypass
MYSQL_USER: mysql
MYSQL_PASSWORD: mypass
DATABASE_HOST: myhost
web:
build: ./web
depends_on:
- db
restart: on-failure
ports:
- "9010:9010"
adminer:
image: adminer
restart: always
ports:
- "7778:8080"
My Dockerfile
for the web or Node.js within web
folder is:
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9010
CMD ["npm","start"]
My Dockerfile
for the database MYSQL
within db
folder is:
FROM mysql:5.7
EXPOSE 3306
COPY ./scripts /docker-entrypoint-initdb.d/
Now when I run the command docker-compose up --build
for the docker-compose.yml
file then everything works fine; the container and Node.js application gets started.
When I open another terminal window and try to navigate to web
container using docker-compose exec web sh
and start the node application to pass the parameters nodemon start New
then I get the following error.
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::9010
at Server.setupListenHandle [as _listen2] (net.js:1360:14)
Since the Nodejs application is already running when I ran the command docker-compose up --build
the port is already asssigned so I am getting this error how can I start the Indey.js application and pass parameters to it?
I am using Ubuntu Os for the application.
The reason is that there is already running nodejs application that run from CMD
and occupied the port, so when run command inside docker you get the EDDRINUSE
error.
You will multiple options,
CMD ["npm","start"]
#change the above cmd to
CMD tail -f /dev/null
This will just keep your nodejs container running and then run the command inside the container.
docker-compose exec web sh
nodemon start New
Update the nodejs code a bit
//Make NodeJS to Listen to a particular Port in Localhost
const port = process.env.PORT;
Now start the process with different port
PORT=9011 nodemon start New
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.