Published on

How to Dockerize a Service

Authors

Putting your App on Docker

For this tutorial, we will be dockersizing a very basic nodejs application, this will serve as intro for the commands and concepts necessary when onboarding any app/service onto Docker.

Nodejs App

Below is basically a hello world nodejs app using express as the only dependency. You create a package.json install express and then just a simple start script like node server.js that will clear out all the ground work to get this code running.

server.js
const express = require("express");
const app = express();
const port = 8080;

app.get("/", (req, res) => {
  res.send("Yelloooo! ");
});

app.listen(port, () => {
  console.log(`listening on port ${port}`);
});

After you have the app running, you should be able to hit localhost:8080 and get the Yelloooo!

Onboarding to Docker

Now that we have our application running, we can start by creatin a Dockerfile and start defining our configuration.

Let's start by getting our base image, we know that we need node for this application we can look in the Dockerhub to see if there is any official node images and in our case there is node:alpine... More details on apline below.

Dockerfile
FROM node:alpine #base image

then we need to move our application to inside the container and install it's dependencies... to do that we first need to def a working directory, Docker has a step for that and it's WORKDIR in our case the working directiory will be as follows

Dockerfile
FROM node:alpine

WORKDIR /usr/app # where we will be moving our application to inside the container

After we have our working directory defined, we need to move the application source code over to the container... to do that we use the COPY src dest step. As notes this takes a source and a destintation. Hopefully you create the Dockerfile in the root directory of your application folder if so, then you can use ./ for the source. For the destination, since we defined our working directiory, Docker will treat this working directory as our root directory from now on so we can use ./ for the dest as well.

Dockerfile
FROM node:alpine

WORKDIR /usr/app

COPY ./ ./

Now we have our application inside the container now we need to install it's dependencies and/or other system dependencies needed to get it running. I like using yarn so I'll add yarn to this image and then install dep. After that then we just define the starting point of our application.

Dockerfile
FROM node:alpine #baseImage

WORKDIR /usr/app #workingDir which will act as the new root for any future copies

COPY ./ ./ #move source code into the container
RUN apk add --update yarn #install sys dep
RUN yarn # install app dep aka yarn install

CMD ["yarn","start"] # Define the command that will start our app

Now if you do docker build -t yourname/basicExample:latest . then you should get a sucessful build and can run the build and try to hit localhost:8080

...

you will notice that your request hangs or it doesn't respond. That is because we haven't defined any open ports in our container so we can start receving requests. We need to port forward all incoming requests from localhost:8080 to a port in our container... to that we just need to run the container with a port specification and that can be done as follows

docker run -p incomingPortNum:portNumAppIsListeningToInTheContainer <CONTAINER_ID/TAG>

following this spec, we end up with this command

docker run -p 8080:8080 md/basic

Wrap Up

Now you should be able to hit your app locally on 8080 and you should see the Yellooo response.

Docker Terms to know

  • Alpine: Alpine is a common base image that's found on Docker Hub; however, in the Docker Ecosystem, Alpine is supposed to be the smallest size and most compact version of a Docker Image. Other images can have 'alpine' versions, noting that's a stripped down version of the image i.e, node:alpine