6

New to docker. I wanted to create a simple Node.js app using docker on my Ubuntu OS. But all the tutorials/YouTube Videos are first installing Node on host machine, then run and test the app, and then dockerizing the app. Means all the tutorials are simply teaching "How to dockerize an existing app".

If anyone can provide a little guidance on this then it would be really helpful.

6
  • 4
    This can certainly be done, but you are going to encounter friction and headaches for essentially zero benefit. Just install node.js on the host machine. I mean, its ubuntu: you're a sudo apt remove away from changing your mind later. Commented May 23, 2022 at 12:02
  • 1
    I don't totally understand this requirement. You will almost certainly need a browser installed on the host machine, and a desktop environment, and an IDE, and Node is very easy to install. Why introduce an isolation environment in the middle of your development flow? Commented May 23, 2022 at 13:21
  • 1
    @JaredSmith yes I am a sudo apt remove away from changing my mind after collecting some more knowledge on Docker. Thank you for commenting Commented May 23, 2022 at 14:34
  • 1
    @DavidMaze I am just learning it, and I want to do it without taking help from the host machine to understand it better. I know I will have to take help from host machine in future. Thank you for commenting Commented May 23, 2022 at 14:35
  • 1
    It's like the whole idea of "containerization" is lost in this comment thread. The whole point of containerizing is that the entire app and ALLLL dependencies are in the container. What if you have one project that needs npm 3, and one that needs npm 8 and you need both apps to run at the same time? Then what do you do with your host machine? Commented Aug 29, 2022 at 12:15

2 Answers 2

4

First create a directory for your source code and create something like the starter app from here in app.js. You have to make one change though: hostname has to be 0.0.0.0 rather than 127.0.0.1.

Then run an interactive node container using this command

docker run -it --rm -v $(pwd):/app -p 3000:3000 node /bin/bash

This container has your host directory mapped to /app, so if you do

cd /app
ls -al

you should see your app.js file.

Now you can run it using

node app.js

If you then switch back to the host, open a browser and go to http://localhost:3000/ you should get a 'Hello world' response.

Sign up to request clarification or add additional context in comments.

Comments

1

Follow this


# 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 ./

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

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]
docker build . -t <your username>/node-web-app
docker run -p 49160:8080 -d <your username>/node-web-app

You can create package*.json ./ files manually, if you don't want to install node/npm on your local machine.

3 Comments

1. We don't allow link only answers and 2. The second step in that tutorial is running npm install on the host machine which requires node.js and is exactly what the OP is trying to avoid.
That step can be skipped because the Dockerfile itself has npm install. I didn't put this link here to promote. I've no connection whatsoever with this link, I just posted it to help.
I wasn't accusing you of anything, that's official node documentation even if you worked at Joyent it wouldn't be a conflict of interest. The issue is that stack overflow (for very good reasons) does not allow link-only answers. If you copy enough of the relevant details from the linked docs as well as an explanation of how to circumvent the steps in the actual tutorial to achieve the OPs aim I'll remove my downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.