When I run the following simple Node.js code (index.js) using Docker, the code executes as expected - waiting for user input. But when I run the same code using Docker-Compose, Node.js exits before waiting for user input.
I've been able to run the Node application using the Dockerfile, but not Docker-Compose
Here is the index.js file that I'm able to run in the Dockerfile (but not Docker-Compose):
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question(`What's your name? `, (name) => {
console.log(`Hi ${name}!`)
readline.close()
});
And the Dockerfile I used:
FROM node:12.2
ENV NODE_ENV=development
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /app
COPY package*.json /app/
RUN npm install && npm cache clean --force
WORKDIR /app/src
CMD ["node", "index.js"]
As with the Dockerfile experience:
```shell
$ docker build -t commandline .
...
$ docker run -it --rm -v $(pwd)/src:/app/src commandline
What's your name? Scott
Hi Scott!
I expected the Docker-Compose version to execute the same way, but it exits the Node and Docker process before the user has an opportunity to respond:
$ docker-compose up
Creating mega_node_1 ... done
Attaching to mega_node_1
node_1 | What's your name? mega_node_1 exited with code 0
$