Saturday, May 20, 2017

Docker Overview

A container is packaged as an entire runtime environment: the service/app plus all dependencies, libraries, & configuration files needed to run it
Portable across environments & lightweight (share the OS)



The above image summarize the difference between container and VM, yet they can be combined and docker can be nested inside VM.

Different docker technoloy available such as Docker : www.docker.com, Mesos : http://mesos.apache.org/ and Kubernetes : https://kubernetes.io/


We will pick Docker to give high level functionality of it here.

Docker


Docker began as an internal project for the dotCloud organization. 

It was developed in-house and then later open sourced in 2013.

Enables you to:
Separate your applications from your infrastructure so you can deliver software quickly.
Manage your infrastructure in the same ways you manage your applications



As we can see Docker composed of Server (docker daemon) which expose the docker functionality via REST APIs, the docker command line client uses these REST APIs to communicate with the daemon service/server.

The main components as we can see is the Images, Containers, Network and data Volumes.
We can add to them the registries.

The following show the architecture and include the registry in the picture:



1)  Docker Images : Templates


An image is a read-only template with instructions “Dockerfile” for creating a Docker container. Often, an image is based on another image, with some additional customization.
You might create your own images or use those created and published by others in a registry.
When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt.
This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.


Example of Dockerfile:



It is composed of 3 main parts, base image for that docker, different docker building steps including our application, finally the start command of that docker.
You should know that docker image is layered and Any RUN commands you specify in the Dockerfile creates a new layer for the container, this allow us to share the layers and build upon them which improve the usability of the containers and their layers.


2) Docker Registries: Templates Store
A Docker registry stores Docker images.
Docker Hub and Docker Cloud are public registries that anyone can use, and Docker is configured to look for images on Docker Hub by default.
You can even run your own private registry - “Docker Trusted Registry (DTR)”
You can push, and pull images from any Docker registry

A free https://cloud.docker.com/ account can be created where you can use it to store your docker images.
To use your Docker Cloud account:
docker login : will prompt for username and password
docker push : push to store any image in your 
docker pull : pull any image to your local machine



You can use:  docker search keyword to search for any docker image.
e.g. docker search oracle  ==> to search for Oracle images.



3) Docker Containers: Running instances
A container is a runnable instance of an image.
You can create, run, stop, move, or delete a container using the Docker API or CLI
You can connect a container to:
One or more networks
Attach storage to it
Capture a new image based on its current state.


4) Docker Network :
By default, Docker provides two network drivers:
Bridge (default) : limited to a single host running Docker Engine.
Overlay network : supports multiple hosts.
You can create your own network:
docker network create -d bridge my_bridge
To list existing networks: docker network ls
To add a docker into a network:  docker run -d --net=my_bridge …..
Optionally you can select the IP as well using --ip=ip_address (or --ip6=…)
To inspect network: docker network inspect my_bridge


5) Docker Volumes :
In addition to Docker Union File System which compose the Docker layers, Additional Storage can be mounted such as Data Volume :
Used to persist data, independent of the container’s lifecycle. 
Mounted during create or run of the docker using -v
Example : $ docker run -d -P --name web -v /webapp training/webapp python app.py
You can also mount existing host directory using the same –v
Example: $ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py
To list volumes: docker volume ls
Note: Shared storage can be used but need to pay attention to write operations to avoid data corruption.


6) Docker Swarm :
A swarm is a group of machines (physical or virtual) that are running Docker and have been joined into a cluster.
Contains Swarm Manager and Worker Nodes.
Uses several strategies to run containers:
“emptiest node” : which fills the least utilized machines with containers
“global” : which ensures that each machine gets exactly one instance of the specified container.
You execute: docker swarm init to convert this machine to Swarm Manager and then use: docker swarm join in other machines to join this cluster.


7) Docker Service :
A service only runs one image.
Described using: docker-compose.yml
Describing: what ports it should use, how many replicas, resources, etc.



docker stack deploy -c docker-compose.yml myapp
docker stack ps myapp
docker stack rm myapp


Example: Micro-service Example: Java REST App connect to Oracle DB



The following are the steps to create this example using docker command line:

Execution Steps:
//Build our Java Application Docker ...
docker build -t my_java_docker .
//Create Network for our Java & DB Dockers
docker network create -d bridge my_bridge
//Search for Oracle DB XE
docker search oracle
//Pull one of Oracle DB EX images (not official)
docker pull wnameless/oracle-xe-11g
//Now run the DB container in the my_bridge network ...
docker run --net=my_bridge -d -p 49160:22 -p 49161:1521 -e ORACLE_ALLOW_REMOTE=true wnameless/oracle-xe-11g
//List current running docker instances to get DB XE Instance ID
docker ps
//Access the DB Docker using bash and execute the required DB scripts.
docker exec -it "container id from the previous step" bash
//run SQLPlus to create our DB objects ..
sqlplus
//install DB objects
...
//Inspect my network to get the IP address of the DB container
docker network inspect my_bridge
//Run the Java container using the DB IP address as Environment variable in the Java docker container so it can connect to the DB successfully.
docker run -p 4010:80 --net=my_bridge -e DBAAS_DEFAULT_CONNECT_DESCRIPTOR=172.18.0.3:1521:XE my_java_docker
// or use this in env file in the format of ENV VAR=VALUE

docker run -p 4010:80 --net=my_bridge --env-file=./env.txt my_java_docker



That's just an introduction about Docker and Container Technologies.

For More information check Docker Documentation including samples and a lot of examples can be found at:
https://docs.docker.com/



No comments:

Post a Comment