NodeJS : Dockerizing a Node.js app

21 Sep 2015

I was working for a long time to dockernize my applications, but never happened. But today after I working on a UDP server in NodeJS I dockernized it without much hassle.

I think I achieved this easily because it doesn’t have any DB or any other dependencies. So I setup my docker file to setup and install node on a ubuntu.

# Dockerfile

FROM ubuntu
RUN apt-get install -y wget make gcc
RUN wget http://nodejs.org/dist/v4.1.0/node-v4.1.0-linux-x64.tar.gz
RUN tar -zxf node-v4.1.0-linux-x64.tar.gz

So now I setup my Dockerfile to install nodejs. Now I need to install the depndencies, copy my sourcecode, port forwarding and running application. So I updated my Dockerfile to

# Dockerfile

FROM ubuntu
RUN apt-get install -y wget make gcc
RUN wget http://nodejs.org/dist/v4.1.0/node-v4.1.0-linux-x64.tar.gz
RUN tar -zxf node-v4.1.0-linux-x64.tar.gz

COPY . /src
RUN cd /src; /node-v4.1.0-linux-x64/bin/npm install
EXPOSE 9030
EXPOSE 3000
CMD ["/node-v4.1.0-linux-x64/bin/node", "/src/index.js"]

then I build the docker image.

docker build -t revathskumar/ubuntu-node .

Now I can run the docker container using command

docker run -p 9030:9030 -p 3000:3000/udp -d revathskumar/ubuntu-node

Since docker have docker-compose now, I added docker-compose.yml so that I can start my container easily.

# docker-compose.yml

notifier:
  build: .
  ports: 
    - "3000:3000/udp"
    - "9030:9030"
  volumes:
    - ".:/code"

Now instead of long docker run command I can use

docker-compose up

docker-compose is helpful when you need more than one container for your application. Thats it. Now I run my notifier application on docker.

If you find my work helpful, You can buy me a coffee.