Dockerize — Learning Docker Series 3

Covenant Chukwudi
MyCloudSeries
Published in
4 min readFeb 21, 2019

This is a continuation of our Dockerize Series.

We have already covered some docker basics such as

  1. What is Docker itself
  2. What are containers and how do they work
  3. Container Networks
  4. The concept of Images & Image Tagging e.t.c

In this section, we are going to do something pretty cool. We are going to learn how to create our own images and publish them to the docker hub.

I also wanted to introduce us to a nice, very lightweight Linux distribution called Alpine which is just around 5mb (yes, you read that right) in size, making it considerably lighter than other Linux distributions and is often preferred to full-fledged Linux distros like Ubuntu in some docker image use cases.

With that said, let’s talk about creating our own images.

Getting Our Project Code

I’m assuming you have a working knowledge of “git” and also have it installed on your local system.

If you are new to git, no worries, take this 15minute quick tutorial and then come back to continue this series.

Open up a terminal, get into your favorite directory and clone our NodeJS code repo on Github by running this command:

git clone https://github.com/VeaSoft/docker-nodejs.git

Next, “cd” into the project directory and run “npm install”

$ cd docker-nodejs/
$ npm install

You can confirm that everything is correctly cloned by running this command in the “docker-nodejs” directory:

node index.js

Open up a browser and type “localhost:5000”, you should see a webpage with just a text on it reading “Your NodeJs App is Up And Running”.

Building Our Image

Remember our definition of Image:

“An Image consists of the App Binaries, Dependencies, Metadata about the image data and how to run the image”

Now that we have our code setup, let’s build our docker image for our project which we would use to create containers.

Create our DockerFile

Create a new file in the project root directory, name it “DockerFile”. This file would contain the set of instructions which docker will use to create and build our image.

Extend A Base Image

Extend a base image from docker hub. Custom Images are usually created by extending a base image and adding the necessary layers we need for our use cases.

A base image can be as “high level” as a prepackaged webserver e.g the official Ngnix image and can also be as “low level” as a bare Ubuntu installation. In our case, we are going to extend the official ubuntu 16.04 LTS image.

In our docker file, type the following:

FROM ubuntu:16.04RUN apt-get update

Install Nodejs & Required Linux Packages

Next, we write our instructions in the DockerFile for docker to use to setup NodeJS on our base image.

RUN apt-get install -y curl

RUN apt-get install sudo -y

RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

RUN apt-get install -y nodejs

RUN nodejs -v

Create a Project Directory

Next, we create a directory for our project in our newly extended ubuntu 16.04 image and we copy our project files from our host system to our image by simply doing this:

RUN mkdir /usr/share/app

WORKDIR /usr/share/app #cd'ing into the directory

COPY . .

Instruct To Start Our Nodejs Application

CMD nodejs index.js

In the end, your DockerFile should look like this:

FROM ubuntu:16.04

RUN apt-get update

RUN apt-get install -y curl

RUN apt-get install sudo -y

RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

RUN apt-get install -y nodejs

RUN nodejs -v

RUN mkdir /usr/share/app

WORKDIR /usr/share/app

COPY . .

CMD nodejs index.js

Build Our Docker Image From The Docker File

Now that we have our docker image description i.e our docker file, we move forward to building our image based on that description.

To achieve this, we simply run the following command:

docker build -t <whateverNameYouWantToGive> . -f DockerFile

It should take a couple of minutes to complete, I will call mine “dockernodejs”

docker build -t dockernodejs . -f DockerFile

Create A Container

We have successfully built & created our image. We now have the ability to use our image to create containers just like we would any other image.

docker container run --publish 80:5000 dockernodejs

Open up your browser and type “localhost:80”, you should see your app up and running😎.

Publishing Your Image

You can store your image in your docker-hub account, contributing to the plethora of images we have available on docker hub.

To publish your image to docker-hub, simply follow the following steps:

1) Login to your docker hub account using your terminal

$ docker login

2) rebuild your image, this time appending your account name with a forward slash.

$ docker build -t <accountname>/dockernodejs . -f DockerFile

Mine is “covenant”, so I will do this:

$ docker build -t covenant/dockernodejs . -f DockerFile

3) Finally, simply run docker push to push your new image to Docker Hub:

docker push covenant/dockernodejs

Sticky Notes

  1. Docker Images are often built by extending Base Images.
  2. A Base Image can be a high-level package e.g Nginx server or a low-level OS e.g. Linux Mint.
  3. We create definitions for our image declaratively through the use of DockerFile.

Conclusion

This brings us to the end of our Dockerize series. Hopefully, you are up and running with Docker. There are of course still a whole lot of other advanced features Docker has to offer.

Hire Me

Have an interesting project?, Great!!. Send me an email at covenantchukwudi@gmail.com

--

--

Covenant Chukwudi
MyCloudSeries

I build products that would have positive effect on lives