Docker

Docker

ยท

6 min read

Table of contents

What is docker?

As an individual who's into tech you may have come across the term docker before, if you haven't there's no problem I will be simplifying the concept of docker.

So, what's docker?, Docker is a platform for building, running and shipping applications.

Let's take an illustrative explanation;

Take for example you are a makeup artist and as one you have your makeup kit and someone comes across your work on the Internet and contacted you for a job but on your way you discovered that you forgot your makeup kit at your studio. What to do, you are only few minutes or seconds away from your clients house so you thought to yourself that you would make use of the client's makeup tools. On getting to the client's residence you could not carryout your work effectively owing to the fact that some tools/resources you use were absent, this basically means that the environment was not conducive or it didn't meet the requirements for you to produce effective results. So what would you do to fix this?

  1. You could request that the client come over to your studio

  2. You could try not to forget your makeup set on another occasion ๐Ÿ˜‚

  3. You could as well get what we call a makeup van like the Scooby Doo mystery incorporated van but yours won't be mysterious, it would house everything you would require to function well and produce effective results.

All options aims at giving you an effective environment for functioning as a makeup artist. Now this is what docker aims to achieve in the software development world.

As a developer there's a possibility that you have asked yourself these questions: It, worked perfectly well on my machine why doesn't it work on your machine, It worked locally why isn't it working in production?.

Here are some reasons why.

  1. Missing files: This is a common issue. A standard way of pushing codes to a collaborative platform like GitHub is excluding your .ENV file which usually houses private data using a . gitignore file. When this is done anyone who clones or forks (getting a copy of your project) won't have the project's .ENV file present which leads to the application or software not functioning as intended, this means that the environment would be inadequate for functioning.

  2. Different packages: If your application works effectively with a version of node 10 and another user using node 17 clones your project there's a possibility that the project/application wouldn't run effectively because of an incompatible environment. The user might be left with a choice of downgrading his/her node.js version which would be stressful and time consuming for such a user especially if other projects he/she is working on depends on node 17, this method of solution is not recommended.

Docker aims at solving this issues by managing containers.

What are containers?

1. Containers are isolated environment for running an application independent on the environment or requirements of other applications on the same system.

  1. Containers can also be described as a running instance of an image or application.

What is an image?

An image with the context of docker can be defined as a blueprint for containers.

An image basically contains everything needed for a container to run effectively. It includes the following:

  1. Application code /source code

  2. Any dependencies

  3. Extra configurations (Environment variables)

  4. Commands

  5. Runtime environment and operating system.

  • Inorder for a container to be created an image must be present.

Before going indepth into docker below is a link for downloading the docker desktop application.

Download Docker Desktop

On visiting, scroll down to the section that reads Install Docker Desktop with Os options attached to it and follow the available steps.

Also create an account on Docker hub.

Docker Desktop helps you manage images locally while Docker hub helps manage images outside your local machine, it's commonly used when you created production based images or images you wish to share or make public.

How are Images created?

First, ensure that Docker Desktop is up and running on your system else an error will occur.

There two methods used for creating images locally.

  1. From within the docker desktop application

  2. From your terminal

I will be taking the terminal route in this article.

  1. Open up your terminal within your project directory

  2. Create a file with name DockerFile with no extension

  3. Open the DockerFile and write the following:

```
FROM node:17-alpine

WORKDIR /app

COPY . .

RUN npm install 

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

Images are mad up of layers and these layers are data being incrementally stacked or added.

Each line is known as a layer with each serving a purpose.

Line one is known as the parent layer which includes the Operating system and runtime environment. You can download Parent layers from the desktop hub website or lookup a version of the parent layer and add to your project.

FROM node:17-alpine

The line above means go to dockerhub and download a node.js parent image which uses node version 17 and runs on the alpine Linux distribution.

The first layer always houses this other layers can contain information such as source code and other required information.

WORKDIR /app

Note that a dockerfile has its own file system it doesn't use that of your PC. This line states the built up image should be stored within a directory called app.

`
COPY . .

The first . means that the source code/ resources we want to create a image for stored within the root directory of the currently opened folder, while the second . states that the image should be stored within the root of the working directory of the DockerFile within a folder named /app. If you intend on storing the image within a folder nested within /app then your text will look like:

COPY . /nested

Note: the folder can be given any name of your choice.

RUN npm install

This tells docker to install all dependencies as the image is being built.

CMD ["node", "app. Js"]

CMD is a docker keyword used for adding commands to be run.

Within the terminal of a node. js project we usually run node entryFile. Js(initial file, file to run, the file that calls everything other file with in your project, the entry point into your project).

Inorder to startup our project, in docker we simply store each word of a command with a string housed within an array separated by comma.

Another Docker keyword I would be introducing you to is the EXPOSE keyword.

FROM node:17-alpine

WORKDIR /app

COPY . .

RUN npm install 

EXPOSE 4000

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

The value immediately after the expose keyword is the port which the container will be accessed at, this value can be any port of your choice.

The build process of an image

Inorder to build an image we would run the command below:

Docker build -t imageName .

The image name can be any name of your choice given that it doesn't already exist.

The . after the image name signifies root directory which means that the DockerFile in use can be found within the root folder of the project.

ย