Welcome to weblogs.com.pk Sign in | Join | Help


Dockerizing Node

Docker can build images automatically by reading the instructions from a Dockerfile. Its a text file that contains the commands how to assemble the required image. This can be used as a replacement of manually creating an image from scratch installing required software etc and then exporting and loading it someplace else; the technique we discussed in the first Docker post here. We can simply handover the Dockerfile instead. Lets create a Node Container using the Dockerfile for that simple Hello World thing! Create a Dockerfile; and punch in the following

FROM ubuntu
MAINTAINER Khurram <khuziz@hotmail.com>

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y nodejs
RUN apt-get install -y build-essential
RUN apt-get install -y npm

ADD hello.js /app/hello.js


CMD ["nodejs", "hello.js"]

  • Using FROM; we are using ubuntu base images; there are many to choose from at Docker Hub / Registry
  • Using RUN; we are giving commands that needs to run to setup the required things in the Container
  • Using ADD; we are adding the application file(s) into the Container; we use ADD and COPY for this
  • Using EXPOSE; we are telling which ports will get exposed; when the container will run using –P; it will expose this port and map to some random available port on the Docker Machine
  • WORKDIR sets the directory for subsequent RUN, CMD, ADD/COPY and ENTRYPOINT etc
  • Using CMD; we are running the NODEJS command to run our application

Once the Dockerfile is in place; we can “compile” it and build the container using docker build

>docker build –t khurram/node:hello .

  • Using –t we are specifying the tag name of the image that will get created
  • The last dot is the context; the directory; where docker build will run; it will look for Dockerfile there (and some other files if we create like .dockerignore etc) and run/compile it from the specified context


After a while; our image will get created that we can check using docker images and can run it using docker run

C:\khurram\src\Staging>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
khurram/node        hello               b35d15d98edb        2 minutes ago       460 MB
microsoft/dotnet    latest              098162c455c7        11 days ago         576 MB
ubuntu              latest              2fa927b5cdd3        2 weeks ago         122 MB

C:\khurram\src\Staging>docker run -d -p 3000:3000 khurram/node:hello

C:\khurram\src\Staging>docker-machine ls
NAME          ACTIVE   DRIVER   STATE     URL                        SWARM   DOCKER    ERRORS
Boot2Docker   *        hyperv   Running   tcp://           v1.11.2

Hello World from Node in Container
C:\khurram\src\Staging>docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES
ecebef464989        khurram/node:hello   "nodejs hello.js"   3 minutes ago       Up 3 minutes>3000/tcp   hopeful_leakey

Tips and Hacks

  • Just like HTML; the best way to learn Dockerfile tricks is to read others; for instance the Node’s official Dockerfile; you will learn that instead of ubuntu image they are using buildpack-deps:jessie base image which is more lean and result better Container
  • Having RUN command in separate lines result better cache; the layers that gets created can get reused across different images in a better way; for instance having apt-get update in its own line and as a first line will result its own layer and if we create another image for something else; say MongoDB; it will get reused
  • Having meaningful tags for the images are useful determining what’s what in long run
  • There exists CURL for Windows; you can download and place in some folder which is in PATH and use it similar to how you use it in Linux
  • You can get prebuilt “docker.exe” (Docker CLI) on Windows three ways; through Chocolatey, through Docker Toolbox or from Docker Toolbox’s repository. Docker Toolbox uses Docker to build it; from Toolbox’s Windows Dockerfile you can find out where precompiled docker files are; look for RUN curl lines with entry –o dockerbins.zip; you can make a URL and using CURL for Windows easily download that zip file and find the latest docker.exe in it
  • As we are using Boot2Docker VM for the Docker; running the container and exposing its port; expose them to the VM level; if we want to expose it further to the gues OS level; we need to forward VM's port; topic of next post may be!



Published Tuesday, June 14, 2016 3:04 PM by khurram
Filed under: , ,


No Comments

New Comments to this post are disabled