Wednesday, 16 February 2022

Create your first Docker Container

Traditional computer vs Virtual machine vs docker container: 

If you get correlation between computer/physical machine, virtual machine and docker container then it will be very easy to understand docker containers.

  1. Physical machine/computer: What is computer...Its just a hardware/machine that performs processes based on instructions provided by a software program. So as per the definition, hardware and software constitutes computer.
  2. Virtual machine(VM): Virtual machines are software computers that provide the same functionality as physical computers. So as per definition, VM needs software and for hardware part it can share hardware of physical machine. In lay-man term, you can say nested computer. But VM has its own OS apart from physical machine. Virtual machines utilises underlying hardware, however can be considered as a completely separate entity.
  3. What is Container: Containers are nothing but light weight VMs and it contain minimal OS. You can deploy containers on physical machine as well as on virtual machines.

In below table we have mentioned docker image, for now you can correlate docker image as ISO of machine.

And you can correlate container with VM or you may say lightweight VM





Physical MachineVirtual MachineContainer
HardwareNeedsDont needDont need
SoftwareOS is installed using ISO.OS is installed using ISO.Does not contain OS but Docker image is needed
to instantiate container.(We will discuss in detail)
ScalingDifficultModerateEasy
ResourceNeeds lot of resourcesNeeds moderate resourcesNeeds very less resources
Startup timeIn minutesIn minutesIn milliseconds


  • When should you use Docker vs VMs? 

One basic use case is - when you need isolated environment for your projects or in case you need to scale-up or scale-down your services, then you can use containers. Generally containers are made for use and throw kind projects. VMs are better choice for running apps that require all of the operating system's resources.

  • Basic terminologies used in docker:

  1.  Docker Image: Docker image can be called as a snapshot of a container. Most docker images start with a base image.Docker image is made up of multiple layers. User can create docker image using dockerfile, which is nothing but set of instructions to create docker image. One can create docker image using "build" command.
  2. Example: docker build -t myimage -f mydockerfile .

  3.  Docker Container: Containers are nothing but instance of an image or you can say its a lightweight VM which is created using docker image. We can create docker container using "run" command.  
  4. Example: docker run -it  myimage /bin/bash 
      
  5. Docker hub: Its a registry service which allows you to download Docker Images that are build by other communities. You can also upload your custom images to docker hub. You just need to sign up at official site.
  6. Example: docker pull jenkins


  • Basic Docker commands:
    • Docker Image related commands:
      • docker images: To list the images
      • docker rmi <image_id>: To remove the image
      • docker inspect <image_id>: To see details of image
      • docker build -f <dockerfile> . :To build docker image from dockerfile
    • Docker Container related commands:
      • docker ps: To list the running containers
      • docker ps -a: To list all the containers
      • docker run -itd <image_id> /bin/sh: To create container using image
      • docker rm <container-id>: To remove containers
    • Docker hub related commands:
      • docker pull: To pull built-in docker image from hub
      • docker push: To push the image to docker hub



  • Build Docker image using Dockerfile: A dockerfile is a script which contains a collection of dockerfile commands and linux commands. Below are some dockerfile commands: 
    • FROM: Base image for building a new image. This command must be on top of the file
    • MAINTAINER: Optional, name of maintainer of image
    • RUN: Execute a command during build process of docker image
    • ADD: Copy a file from host machine to new docker image.
    • ENV: Define an environment variable 
    • CMD: Used for executing commands when we build new container from image.
    • ENTRYPOINT: Default command that will be executed when container is running






  • Steps to create your first container:

  1. Create sample file named sampleDockerfile and copy below content

FROM ubuntu:18.04
RUN apt-get update
ADD sample.rpm /tmp
EXPOSE 80
WORKDIR /root
CMD ["/sbin/init"]

2. Build docker image using:

docker build -f sampleDockerfile .


3. Now check the docker image and note image_id using:

 docker images


4. Now create a container from image and go inside container using:

docker run -it <image_id> /bin/bash