Geek Culture
Published in

Geek Culture

Docker Tutorial for Beginners

I welcome you and hope you are doing well.

What is Docker?

A platform for building, running, and shipping applications in a consistent manner.

But why Docker? we can use our development machine to run the application.

Okay, Let’s assume you built the application and exported the source code to the client but now he is not able to run it on his machine. Probably due to software version mismatch or different configurations. And this is where Docker comes to the rescue.

We can easily package up our application with the respective requirements and run it anywhere, on any machine with Docker.

Configure once and run it on any machine where the configuration part will be then handled with the Docker and you are not required to configure it again and again for different machines.

Containers

When the dependencies are installed it runs under an isolated environment called containers. This isolated environment allows multiple applications to use different versions of any software side by side.

For example, if there are two applications both are using Node but with different versions. They can run side by side in an isolated environment without messing with each other.

Virtual Machines

A virtual machine is an emulation of a machine/physical hardware. We can run several virtual machines on a real physical machine using a hypervisor.

Virtual Machine

A hypervisor is a tool that we used to create and manage virtual machines. There are many hypervisors available like VirtualBox and VMware which are cross platforms.

It helps us to run applications in Isolation in VM and on the same physical machine we can have another VM which is running a completely different application within it.

So, why don’t we use VM’s instead of containers?

Virtual Machines vs Containers

Because virtual machines come with some problems. Let’s discuss them

  1. Each virtual machine needs a full copy of the operating system.
  2. It is slow as it mimics our computer.
  3. Each virtual machine utilizes the actual physical hardware resources.

Let’s talk about containers

  1. Containers also provide us the same kind of isolation.
  2. They are lightweight and don’t need a full operating system.
  3. It utilizes the operating system of the host.
  4. As the operating system has already started on the host a container start’s up quickly.
  5. Also, it does not utilize any hardware resources like CPU, memory, etc. Due to which we can run a lot of containers side by side based on requirements.

Docker Architecture

Docker uses a client-server architecture. Where a client component talks with the server component using a rest-full API, over UNIX sockets, or a network interface.

Docker Engine
Docker Engine

All containers on the host share the kernel of the host with respect to the operating system. For example, Linux containers run on the Linux operating system, Windows containers run on the Windows operating system, and so on.

Installing Docker

visit https://docs.docker.com/get-docker/ and follow the respective guide to install Docker on the respective platform i.e. Windows, Linux, or MAC. But before installing check the system requirements.

Once the Docker is installed. We have to verify it, for that run the following command. This command will list all the options available for the docker that you can use.

$ docker

In case, you see the following error or something similar.

'docker' is not recognized as an internal or external command,
operable program or batch file.

please ensure that you have followed installation steps correctly and then proceed.

After that run the following command to check which version you have installed in your system. This command will give you all the detailed information about the installed version of Docker.

$ docker version

Development Workflow

We take an application irrespective of the technologies that it uses and Dockerise it.

How?

By simply adding Dockerfile that contains instructions that are further used by Docker to package up the application into an image. This image fulfills all the requirements of the application that it needs to run.

We can push the image to a Docker Hub just like we do in Github and then we can put it into any machine from the Docker hub and run it virtually as it contains all the specific dependencies required by the application to run.

Repositories: Push and pull container images.

Working with Docker

  1. Open terminal and type below commands

First, make sure that you have installed vscode and node

$ mkdir docker-app
$ cd docker-app
$ code .

2. Create an index.js file and paste the below code

console.log("My First Docker App")

In terminal run the below command to run the index.js file

$ node index

3. Create Dockerfile at the same level where you created index.js file and add Docker extension in vscode as shown in below image

Filename: Dockerfile [without any extension]

4. Copy the below code in Dockerfile and save it

FROM node:alpine
COPY . /index
WORKDIR /index
CMD node index.js

5. Let’s build our docker app

sudo docker build -t docker-app .

-t stands for tagline

6. As we created the image but it is not visible in our working directory. So to check it out, run the following command

$ sudo docker image ls

7. Let’s run the docker-app image on our development machine.

$ sudo docker run docker-app

Refer to this blog to know how to utilize the Docker Hub to push and pull the images — https://tiwarivikas.medium.com/working-with-docker-hub-3a0ba1d7cc05

Conclusion

You have successfully built the docker image and learned the basic detailed overview about Docker. If you face any problem feel free to reach me out on LinkedIn.

If you like the blog make sure to take a look on my YouTube channel for more amazing stuff.

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store