DOCKER BASIC INTRODUCTION
What is Docker
Docker is an open platform that helps with the universal distribution of applications.
Docker is a platform to develop, deploy, and run applications with containers.
Docker engine is responsible for running processes in isolated environments. For each process,
it generates a new Linux container, allocates a new filesystem for it, allocates a network interface,
sets an IP for it, sets NAT for it and then runs processes inside of it.
The use of Linux containers to deploy applications is called containerization.
Containerization is :
| Flexible | : | Even the most complex applications can be containerized. |
| Lightweight | : | Containers leverage and share the host kernel. |
| Interchangeable | : | You can deploy updates and upgrades on-the-fly. |
| Portable | : | You can build locally, deploy to the cloud, and run anywhere. |
| Scalable | : | You can increase and automatically distribute container replicas. |
| Stackable | : | You can stack services vertically and on-the-fly. |
So What is Docker ? – Docker is a containerization platform that packages your application and all its dependencies together in the form of a docker container to ensure that your application works seamlessly in any environment.
In other words, Docker is designed to benefit both Developers and System Administrators, making it a part of many DevOps toolchains. Developers can write their code without worrying about the testing or the production environment and system administrators need not worry about infrastructure as Docker can easily scale up and scale down the number of systems for deploying on the servers.
Images and containers
A container is launched by running an image. An image is an executable package that includes everything needed to run an application--the code, a runtime, libraries, environment variables, and configuration files.
A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process).
A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process).
Containers and virtual machines
- A container is a runtime instance of an image.
- A container runs natively on Linux and shares the kernel of the host machine with other containers.
- It runs a discrete process, taking no more memory than any other executable, making it lightweight.
Virtual Machines
VM or Virtual Machine is simply a virtual computer. It executes each and every program like a real computer. It runs on top of a physical machine with the help of a hypervisor.
There are 2 types of Hypervisors
Type 1, also called “Bare Metal Hypervisor” & Type 2, also called “Hosted Hypervisor”
“Bare Metal Hypervisor” : - Type 1 hypervisors have their own device drivers and interact with hardware directly unlike type 2 hypervisors. That’s what makes them faster, simpler and hence more stable.
“Hosted Hypervisor” : - This is a program that is installed on top of the operating system. You are probably more familiar with it, like VirtualBox or VMware Workstation. This type of hypervisor is something like a “translator” that translates the guest operating system’s system calls into the host operating system’s system calls.
After that need to register your docker account on "https://hub.docker.com/"
Note : - After registration use (docker ID) to login on docker.com
After that need to register your git account on "https://github.com/" and also need to install git on your machine from "https://git-scm.com/downloads" url.
Note : - It is important for "CONTINUOUS INTEGRATION WITH DOCKER HUB"
After that need to download "DOCKER DESKTOP FOR WINDOWS"
from "https://www.docker.com/products/docker-desktop" url & install & restart your machine.
let's start
Dear all, I am going to cover below topic(s) step by step with practical
What is an Docker image : -
• An image is a collection of files + some meta data.
(Technically: those files form the root filesystem of a container.)
• Images are made of layers, conceptually stacked on top of each other.
• Each layer can add, change, and remove files.
• Images can share layers to optimize disk usage, transfer times, and memory use.
Images are like templates or stencils that you can create containers from Object-oriented programming.
• Images are conceptually similar to classes.
• Layers are conceptually similar to inheritance.
• Containers are conceptually similar to instances
An image is a read-only filesystem , but if an image is read-only, how do we change it?
• We don't.
• We create a new container from that image.
• Then we make changes to that container.
• When we are satisfied with those changes, we transform them into a new layer.
• A new image is created by stacking the new layer on top of the old image.
Images namespaces
There are three namespaces: (Root namespace , User namespace, Self-Hosted namespace)
Root namespace :- The root namespace is for official images. They are put there by Docker Inc., but they are generally authored and maintained by third parties.
Example : - ubuntu
User namespace :- The user namespace holds images for Docker Hub users and organizations.
Example: mvr1234/clock
Explanation : -
| The Docker Hub user is | : | mvr1234 |
| The image name is | : | clock |
Self-Hosted namespace :- This namespace holds images which are not hosted on Docker Hub, but on third party registries. They contain the hostname (or IP address), and optionally the port, of the registry server.
Example: localhost:5000/mysql
| The remote host and port is | : | localhost:5000 |
| The image name is | : | mysql |
• A self-hosted registry can be public or private.
• A registry in the User namespace on Docker Hub can be public or private.
How do you store and manage images
Images can be stored:
• On your Docker host.
• In a Docker registry.
You can use the Docker client to download (pull) or upload (push) images.
To be more accurate: you can use the Docker client to tell a Docker server to push and pull images to and from a registry
Searching for images
Example : - docker search mysql
Explanation : - "docker search" is command & "mysql" is image.
Downloading images
Example : - docker pull ubuntu:latest
Explanation : - "docker pull" is command & "ubuntu:latest" is image ,
but notice that actual image is "ubuntu" and "latest" is tag name
Image and tags
• Images can have tags.
• Tags define image variants.
• docker pull ubuntu will refer to ubuntu:latest.
• The :latest tag can be updated frequently.
• When using images it is always best to be specific.
Building Docker Images
• Write a Dockerfile.
• Build an image from a Dockerfile.
Docker file overview :-
• A Dockerfile is a build recipe for a Docker image.
• It contains a series of instructions telling Docker how an image is constructed.
• The docker build command builds an image from a Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Using docker build users can create an automated build that executes several command-line instructions in succession.
This page describes the commands you can use in a Dockerfile .
Docker file creation : - We always create Docker file with "Dockerfile" name without extension.
Example : -
create "Dockerfile" file without extension. & put below lines in that file
FROM ubuntu
RUN apt-get update
RUN apt-get install -y wget
Explanation : -
• FROM indicates the base image for our build.
• Each RUN line will be executed by Docker during the build.
• Our RUN commands must be non-interactive.
(No input can be provided to Docker during the build.)
Docker file Command & Usages : -
| # | : | Lines starting with # are treated as comments |
| MAINTAINER | : | The MAINTAINER instruction allows you to set the Author field of the generated images. Example : - MAINTAINER authors_name(Mauresh Ratnaparkhi) |
| FROM | : |
FROM directive is probably the most crucial amongst all others for Dockerfiles.
It defines the base image to use to start the build process.
It can be any image,
including the ones you have created previously. If a FROM image is not found on the host, Docker will try to find it (and download)
from the Docker Hub or other
container repository. It needs to be the first command declared inside a Dockerfile.
Example : - FROM ubuntu |
| RUN | : |
The RUN command is the central executing directive for Dockerfiles. It takes a
command as its argument and runs it to form the image. Unlike CMD,
it actually is
used to build the image (forming another layer on top of the previous one which is
committed).
|
| CMD | : | CMD defines a default command to run when none is given. It can appear at any point in the file.Each CMD will replace and override the previous one. As a result, while you can have multiple CMD lines, it is useless. Example : - CMD wget -O- -q http://ifconfig.me/ip CMD "echo" "Hello docker!" Explanation : - • -O- tells wget to output to standard output instead of a file. • -q tells wget to skip verbose output and give us only the data. • http://ifconfig.me/ip is the URL we want to retrieve. • echo will print the "Hello docker!" message on screen. |
| ENV | : |
The ENV command is used to set the environment variables (one or more).
These variables consist of “key value” pairs which can be accessed within the
container by scripts and applications alike. This functionality of Docker offers an
enormous amount of flexibility for running programs.Example : - ENV VERSION="5.15.7" USER="activemq" |
| LABEL | : | The LABEL instruction adds metadata to an image. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing. Labels are additive including LABELs in FROM images. If Docker encounters a label/key that already exists, the new value overrides any previous labels with identical key. To view an image’s labels, use the docker inspect command. They will be under the "Labels" JSON attribute. Example : - ENV VERSION="5.15.7" USER="activemq" LABEL activemq-version="${VERSION}" desc="Apache ActiveMQ" |
| EXPOSE | : | Informs Docker that the container listens on the specified network port(s) at runtime. EXPOSE does not make the ports of the container accessible to the host. Example : - # Usage: EXPOSE [port] EXPOSE 8161 |
| ADD | : | The ADD command gets two arguments: a source and a destination. It basically copies the files from the source on the host into the container's own filesystem at the set destination. If, however, the source is a URL (e.g. http://github.com/user/file/), then the contents of the URL are downloaded and placed at the destination. Example : - # Usage: ADD [source directory or URL] [destination directory] ADD /my_app_folder /my_app_folder |
| COPY | : | ADD and COPY are functionally similar, generally speaking, COPY is preferred. Example : - # Usage: COPY [source directory or URL] [destination directory] ADD /my_app_folder /my_app_folder |
| ENTRYPOINT | : | ENTRYPOINT argument sets the concrete default application that is used every time a container is created using the image. Example : - FROM openjdk:8-jre-alpine |
| VOLUME | : | Creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers. The VOLUME command is used to enable access from your container to a directory on the host machine Example : - # Usage: VOLUME ["/dir_1", "/dir_2" ..] VOLUME ["/my_files"] Docker volumes can be used to achieve many things, including: • Bypassing the copy-on-write system to obtain native disk I/O performance. • Bypassing copy-on-write to leave some files out of docker commit. • Sharing a directory between multiple containers. • Sharing a directory between the host and a container. • Sharing a single file between the host and a container. Volumes are special directories in a container Volumes can be declared in two different ways. • Within a Dockerfile, with a VOLUME instruction. VOLUME /var/lib/postgresql • On the command-line, with the -v flag for docker run. $ docker run -d -v /var/lib/postgresql training/postgresql In both cases, /var/lib/postgresql (inside the container) will be a volume. |
| USER | : | The USER instruction sets the user name or UID to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile. Example : - # Usage: USER [UID] USER 751 |
| WORKDIR | : | The `WORKDIR` line specifies a new default directory within the image's file system. Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it. It can be used multiple times in the one Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction. Example : - # Usage: WORKDIR /path WORKDIR ~/ |
| ARG | : | A running container won't have access to an ARG variable value. ENV values are available during the image build phase, but also after a container is launched from the image. You can override ENV values which were set in a Dockerfile by providing environment variables in the command line or your docker-compose.yml file. ARG are also known as build-time variables. They are only available from the moment they are 'announced' in the Dockerfile with an ARG instruction up to the moment when the image is built. Running containers can't access values of ARG variables. Example : - ARG some_variable_name # or with a hard-coded default: #ARG some_variable_name=default_value RUN echo "Oh Mayuresh look at that $some_variable_name" # you could also use braces - ${some_variable_name} When building a Docker image from the command line, you can set ARG values using –build-arg: $ docker build --build-arg some_variable_name=a_value |
Dockerfile usage summary
• Dockerfile instructions are executed in order.
• Each instruction creates a new layer in the image.
• Instructions are cached. If no changes are detected then the
instruction is skipped and the cached layer used.
• The FROM instruction MUST be the first non-comment instruction.
• Lines starting with # are treated as comments.
• You can only have one CMD and one ENTRYPOINT instruction in a Dockerfile.
• Specifies the source image to build this image.
• Must be the first instruction in the Dockerfile, except for comments
• The FROM instruction can be specified more than once to build multiple images.
Example : -
FROM ubuntu:14.04
FROM fedora:20
PRACTICAL
1. Go to the PowerShell command prompt as Administrator & do practice of below commands


