1

I am creating a console-based app in Node JS. No ports are being used... It's just a basic app. The problem I am having is using Docker to run my app.

Here's my docker file:

FROM node:16

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

CMD [ "node", "index.js" ]

After building the image, I try to run 'docker run ' and the container just exits right away. However, when i use the Docker desktop app, after building the image, if I click 'run' it builds the container and does not exit, but actually runs. When I log into that container, I am able to run 'node index.js' and my node app runs fine.

My question is, why does it work when using the GUI docker run and not my command? Also, why do I have to run 'node index.js' manually when the docker file should take care of that?

Commands to build image and run the container:

Image build:

docker build -t 'appname' .

Create container:

docker run 'appname'
Phil
  • 157,677
  • 23
  • 242
  • 245
Red
  • 65
  • 8
  • Yea, i have that – Red Jul 06 '22 at 03:53
  • Lol, i am sure. I had '<'appname'>' and it had that is hidden – Red Jul 06 '22 at 03:55
  • I can't reproduce this. I created an `index.js` file with `console.log("Running node app", process.version);`, ran `npm init -y` to create a `package.json` then ran `docker build -t so-72877737 .` then `docker run --rm so-72877737` and got _"Running node app v16.15.1"_ – Phil Jul 06 '22 at 03:56
  • @dallgood, after starting docker with run command, did you check ```docker container ps``` and verified the status ? Try connecting to container with ```docker exec -it bash``` command – gvmani Jul 06 '22 at 04:05
  • The same setup works for me as well, but for some reason, the app in question will not work with docker run 'appname'. The application runs fine locally though and runs fine when creating the container via the Docker desktop app, but it behaves in this werid way. – Red Jul 06 '22 at 04:05
  • 1
    Is your app waiting for some input? Try `docker run -it --rm appname`. That way `stdin` and `stderr` will forward to your terminal – Phil Jul 06 '22 at 04:06
  • That works. What does that command do? – Red Jul 06 '22 at 04:08
  • I guess that makes sense... since I am prompting the user for input – Red Jul 06 '22 at 04:10
  • The docker CLI is [well documented](https://docs.docker.com/engine/reference/run/#foreground) – Phil Jul 06 '22 at 04:10
  • Does this answer your question? [Intercept Keyboard Key presses into a docker container](https://stackoverflow.com/a/58243614/283366) – Phil Jul 06 '22 at 04:12

0 Answers0