Introduction to Docker

Introduction to Docker

  • What problems Docker is trying to solve?

    • Earlier what is to happen is that One App used to run on One Server
    • VMWare solved this problem -> Through Virtual Machines
      • In VMs you can run multiple apps on the same server
      • But There was a problem with this also:
        • Virtual Machines require their own OS
        • Not super fast
        • Dedicated storage, CPU, RAM is required
      • More problems
        • Your dependencies management issue (i.e. It works on my machine and not yours stuff )
        • Shift your code from one point to another
          • Migration can be time consuming
        • suppose you are contributing to open source and you want to setup the project on your local system that can be very tedious task
  • Containers:

    • Container is sort of like a VM but you don't require entirely new OS to use it
    • An app is running in an isolated environment (i.e. container) <- Docker helps us in doing that
      • Docker is a container tool that allows u to create container, manage containers, scale containers
  • Virtual Machines Vs. Containers:

    • Virtual Machines:
      • An application on a VM requires a guest OS and thus an underlying hypervisor to run.
      • Hypervisor is used to create multiple machines on a host operating system and it manages virtual machines.
      • These virtual machines have their own operating system and do not use the host’s operating system. They have some space allocated.
    • Containers:
      • In the software world, containerization is an efficient method for deploying applications.
      • A container encapsulates an application with its own operating environment.
      • It can be placed on any host machine without special configuration, removing the issue of dependencies.
      • VM is hardware virtualization, whereas containerization is OS virtualization.
      • In comparison, virtualization is the creation of a virtual version of something such as an operating system, server or storage device or network resources. Essentially, containerization is a lightweight approach to virtualization.
  • Imp Note:

    • If a windows app is containerized it will not run on Linux based docker and vice Versa
    • So that's why windows container will require windows host and Linux container will require Linux host
  • What is Docker?

    • Docker is basically helping us to run our applications in isolated environment(container)
    • Docker is a container platform that allows you to build, test and deploy apps quickly
    • Process:
      • A developer defines all the apps and its dependencies is a docker file which is then used to build Docker image that defines a docker container
      • Doing the above steps ensures your app will run in any environment
  • Architecture of Docker:

    • Runtime:
      • Runtime basically allows us to start and stop containers
      • Runtime is of two types:
        • -> Low level: runC
          • Role of runC runtime is basically to work with the operating system and starts and stops the container
        • -> High level: containerd
          • Role of containerd is basically to manage runC aand it also helps in managing containers
      • Remember: Every docker container will have both runC and containerd
    • Docker Engine: (Daemon)
      • Docker engine is an open-source containerization tech. for building and containerizing your application
      • Docker engine acts as a client server application
        • Server with long-running daemon process
        • APIs which specify interfaces that programs can use to talk to and instruct the docker daemon
        • CLI client docker
    • Orchestration:
      • As we scale our application up, we need some tools to help automate the maintenance of those application, enable the replacement of failed containers automatically, and manage the rollout of updates and reconfigurations of those containers during their life cycle
      • Tools to manage, scale and maintain containerized applications are called orchestrators
        • e.g. Kubernetes and Docker swarm
  • Docker Commands

    • Note: I ran all the below commands in ubuntu using WSL
    • $ docker run hello-world
      • Here "run" means you run an image to create a container
      • "hello-world" is its image name
    • $ docker run -it ubuntu
      • It means Docker run in the interactive environment ubuntu
    • $ docker images
      • It shows which apps are there in local system
    • Note: If a particular application is not there in local system, then it will download it from online registry(dockerhub)
    • Remember: When we talk about docker image -> they also contains OS files and the application and the dependencies for the application as well
    • $ docker pull mysql
      • Docker hub contains many pre-build images that you can "pull" and try without needing to define configure your own
    • $ docker pull ubuntu : 16.04
      • Here "16.04" is the version of ubuntu. So similarly we can pull specific versions of any application as well
      • $ docker run -it ubuntu: 16.04 : To run that version
    • $ docker container ls
      • This will show all the containers running on PC
    • $ docker stop xxxxxxxxxx648
      • We stopped a particular container with container id = xxxxxxxxxx648, this id we can get by running $ docker container ls
    • $ docker ps -a
      • It will show all the containers that is stopped
    • $ docker rm xxxxxxxxxx648
      • We deleted a particular container with container id = xxxxxxxxxx648
    • $ docker inspect ubuntu/container id
      • It gives all the info about the container
    • $ docker logs xxxxxxxxxx648
      • It will show what are the things we have done in the container with container id =xxxxxxxxxx648
    • $ docker container prune -f
      • It deletes all the containers
    • $ docker run alpine ping www.civo.com
      • Checks the connectivity status
    • $ docker run -d alpine ping www.civo.com
      • This is detached mode, it will run in background
    • $ docker logs xxx4
      • Here "xxx4" is the first 4 digits of a container id
      • It will show the logs of that particular container'
    • $ docker logs --since 5s xxx4
      • It will show the logs of the container of last 5 sec
    • $ docker stop xxx4
      • To stop that particular container
    • $ docker rm xxx4
      • To remove that particular container
    • $ docker start xxxxxxxxxxx67
      • It starts that particular container which we already created
      • $ docker exec -it xxxxxxxxxxx67 bash

    • $ docker commit -m "added names.txt file" xxxxxxxxxxx67 names_ubuntu : 1.01
      • Here "names_ubuntu : 1.01" is the image name
      • To create new image which will have names.txt file in it
    • $ docker run -it names_ubuntu:1.01
      • To run that image
    • $ docker images -q
      • It shows only IDs of all the images
    • $ docker rmi $(docker images -q)
      • To delete all the images at once
    • $ docker rmi $(docker images -q) -f
      • Here -f forcefully deletes the images
  • What if I want to create my own image?

    • Before doing anything else go to Docker Hub website and login
    • Now first step is create Dockerfile:
      • Dockerfile describes steps to create a Docker image
      • Its like a recipe with all ingredients and steps necessary in making your dish
      • In terminal(Linux):
        • $ touch Dockerfile
        • $ vim Dockerfile
          • Inside Dockerfile write following lines
          • FROM ubuntu MAINTAINER amaan Laskar laskaramaan@gmail.com
            RUN apt-get update
            CMD ["echo", "Hello World"]
          • Here , During building of the image, the commands in RUN section of Dockerfile will get executed
          • The commands in CMD section of Dockerfile will get executed when you create a container out of the image.
        • $ docker login
        • $ docker build -t myimage:1.1 .
        • $ docker run myimage:1.1
  • How Docker container actually runs?