Docker — Understanding Basics

Lets talk about the most common problem the industries were facing before the terms containerization and docker.

Here we can see a developer has built an application that works fine in his environment but when it reaches production, there were certain issues with that application which happens because of the difference in the computing environments between Dev and prod.

This is where containerization and Docker come in. Docker is a containerization platform that solves the libraries and dependency issues once and for all. As the tester and developer now have the same system running on Docker container, they both are able to run the application in the Docker environment without having to face differences in dependencies issues as before.

In simple terms, you can now wrap up all the pieces your software needs in a single unit called a docker image, then ship or share this image with anyone. And, as long as the recipient has Docker, they will be able to run or test your project. Gone are the days of, “But it worked on my machine!”

Container refers to a lightweight, stand-alone, executable package of a piece of software that allows a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that the machine might have that could differ from the machine used for writing and testing the code.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. Also, containers are created and booted in seconds, use fewer resources while a VM takes time to boot and use more resources.

Docker Architecture

Docker Architecture
  • The server is the physical server that is used to host multiple virtual machines. So this layer remains the same.
  • The Host OS is the base machine such as Linux or Windows. So this layer remains the same.
  • Now comes the new generation which is the Docker engine. This is used to run the operating system which earlier used to be virtual machines as Docker containers.
  • All of the Apps now run as Docker containers.

The clear advantage in this architecture is that you don’t need to have extra hardware for Guest OS. Everything works as Docker containers.

Now, A developer can create a code, without having to worry about the packing of the application and uploading it to other environments within a short duration, and finally, delivering a properly working application that works fine on all the machines.

Docker Registry :

Is a repository for Docker images. Using the Docker registry, you can build and share images with your team. A registry can be public or private. Docker Inc provides a hosted registry service called Docker Hub. It allows you to upload and download images from a central location. If your repository is public, all your images can be accessed by other Docker hub users. You can also create a private registry in Docker Hub, Google Cloud Registry, Amazon Registry, etc. Docker hub acts like git, where you can build your images locally on your laptop, commit it, and then can be pushed to the Docker hub.

Some Basic Docker Commands :

  1. docker version: Shows docker version
  2. docker images: list all images
  3. docker ps: shows all running containers
  4. docker ps -a: shows all containers, running + exited
  5. docker ps -aq: shows the id of all containers
  6. docker search <image_id> : search images
  7. docker rm <container_id> : removes a container
  8. docker rmi <id> : removes an image
  9. docker pull ubuntu:20.04: pulls an ubuntu image from docker hub of version 20.04
  10. docker login: login to docker hub
  11. docker push: pushes images from local to docker hub
  12. docker stats <id> : displays resource utilization of container
  13. docker logs <id> : Generate logs of container
  14. docker run <image>: pulls an image from docker hub and runs as a container
  15. docker tag: provide a tag to the docker image

Creating and building images using a Dockerfile :

A Dockerfile is a text file that defines a Docker image.

We can use a Dockerfile to create our own custom Docker image, in other words, to define our custom environment to be used in a Docker container. The Dockerfile acts like a recipe for your image, what OS you need, what libraries to install, what variables to use, and so on.

The workflow to follow using Dockerfile :

  1. Create the Dockerfile and define the steps that build up your images
  2. Execute the docker build command which will build a Docker image from your Dockerfile.
  3. Use this image to start containers with the docker run command

How to Write a Dockerfile?

Every Dockerfile must start with the FROM instruction.

The idea behind this is that you need a starting point to build your image. The image you start from is called the base image.

Dockerfile Contents

The next step is to build an image using the Dockerfile using docker buildand then running a container using docker run.

Wrapping Up :

I hope you’re now equipped with the knowledge you need to learn about Docker and maybe even use it in a project one day.

As always, drop me a line in the comments if I’ve made any mistakes or can be helpful in any way! :)

Learner / Achiever / Performer

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