0

I'm taking my first steps with Node.js and wrap my head around Docker and following the guide here https://www.docker.com/blog/getting-started-with-docker-using-node-jspart-i/ I successfully create the Docker image as logged on terminal

vinnytwice@Vinnys-iMac fixit_server_node % docker images          
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
node-docker   latest    483e8ea7d5e8   2 minutes ago   950MB
node          15.14.0   3d3f41722daf   3 days ago      936MB

but then the I try to run it with docker run node-docker as the guide says

After running this command you’ll notice that you were not returned to the command prompt

but nothing happens and instead I'm indeed returned to the prompt..

vinnytwice@Vinnys-iMac fixit_server_node % docker run node-docker
vinnytwice@Vinnys-iMac fixit_server_node % 

I suppose there is something wrong in my Dockerfileas I'm using my own app and not cloning the one from the guide. Here is my Dockerfile:

FROM node:15.14.0

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json package.json
COPY package-lock.json package-lock.json

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

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

As when I start my app I use nodemon server.js I tried changing to CMD [ "nodemon", "server.js" ] but still the same result..

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
  • Have you tried to `brew install node` outside Docker, and then directly run `node index.js` on the host? (Using this environment might be easier than trying to learn Node _and_ Docker at the same time.) What's in the `index.js` file? – David Maze May 15 '21 at 22:18
  • Indeed I used brew to install Node and Docker, I see them in my Cellar folder. When I start my app I can either use `npm start` or `node server.js` or `nodemon server.js`, `node index.js`does nothing.. – Vincenzo May 15 '21 at 22:25

1 Answers1

0

You should list the running processes to see if it is indeed running.

Unless I'm reading bad, this seems to be just a correct behavior, you're starting a docker container, not asking to get inside it.

docker container ls

This will show the running processes. More info here: https://docs.docker.com/engine/reference/commandline/container_ls/

Once you start a container, you can connect into it, like in a SSH connection.

docker exec -it <container name> /bin/bash

Next time you can try to start the container and connect at the same time:

 docker run -it --name <container name>
jacksonbenete
  • 159
  • 1
  • 9
  • It should be unusual to need `docker exec` to "connect to it"; you wouldn't get a shell inside the same Node process running outside a container. – David Maze May 15 '21 at 22:16
  • `docker container ls` shows no containers in terminal – Vincenzo May 15 '21 at 22:26
  • @Vincenzo try to do a `docker run -d --name my-container node-docker`, it will be a detached container (-d) and now you have a name (--name my-container) so it should be easier to find and reference it. Of course you will learn all those things in the same tutorial you're following, but after creating a detached container try another "ls" to see if something shows up. – jacksonbenete May 16 '21 at 00:18
  • @DavidMaze thanks for your comment, you're probably right. I'm trying to help about the "docker part" but I'm not really familiar with Node. Since there is no "node" tag, I didn't paid attention to this possibility as I'm not really aware about Node details. – jacksonbenete May 16 '21 at 00:22
  • @jacksonbenete I addended the node tag, thanks for pointing it out. I run `docker run -d --name my-container node-docker` but the result is the same, listing containers with `docker container ls` lists none as well as `docker ps`, but `docker ps -a` does show containers.. the two I created and the newly detached my-container: `6a5e3b4183dd node-docker "docker-entrypoint.s…" About a minute ago Exited (0) About a minute ago my-container`. This shows that the container exits as soon as it's run right?? – Vincenzo May 16 '21 at 05:03
  • And found where the problem is https://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d even if in the tutorial is not having this issue applying the accepted solution `docker run -d --name my-container2 node-docker tail -f /dev/null` makes the container stay running as the container is run on the foreground.. – Vincenzo May 16 '21 at 05:26