Beginners guide to Docker | Harshit Sharma

The goal of this blog is to introduce readers to docker and help them setup their first docker image.

Docker is software that provides us the technological enablement to perform os-level virtualisation, which is also known as containerisation. Now as a reader you may wonder that anyone could use virtualisation software like VMware or Virtual box then why to use docker and what is the use case of docker or what problem does docker solve?

Out of all phases of the Software Development Lifecycle, Docker is used in the Deployment phase of software development. The reason docker is so popular and so widely used is that It solves the following problem i.e. 

Sometimes an app developer builds and deploys an application on his/her own environment or PC, It works perfectly and without any error but when the same app or code, when compiled on a different machine or in a different environment gives out an error or doesn't work properly. This failure in running the app could depend on a lot of factors like OS, Technology Dependencies, Version of technology used, etc. 

Docker allows us to develop and deploy apps in neatly packaged containerised environments which enable apps to run the same way in all types of operating systems without a compatibility issue.

These containers are like microcomputers with very specific jobs and each of the containers have their own operating system, their own isolated CPU processes, and Network resources. Which makes it easy to add, remove, start, and stop them without affecting the host machine.
Users can set up their own docker containers or download a preconfigured container from the Docker hub.  Docker hub is an online store where a lot of images are available.

What is the difference between Virtual Machines and Containers?

Virtualisation uses hypervisor which helps create and run individual operating systems on a host OS, A VM or virtual machine has its own operating system, Now there are pros and cons of everything but in a scenario where multiple os environments are needed then it becomes a hard task to deploy a lot VM's in a host machine as each VM will have its own OS and will take a large number of disk space.

Docker Containers makes use of host operating system's Linux Kernel and the size of a container is quite small.

What Does it take to build a Docker Image ?

Building your own docker image is quite simple, The below given instructions is a depiction of a web app running as a docker container.

You can use any linux but for this demo, I have opted to use Ubuntu. After you setup an ubuntu machine all you need to do is go to terminal and type the following commands.

  • sudo apt install

  • sudo systemctl start docker
  • sudo systemctl enable docker

This will help us start docker engine and enable docker, You can check if installation was successful by typing

  • docker --version

Step 1: Create a web app to put in a container

To simulate a web app, I created index.html and wrote "hello world!" and saved the file in a newly created directory named as test.

Step 2: Create a Docker File

Dockerfile is a text document that a user would use to assemble an image. In order to create a docker file you need to add the following commands in text file and save it as docker file, We will used docker build command to build an image.

FROM ubuntu:16.04    - This means take base image as ubuntu 16.04

RUN apt-get update -y    - Run Update to be able install basic utilities like apache
RUN apt-get install -y apache2
RUN chown -R www-data:www-data /var/www    - Changing the ownership and making apache the owner of /var/www/

ENV APACHE_RUN_USER www-data    -Environment variable for apache server
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

ADD index.html /var/www/html


ENTRYPOINT ["usr/sbin/apache2ctl"]

(Entry point to start the container with options in cmd)

Note: Create the Dockerfile in the same directory as test.

Step 3: Build

In order to build the image from you need to use the following command.

  • docker build -t lucideus:latest .

In the above command -t means tag and the dot "." indicates that the docker file is supposed to be picked from the same directory.

Its is possible that you may face permission error to fix it just use the following command.

  • sudo chmod 666 /var/run/docker.sock

It will take 10-15 minutes and you docker image would be ready. You can type in the following command to view your newly built image.

  • docker images

You can also pull other images from docker hub by using the following command.

  • docker pull {name of the image}
You can mention the version of the image, by default it will download the latest image.

Step 4: Run your Docker Image

You can type the following command to run image as a container.

  • docker run -itd -p 8080:80 {name of the image}:{tag of the image}
'i' means  interactive mode and keep stdin open, 't' tells to allocate a tty and 'd' tells to run in background and print container.

To verify that its running you can open up browser.

You can use the following command to gain an interactive shell in a docker container.
  • docker exec -it {container id} sh

Image vs containers

The whole point of adding this section is to avoid confusion between images and containers as while reading the blog you would have encountered phrases like creating an image and running a container which might confuse you as reader. So in this section there is a to the point answer to your confusion.

The image we have referred to time and again is actually known as "Docker Container Image".
Docker Container Image is a Light weight package of software that contains everything required to run the application e.g. code, libraries, system files and tools.

Docker Container on the other hand is an instance of an Image, In layman terms it can be described as a standard unit of software that has packaged up code and all it dependencies to enable migration and deployment from one computing environment to another. Kindly refer to the below given image.

Article Link: