Dockerize you projects for faster setup and development — Part 1

Ishtiaque Zafar
6 min readMar 16, 2020

--

Overcome your set-up barriers and unlock your speed. Photo by Alyssa Ledesma on Unsplash

I love exploring new technologies and I feel I haven’t learnt it unless I have played with it for a long time, got my hands really dirty with it and experimented with a lot of cases.

My problem is:

Every time I start a new project, I have to install a lot of stuff. Database, libraries, tools, etc. Conflicts arise if I have older versions of these stuff installed on my computer. I want to work with the latest version, so I have to upgrade them. This comes with a risk of breaking my older projects which might not work anymore with the upgraded libraries/tools.

If you have been in the software development world, you know how frequent this is and you know how I feel, and maybe, it’s the reason you are reading this.

The solution: Containerize

In this article, I will explain how to use docker to containerize your project, allowing you to focus on your primary goal: LEARNING, and not get derailed by these trivial “set-up” hassles. Containerizing gives you a blank canvas to work with, an isolated environment. You don’t have to worry about conflicts, upgrading stuff, etc. Plus, there is an excellent community which might provide you with images which have everything installed and ready-to-go. Pull the image, spin up a container and start learning.

This can be applied to any technology (mostly), but I will be using nodejs for this article. We will be creating a simple API server based on nodejs and express. You will see that you don’t have to install nodejs on your system to get it running.

Another benefit you get out of this is easy cleanup. When you decide you no longer need the resources, just stop the container, remove it and remove the container image as well. That’s it.

This article is part of a series where I’ll be getting a full-fledged nodejs REST API server containerized, up and running in minimal time as possible.

Part 1: This article
Part 2: https://medium.com/@ishtiaque/dockerize-you-projects-for-faster-setup-and-development-part-2-6ebe9ba2e7e2

Before we start,

You need to have docker installed on you system. A working knowledge is not required as I will be explaining all the stuff, but I will suggest you become familiar with docker terms over here.

Lets begin:

Step 1:

Create your project folder and open it in your favourite IDE

# Create the project folder
mkdir docker-node-api
# Go inside the project folder you just created
cd docker-node-api

Step 2:

Initialise the project with npm init

npm init

Follow the prompts given by the cli. It will look something like:

By the end of this step, you will have a package.json file created in your root folder that looks something like this:

initial package.json file

Step 3:

Install the required packages

npm install express body-parser cors

Step 4:

Create server.js file in your root folder and paste the following contents into it.

Now that we have the code ready for the API server to function, we need to run it and see whether everything is working properly or not. We need nodejs to execute the code, but due to my pain points mentioned earlier, I don’t want to install nodejs natively on my system. In the future, I might plan to install a database. In that case as well, I will not want to install it natively on my machine. So, how do I get those things without installing them? The answer is containers. Think of containers as an isolated environment, completely aloof from your system’s environment. Docker is the most popular containerization tool available. At this point, I’m assuming you have docker installed, up and running.

Step 5:

Create a Dockerfile in your root folder. Put in the following contents

Dockerfile is nothing but a text file which tells Docker how to build an image. Let me explain the contents in the dockerfile:

  • The first line FROM node:10 tells Docker to use node as the base image. This image a pre-built image in which nodejs, version 10.x.x (the current LTS version) is already installed. We will build our image on top of this image.
  • WORKDIR /usr/src/app Sets the current working directory to /usr/src/app
  • COPY command is used to copy files from our host machine to the container
  • RUN command is execute a command in the container’s shell. Here we execute npm install to install all our dependencies
  • COPY . . Copy all the contents from the host’s current working directory (your project folder) to the container’s current working directory (in this case usr/src/app )
  • EXPOSE 8001 Exposes the port 8001 to our host machine. This port is now accessible from our system.
  • CMD This also used to execute a command. We execute node server.js to get our express app up and running.

Let’s create a .dockerignore file as well. This file tells docker to ignore the stuff mentioned in this file while building.

Now, we have everything ready. Let’s build our docker image:

Step 6:

Build docker image

docker build -t izafar/docker-node-api .

This command tells docker to search for the dockerfile from the current directory (notice the . at the end) and build with name izafar/docker-node-apiand tags it for versioning ( the -t ).

You can run the command docker images in your terminal to see whether the image was created. It will show up something like this:

Step 7:

Run a container based on the image we just created.

docker run -p 8001:8001 -d izafar/docker-node-api

This runs a container in detached mode, maps the host port 8001 with container’s 8001 port. You can run the command docker ps to list all the running containers and you should see your new container listed over there.

Time to test our API server. Open your browser and point it to localhost:8001/api/v1 .

If you see something like this, it means you have successfully set up your node API server inside a docker container and are able to access it from your system.

To see the logs from your app:

# Get container ID
$ docker ps

# Print app output
$ docker logs <container_id>

# Sample logs will look something like
API server up and running on port 8001

You might even need to log into the container’s terminal to execute some commands manually. You can use the exec command:

# Enter the container
$ docker exec -it <container id> /bin/bash

Major Pain:

If you have reached till here, good, but that’s not where our problems end. This set-up has a major flaw: Every time you make changes to your code, you have to stop the container, build it again and then restart it to see your changes deployed. This is a major pain-in-the-ass and you see that this kind of set-up is not ideal for development. In my next tutorial, we will see how we can overcome this hurdle and make development easier.

Next: Let’s resolve this pain

--

--

Ishtiaque Zafar

Founding Engineer @ Skyflow. Using my tech powers to solve real-life problems. Dreamer, doer, go-getter.