Repository on Docker Hub
SOME ADVANCE
We are going to develop "activemq" Docker Image and will write a java application , which will consume and produce messages.
for better understanding you can download the code from here Docker-with-ActiveMq
Now see the second example, this time, We are going to develop "mysql" Docker Image and will write a java application , which will check mysql connectivity.
for better understanding you can download the code from here docker-mysql-demo
WHAT IS DOCKER CONTAINER
• A container is an encapsulated set of processes running in a read-write copy of that filesystem.
• To optimize container boot time, copy-on-write is used instead of regular copy.
• docker run starts a container from a given image.
Docker container is an open source software development platform. Its main benefit is to package applications in “containers,” allowing them to be portable among any system running the Linux operating system (OS).
Docker Containers: Another Form of Virtualization
Think of a container as another form of virtualization. Virtual Machines (VM) allow a piece of hardware to be split up into different VMs – or virtualized — so that the hardware power can be shared among different users and appear as separate servers or machines.Containers virtualize the OS, splitting it up into virtualized compartments to run container applications.
This approach allows pieces of code to be put into smaller, easily transportable pieces that can run anywhere Linux is running. It’s a way to make applications even more distributed, and strip them down into specific functions.
Benefit of Docker Container
- Virtual Machines are slow and take a lot of time to boot.
- Containers are fast and boots quickly as it uses host operating system and shares the relevant libraries.
- Containers do not waste or block host resources unlike virtual machines.
- Containers have isolated libraries and binaries specific to the application they are running.
- Containers are handled by Containerization engine.
- Docker is one of the containerization platforms which can be used to create and run containers.
Virtual Machine and Docker Container are compared on the following three parameters:
- Size – This parameter will compare Virtual Machine & Docker Container on their resource they utilize.
- Startup – This parameter will compare on the basis of their boot time.
- Integration – This parameter will compare on their ability to integrate with other tools with ease.
Size : This parameter will compare Virtual Machine & Docker Container on their resource they utilize.
Startup : This parameter will compare on the basis of their boot time.
Integration : This parameter will compare on their ability to integrate with other tools with ease.
Integration of different tools using Virtual Machine maybe possible, but even that possibility comes with a lot of complications.
DOCKER CONTAINER LINKING
now we will learn docker container linking, so what is docker container linking
Docker container linking : - Docker
also has a linking system that allows you to link multiple containers
together and send connection information from one to another. When
containers are linked, information about a source container can be sent to a recipient container.
Note: - with --link flag , we bind the one container to another container , but the
--link flag is a legacy feature of Docker.activemq-demo-with-container-linking
DOCKER NETWORKING
for better understanding you can download the code from here Docker-Networking-with-ActiveMq
Docker Swarm
A swarm is a group of machines that are running Docker and joined into a cluster.After that has happened, you continue to run the Docker commands you're used to, but now they are executed on a cluster by a swarm manager.
Docker Swarm is a technique to create and maintain a cluster of Docker Engines.
Docker Swarm is a technique to create and maintain a cluster of Docker Engines.
Docker Swarm is a clustering and scheduling tool for Docker containers.
SWARM : A swarm is a group of Docker hosts that run in swarm mode, operating as a single virtual host.
NODE: A node is an individual Docker Engine that is part of a swarm. You can many nodes on a single computer, but the more typical use is when nodes are distributed across multiple machines. Nodes can act as managers, leaders, or workers.
MANAGER NODE: This type of node assigns tasks to worker nodes in addition to helping with the management of swarms.
LEADER NODE: One node is elected as a leader node using the Raft consensus algorithm. The leader node conducts orchestration tasks in the swarm.
WORKER NODE: Worker nodes simply receive and execute tasks designated by the manager nodes. Note that manager nodes function as both managers and workers by default.








































No comments:
Post a Comment