skip to Main Content

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.

2

Answers


  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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search