Docker — Create a Node Microservice — Part 1

This super quick tutorial is to bootstrap a simple node express microservice using docker containers. I will list only the docker commands with only the relevant explanations. So… short and sweet…

Overall plan:

  1. Create a container wth ubuntu image
  2. install node & npm on the image
  3. Create a base image using commit
  4. Create a microservice API using Express
  5. Create a base image with Ubuntu, Node & Express service
  6. Test it out

1. Create ubuntu container

host$> docker run -i -t --name ubuntu-container ubuntu /bin/bash
this will create the container and you will on the bash shell @ root

options: -i (stdin), -t (tty)

2. Install node and npm within the container

Within the container you can enter the following commands. This will update ubuntu with the latest patches and then install node & npm

container$> apt-get update
container$> apt-get install --yes nodejs
container$> apt-get install --yes nodejs-legacy
container$> apt-get install --yes npm
container$> node --version

3. Create a base node image

We can use the docker commit command to create a new base image of the currently created container.

We need to get the container id using “docker ps” command.

# get the first letters of the container id
host$> docker ps
37c0e3afb8a0 ubuntu "/bin/bash"
# -a = author, 37c0 = containerID, base-image-name
host$> docker commit -a "sunkay" 37c0 ubuntu-node:0.1
host$> docker images
ubuntu-node 0.1 cc7e05648562
ubuntu latest 97434d46f197

We can see that 2 images are now created. The first one is our base image with node installed within the container.

4. Create an express based microservice API

Install express-generator which will help in bootstrapping an express service in no time

host$> npm install -g express-generator
host$> express hello-microsvc
create : hello-microsvc
create : hello-microsvc/package.json
create : hello-microsvc/app.js
create : hello-microsvc/public
create : hello-microsvc/public/javascripts
create : hello-microsvc/public/images
create : hello-microsvc/public/stylesheets
create : hello-microsvc/public/stylesheets/style.css
create : hello-microsvc/routes
create : hello-microsvc/routes/index.js
create : hello-microsvc/routes/users.js
create : hello-microsvc/views
create : hello-microsvc/views/index.jade
create : hello-microsvc/views/layout.jade
create : hello-microsvc/views/error.jade
create : hello-microsvc/bin
create : hello-microsvc/bin/www
install dependencies:
$ cd hello-microsvc && npm install
run the app:
$ DEBUG=hello-microsvc:* npm start

Now, add a new route ‘/api’ to the express server.

  • Add a new route file called api.js, copy the users route as an example
  • Run it locally to make sure it is workin

5. Create a base image with Ubuntu, Node & Express service

Now that we have an express microservice running locally, we need to add this to the container, make sure it is working and then create a base image using docker commit. There is a easy way to do this using Dockerfile and docker-compose, but this is all about doing it via the command line.

host$> docker run -it -v <cwd>:/host -p 3000:3000 ubuntu-node:0.1 /bin/bash
# you are now in the container
container$> cp -r /host microservice
container$> cd microservices;npm start
# TEST the service is running from within the docker container

create a new base image with the microservice and you can publish it over to docker hub

6. Run the service in the background

host$> docker run -d -w /microservice -p 3000:3000 microsvc:0.1 npm start

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.