Wilson Tayar //@wilsontayar

Using docker to ship your app

Hey, it's been a long time since the last post. Guess it's time to clear out the dust and start from scratch.


Well, in the past few months one of the things I've been studying is docker, just to see what the buzz was all about. And it's awesome. :)

In this post I'll try to show you guys how to "dockerize" your app. And by "dockerize" I mean to put it inside a container, where you can include all your app dependencies as well, and finally start hosting it in your favourite cloud provider.


The app we'll dockerize is just a hello world written in node.js (without any dependencies for now). Also, it's answering calls on http://127.0.0.1:8000, as shown in the gist below:

  var http = require('http');

  var server = http.createServer(function (req, res) {
    res.end("Hello World");
  });

  server.listen(8000);

Now we'll create another file named "Dockerfile". This is where you tell docker your "container recipe", so it knows what to do in the build process:

  FROM ubuntu

  RUN sudo apt-get update
  RUN sudo apt-get install curl -y
  RUN curl -sL https://deb.nodesource.com/setup | sudo bash -
  RUN sudo apt-get install nodejs -y

  ADD . /src

  EXPOSE 8000
  CMD ["node", "/src/app.js"]

Let's take a closer look at it:

FROM ubuntu

Docker will pull the ubuntu repository (or use the local copy if you have it) to use as the base image, meaning that every line below will be applied to it.

RUN sudo apt-get update
RUN sudo apt-get install curl -y
RUN curl -sL https://deb.nodesource.com/setup | sudo bash -
RUN sudo apt-get install nodejs -y

RUN is used to send shell commands to the base image. In here we're telling it to update the apt-get repos, install curl, use curl to register node's source and install node.

ADD . /src

Then we'll copy all the files from your current Dockerfile's path to the image's /src. In this case we are moving our app.js to /src/app.js.

EXPOSE 8000

Tell the container to listen to port 8000, note that this is the same port that our app.js is using.

CMD ["node", "/src/app.js"]

CMD will execute a command, but it can only be used once. It's purpose is to tell the "default" way to get things running. In this line we're telling node to start our /src/app.js.

If you wish to know what else can be done in the Dockerfile or the full reference of the commands shown above, the Dockerfile reference has it all.


K, so now it's time to tell docker you'd like to build a container. If you're using Windows or Mac OS you'll need boot2docker, which is the way I'm going to show in here. If you're a linux user just follow the steps after the boot2docker commands and you'll be fine.

After installing boot2docker (if you haven't installed it already, check out docker's website to see your OS installation method), open your terminal and type:

boot2docker init

This will install boot2docker's image in your vmware, and you'll only need to run this once. After everything gets set up and the terminal starts responding again, type:

boot2docker start

This will give you some instructions on how to access docker. If you're on Windows, it will probably tell you to ssh the vm directly. If you're on Mac OS, it will give you a shell variable that you'll need to set things up. The IP address in this command is your way of accessing anything that is running inside boot2docker's vm (aka your containers). After following the instructions, let's move on:

cd path/to/your/app.js
docker build -t username/container-name .

In here, after navigating to our app's directory we tell docker to build our container. The -t means we want to tag this container as "username/container-name". The dot at the end is needed, and it's telling docker to use the current directory's Dockerfile.

And that's it, you have just built your first container!

To check it out, you can type "docker images" to get a list of all your current images.

To run it, just type:

docker run -p 49491:8000 -d username/container-name

So, with "-p" we basically mapped everything that comes in the host's 49491 port to the container's 8000 exposed port. The "-d" was used to tell docker that we want it to run in the background.

To access our app, type "boot2docker ip" to get the vm's IP address, open your favourite browser and type "INSERT_IP_HERE:49491" to see the "Hello World" message.


 

Well, that was it. Hope you guys enjoyed this as much as I did. :)

Bye,

Wilson Tayar