Overview of Docker
- Open source
- Used for developing, shipping and running applications.
- With docker, now the application is independent of the infrastructure.
- Infrastructures are also maintained by docker.
- Hence software can be delivered quickly.
- It provides loosely coupled environment called container.
- Containers are lightweight because they don’t need hypervisor and run directly from machine’s kernel only.
- Docker run on virtual machines too.
- It is a client server application.
- There is a docker daemon which is called server.
- Rest APIs works as interface, client and programs(/scripts) can talk to the server using these api.
- Clients can interact with server and ask it to perform tasks like:
- create and manage Docker objects eg:
- create and manage Docker objects eg:
- The Docker Daemon: (>>docker)
- It listens for api request and manages the docker objects.
- Can also communicate with other daemons.
- The Docker client: (>>docker)
- Way to interact with docker daemon.
- Client can talk to more than one daemon.
- The Docker Registry:
- Stores the docker images.
Docker hub is a public registry. (docker pull, docker push to pull and push images to registry).
Images, containers, networks, volumes, plugins etc. are docker objects.
- Images: these are read only templates.
- Image can be based on other images and can be customized.
- Create a DOCKERFILE to create image.
- Containers: containers are runnable instance of an image.
- create, start, stop, move, delete a container using docker api.
- It can be connected to network objects, storage or image can be created based on current state.
- Containers isolation level can also be controlled.
$docker run -i -t ubuntu bin/bash
- Search for ubuntu image locally if not found then search default registry.
- Docker creates new container.
- Docker will allocate a filesystem to container.
- Docker will create a network interface(default) and connect it to container.
- An ip address will be assigned to container.
- Docker starts the container and execute bin/bash.
- Because of -i (interactively) and -t option provide terminal hence, input can be given by keyboard and output will be logged onto terminal.
- Services: allows to scale docker application to multiple Docker Daemon.
- These multiple docker daemons are called swarm as they replicate them.
- These swarms are managed by multiple managers and workers.
- All these daemons communicate via daemon APIs.
- Service allows to define desired state eg: number of replicas of service.
- By default, services are load balanced across all worker nodes.
Docker engine supports swarm mode in Docker 1.12 and higher.
Why to use docker?
- Fast, consistent delivery of your applications: Containers are great for continuous integration and continuous delivery (CI/CD) workflows
- Responsive deployment and scaling: Docker’s container-based platform allows for highly portable workloads.
Real time dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate because of its light weight and portable nature.
- Running more workloads on the same hardware: no need of hypervisor based virtual machines
- Docker is a platform for developers and system admins to build, run and share application with Containers.
- Containerization: use of containers to deploy application.
What is container and image?
- A container is a running process.
- There is some encapsulation applied to container so as to keep it isolated from other containers and the host.
- With isolation now, each container will have its own filesystem.
- Filesystem will be provided by docker image.
- Image: an image will contain –code, runtimes, dependencies, filesystem objects etc.
- Loosely coupled
The pull command fetches the ubuntu image from the Docker registry and saves it in our system. In this case the registry is Docker Hub.
List all the images present
Run docker container
Docker client finds the image (ubuntu), creates the container and then runs a command in that container and after running the ls command it exited after returning the output.
So ubuntu machine was launched, it executed the ls- l command, shut down, output back to docker.
Now if we don’t want to exit so fast that is have to execute some more commands then start bash in interactive mode and when we have to exit ubuntu type = exit command.
Listing all docker containers with their status
This is a critical security concept in the world of Docker containers! Even though each docker container run command used the same alpine image, each execution was a separate, isolated container. Each container has a separate filesystem and runs in a different namespace; by default, a container has no way of interacting with other containers, even those from the same image
Now to restart the existing container use its containerid. Get the containerid from ls command.
>>docker container start <container ID>
Now the container will not exit and to execute any command now type
>>docker container exec <container ID> ls
How to make our own Docker Image
there are over 700K images on Docker Store it is almost certain that none of them are exactly what you run in your data center today.
Either we can commit our changes and distribute it as an image otherwise we will use the dockerfile.
Image creation from a container
1: run ubuntu image in interactive mode
>>docker container run -i -t bash
2: for customization purpose now let us add a package called figlet.
>>apt-get install –y figlet
>>figlet “hello docker”
3: now exit
4: suppose now this particular figlet application has to be shared with other people, as if its our application and is very difficult to configure every time on different system.
So now whole image will be shared instead.
5: find the id of the container using ls –a command. (use –a as non-running instances are not returned by simple ls command).
>>docker container ls –a
6: if changes have to be checked from the original image type:
>>docker container diff <container ID>
7:to create an image commit this container and an image will be created locally on the system where docker daemon is running.
>>docker container commit <containerId>
Full container Id is not necessary just number of characters which can uniquely identify the container.
Now run the image.