Search code examples
dockerboot2docker

deploying nodejs on docker


I am trying my hands on deploying a sample node js application to docker.

I am following the tutorial from http://oskarhane.com/create-a-nodejs-docker-io-image/.

In the tutorial , there is step 4: where the author asks to modify the app.js with the code and place in /app/www/app.js But my question is when I do winscp , I could not find the folder /www in /var . I am literally stuck at this place .

Can anybody tell me how to modify the js file app.js with the server code ? Or any simpler step by step appraoch is there for deploying any apps on ubuntu 12.04 docker please let me know.


Solution

  • The author creates /var/www in step 2:

    mkdir /var/www && echo 'console.log("Hello from Node.js");' > /var/www/app.js
    

    He commits it to an image in step 3. So the image you get after step 3 should container /var/www:

    docker commit 0a7e9dd8dbdd NodeBase
    

    So the image NodeBase will contain a folder /var/www. If not, you made a mistake in step 2 or 3.

    However, I think this is a strange tutorial. Building the container in a interactive shell isn't very advisable. Instead I would use a Dockerfile to build your image. Do you know how to install Node.js on (let's say) Ubuntu? If you do, you can do the very same in a Dockerfile:

    FROM ubuntu
    RUN apt-get -y install nodejs
    ADD /your-app/ /var/www/your-app
    CMD run-node-here
    

    Then build the image by docker build -t yourImage . and run it.

    Node.js is very popular, so you have good luck to find an already prepared Node.js image. You could go to https://index.docker.io and just search for a suitable Node.js image. The very first one you will find will be https://index.docker.io/u/dockerfile/nodejs/. You can just use this image and mount your own application to it:

    docker pull dockerfile/nodejs
    docker run -it -v /your/node/app:/data:rw --rm dockerfile/nodejs node 
    

    Note that I have no experience with Node.js, just with Docker. So I haven't tested this stuff, I just give you a general advise how to do it. If you don't want to bother with installing Node.js, search an image where you can mount your app and start Node.js (maybe the one mentioned above). Otherwise, write your own Dockerfile and install Node.js just as you would do it on Linux.