Docker allows simple environment isolation and repeatability so that we can create a run-time environment once, package it up, then run it again on any other machine. Furthermore, everything that runs in that environment is isolated from the underlying host (much like a virtual machine). And best of all, everything is fast and simple.
This presentation, we’ll provide basic introduction. What is Docker? why to use it? and demonstrate how we can use Docker to compose and deploy an application.
2. www.tothenew.com
About Me
Puneet Behl
Associate Technical Lead
TO THE NEW Digital
puneet.behl@tothenew.com
GitHub: https://github.com/puneetbehl/
Twitter: @puneetbhl
LinkedIn: https://in.linkedin.com/in/puneetbhl
3. www.tothenew.com
Agenda
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello world demo
● Running Stack of services using Docker Compose
● Moving to Production
4. www.tothenew.com
Agenda
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello world demo
● Running Stack of services using Docker Compose
● Moving to Production
11. www.tothenew.com
● “Works on my machine” syndrome
● Hard disk crashed -> New Setup -> Nothing Works :(
● Adding new Developer in the team
● Going live? Please DO NOT break on production.
● Dependency Hell - Common dependency with different versions
In Nutshell, what are the challenges?
12. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Understanding Docker components & architecture.
● Installation
● Hello World demo
● Running Stack of services using Docker Compose
● Moving to Production
15. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello World demo
● Running Stack of services using Docker Compose
● Moving to Production
16. www.tothenew.com
A Docker Container allows application
developer to package up their application with
all it’s dependencies they are associated with.
What is Docker Container?
24. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello World demo
● Running Stack of services using Docker Compose
● Moving to Production
25. www.tothenew.com
● Scalability - The containers are extremely lightweight so scaling up
and scaling down is very easy.
Benefits of using Docker
26. www.tothenew.com
● Scalability - The containers are extremely lightweight so scaling up
and scaling down is very easy.
● Portability - just pull the image and start container.
Benefits of using Docker
27. www.tothenew.com
● Scalability - The containers are extremely lightweight so scaling up
and scaling down is very easy.
● Portability - just pull the image and start container.
● Deployment - because containers can run almost anywhere we can
deploy to Desktop, Physical Server, Virtual Machine, Public/Private
Cloud etc.
Benefits of using Docker
28. www.tothenew.com
● Scalability - The containers are extremely lightweight so scaling up
and scaling down is very easy.
● Portability - just pull the image and start container.
● Deployment - because containers can run almost anywhere we can
deploy to Desktop, Physical Server, Virtual Machine, Public/Private
Cloud etc.
● Efficient Resource Utilization - Multiple isolated containers sharing
resources.
Benefits of using Docker
30. www.tothenew.com
● Build any application in any language using any stack.
● Dockerize application can run anywhere on anything.
● No longer need to cross our fingers when we deploy to production
● Helps improving application design
Why Developers Care?
32. www.tothenew.com
● Easy migrations to different infrastructure
● Replication of different environments is very easy.
● Fix an issue once, it’s fixed everywhere
● Less conflicts with developers, because of same environment.
Why Devops Care?
37. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello World demo...
● Running Stack of services using Docker Compose
● Moving to Production
39. www.tothenew.com
Docker Core Components
● Docker Daemon
The Docker daemon runs on a host
machine. The user does not directly
interact with the daemon, but instead
through the Docker client.
41. www.tothenew.com
Docker Core Components
● Docker Daemon
● Docker Client
The Docker client, in the form of the
docker binary, is the primary user
interface to Docker. It accepts
commands from the user and
communicates back and forth with a
Docker daemon.
44. www.tothenew.com
Docker Workflow Components
● Docker Image
A Docker image is a read-only
template to build Docker Containers
● Docker Registries
Docker registries hold images. These
are public or private stores from
where you upload or download
images.
45. www.tothenew.com
Docker Workflow Components
● Docker Image
A Docker image is a read-only
template to build Docker Containers
● Docker Container
Created from images. start, stop, run
● Docker Registries
Docker registries hold images. These
are public or private stores from
where you upload or download
images.
47. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello World demo
● Running Stack of services using Docker Compose
● Moving to Production
48. www.tothenew.com
Installation
● Linux
Follow steps for your version of linux
https://docs.docker.com/engine/installation/
apt-get install docker-engine
yum install docker-engine
● Mac or Windows OS
Use docker toolbox isntaller : https://www.docker.com/docker-
toolbox
50. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello world demo
● Running Stack of services using Docker Compose
● Moving to Production
54. www.tothenew.com
Contents
● Understanding the problem of shipping code
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello world demo
● Running stack of services using Docker compose
● Moving to Production
58. www.tothenew.com
Contents
● Understand the problem
● The solution
● What is Docker Container?
● Benefits of Docker
● Understanding Docker components & architecture.
● Installation
● Hello world demo
● Running Stack of services using Docker Compose
● Moving to Production
59. www.tothenew.com
Orchestration tools needed for docker cluster environment
● Provision servers
● Deploy
● Manage servers
Some of the available orchestration tools
● Docker swarm
● Centurion
● Amazon EC2 container service
Moving to Production
62. www.tothenew.com
Sample Demo: https://github.com/puneetbehl/gr8conf-docker-demo
Docker documentation: https://docs.docker.com
Docker tool-box https://www.docker.com/docker-toolbox
Docker hub: https://hub.docker.com/
Docker up and running from O'Reilly publication Authors: Karl Matthias & Sean P. Kane
Union file system : https://en.wikipedia.org/wiki/UnionFS
http://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-normal-virtual-
machine
Build and push images to docker hub https://youtu.be/QCEWQs6LwAk
Docker-compose https://youtu.be/kn_dUA6f29I
References
Notas do Editor
Let’s build the build the story, and start from the very ground of application.
In this step, we have added more developers which means we need to set-up runtimes on each machine with a very specific version and make sure that application is running. Talk about that going forward
Please note that we need an extra Proxy Server (Apache) in case of production and other environments.
As we need to set-up multiple environments I need to:
As DevOps, need to set-up everywhere again and again.
if installed wrong version, need to fix it everywhere
Set-up a new environment on completely different infrastructure.
Manually repeat.
Make sure/test that my application is working on all the environments every time I am going to deploy a new version.
Talk about how things are getting complex, what
Continue the story that now we break the application into one web application and multiple background workers so that if something breaks on one of the background workers it SHOULD NOT impact my application to which users are interacting. But the problem it introduces is that now
I need to test all applications on each machine where I am going to run.
I need to update deployment process on all of the servers.
Talk about current solution to this problem without Docker
Continue the story, that we decide to break application services into separate applications and create different services for different purposes. So now, we need multiple runtimes in order to run the application, more libraries, more complexities, more challenges, like Dependency Hell? and making sure that
everything runs on every machine.
everything runs on every environment
Adding a new person to the team.
Problem like “Works on my machine”
Challenges with deploying over multiple different infrastructure.
Build > Deploy > Run getting more and more complex.
This slide is like a review of all the challenges we discussed in the earlier flow.
Docker is an engine that
enables any payload to be encapsulated as lightweight, portable, self sufficient container.
can be manipulated using standard operations and run consistently on virtually any environment.
Explain how every service will run in an isolated environment.
Heavyweight Images
Image Layering
Heavyweight Images
Image Layering
Heavyweight Images
Image Layering
Scalability
Let’s say have a web application and all of sudden I get lot of users, so it’s
Portability
Move to new Machine
Let’s say I want to move my current set-up to a new machine, to a Cloud, to Physical network etc.
Deployment
Less risk while deploying to different env
Rollback - installed an application over server apart from code changes, so now I need to Rollback
Efficient Resource Utilization
Scalability
Let’s say have a web application and all of sudden I get lot of users, so it’s
Portability
Move to new Machine
Let’s say I want to move my current set-up to a new machine, to a Cloud, to Physical network etc.
Deployment
Less risk while deploying to different env
Rollback - installed an application over server apart from code changes, so now I need to Rollback
Efficient Resource Utilization
Scalability
Let’s say have a web application and all of sudden I get lot of users, so it’s
Portability
Move to new Machine
Let’s say I want to move my current set-up to a new machine, to a Cloud, to Physical network etc.
Deployment
Less risk while deploying to different env
Rollback - installed an application over server apart from code changes, so now I need to Rollback
Efficient Resource Utilization
Scalability
Let’s say have a web application and all of sudden I get lot of users, so it’s
Portability
Move to new Machine
Let’s say I want to move my current set-up to a new machine, to a Cloud, to Physical network etc.
Deployment
Less risk while deploying to different env
Rollback - installed an application over server apart from code changes, so now I need to Rollback
Efficient Resource Utilization
The technology is particularly appealing for developers because it is now easier than ever to make sure you develop, test and deploy using the same environment as your colleagues, resulting in less issues caused by differences or missing libraries. Docker also offers developers the flexibility to quickly run their apps anywhere, whether its on laptops, VMs or QA servers. More simply put, “Docker helps developers build and ship higher-quality applications, faster.”A clean, safe, hygienic and portable runtime environment for your app.
No worries about missing dependencies, packages and other pain points during subsequent deployments.
Run each app in its own isolated container, so you can run various versions of libraries and other dependencies for each app without worrying
Automate testing, integration, packaging…anything you can script
Reduce/eliminate concerns about compatibility on different platforms, either your own or your customers.
Cheap, zero-penalty containers to deploy services? A VM without the overhead of a VM? Instant replay and reset of image snapshots? That’s the power of Docker
“Docker interests me because it allows simple environment isolation and repeatability. I can create a run-time environment once, package it up, then run it again on any other machine. Furthermore, everything that runs in that environment is isolated from the underlying host (much like a virtual machine). And best of all, everything is fast and simple.”
-Gregory Szorc, Mozilla Foundation
http://gregoryszorc.com/blog/2013/05/19/using-docker-to-build-firefox/
The technology is particularly appealing for developers because it is now easier than ever to make sure you develop, test and deploy using the same environment as your colleagues, resulting in less issues caused by differences or missing libraries. Docker also offers developers the flexibility to quickly run their apps anywhere, whether its on laptops, VMs or QA servers. More simply put, “Docker helps developers build and ship higher-quality applications, faster.”A clean, safe, hygienic and portable runtime environment for your app.
No worries about missing dependencies, packages and other pain points during subsequent deployments.
Run each app in its own isolated container, so you can run various versions of libraries and other dependencies for each app without worrying
Automate testing, integration, packaging…anything you can script
Reduce/eliminate concerns about compatibility on different platforms, either your own or your customers.
Cheap, zero-penalty containers to deploy services? A VM without the overhead of a VM? Instant replay and reset of image snapshots? That’s the power of Docker
“Docker interests me because it allows simple environment isolation and repeatability. I can create a run-time environment once, package it up, then run it again on any other machine. Furthermore, everything that runs in that environment is isolated from the underlying host (much like a virtual machine). And best of all, everything is fast and simple.”
-Gregory Szorc, Mozilla Foundation
http://gregoryszorc.com/blog/2013/05/19/using-docker-to-build-firefox/
Sysadmins are finding the technology useful as well, because of the ability to standardize development environments among other reasons. “Docker helps sysadmins deploy and run any app on any infrastructure, quickly and reliably.”
Make the entire lifecycle more efficient, consistent, and repeatable
Increase the quality of code produced by developers.
Eliminate inconsistencies between development, test, production, and customer environments
Support segregation of duties
Significantly improves the speed and reliability of continuous deployment and continuous integration systems
Because the containers are so lightweight, address significant performance, costs, deployment, and portability issues normally associated with VMs
Sysadmins are finding the technology useful as well, because of the ability to standardize development environments among other reasons. “Docker helps sysadmins deploy and run any app on any infrastructure, quickly and reliably.”
Make the entire lifecycle more efficient, consistent, and repeatable
Increase the quality of code produced by developers.
Eliminate inconsistencies between development, test, production, and customer environments
Support segregation of duties
Significantly improves the speed and reliability of continuous deployment and continuous integration systems
Because the containers are so lightweight, address significant performance, costs, deployment, and portability issues normally associated with VMs
On the business side, the benefits may be huge. By simplifying the way we deploy apps and creating more manageable environments, Docker can streamline all related processes, potentially offering a boost in productivity, reducing risks and reducing costs.
On the business side, the benefits may be huge. By simplifying the way we deploy apps and creating more manageable environments, Docker can streamline all related processes, potentially offering a boost in productivity, reducing risks and reducing costs.
On the business side, the benefits may be huge. By simplifying the way we deploy apps and creating more manageable environments, Docker can streamline all related processes, potentially offering a boost in productivity, reducing risks and reducing costs.
On the business side, the benefits may be huge. By simplifying the way we deploy apps and creating more manageable environments, Docker can streamline all related processes, potentially offering a boost in productivity, reducing risks and reducing costs.
Docker daemon runtime, runs on host machine, spin up containers, explain how it’s different from Windows and Mac
Docker daemon runtime, runs on host machine, spin up containers, explain how it’s different from Windows and Mac
Docker daemon runtime, runs on host machine, spin up containers, explain how it’s different from Windows and Mac
Docker daemon runtime, runs on host machine, spin up containers, explain how it’s different from Windows and Mac
Give example of image
Give example of image
Give example of image
Give example of image
Explain the whole workflow here
Installation is very easy on linux boxes. You can use apt-get or yum to install docker-engine. You don’t need a virtualbox on linux box as the docker runs natively on linux box.
Download and run docker toolbox installer on Windows and Mac OS. You are all set with docker environment on your computer. What you install is actually:d ocker client, server, machine, compose, kitematic and virtualbox. We will talk about compose and kitematic later.
Source code of the slide is a complete web application which can be run using spring boot CLI. No need to worry if you are not aware of Spring boot. I can easily create a jar file with embedded tomcat and other jar dependencies using spring boot CLI tool. To execute jar file you just need Java and nothing else. So the jar file and the java dependency is of more interest in demo.
You can see how easy it to create jar file and run it using java.
Let me check the files in current directory. I have already created the jar file. Let me try this to run using docker container.
docker run java:8 java -jar hello.jar
So command what actually does is to instruct docker server to fetch java image with tag 8. Docker service will first look into local cache and if it is found there, the cached image will be used to create the container. Once the container is created it will execute the command that you have specified in out case execute the jar file. Lets try to run this.
Oops it failed. It was unable to find hello.jar. Very surprising as you already seen this in directory listing that the file exists. Let’s recall the concept of Container. Container runs in isolation and does not have access to files outside the container file system. That’s why hello.jar is not there.
We have to hello.jar available to the container by some means. One quick solution is to mount working dir as a volume to this folder. Lets try this.
docker run -v $PWD:/app java:8 java -jar /app/hello.jar
so all the files and folders of present working directory will be available in /app folder of the container.
Lets try to find the usual text that says tomcat is ready and listening on 8080 port. Here it is.
Lets access this web app using browser. http://localhost:8080 . Hmmm it’s still not working. It seems that nothing is running on port 8080. Any guess what might be the issue.
Well it’s not just the file system that is isolated, the whole network is isolated from the host machine. We have to explicitly publish container’s port on host machine. Let me try this.
docker run -v $PWD:/app -p 8080:8080 java:8 java -jar /app/hello.jar
Violla, its working now.
Lets prepare a docker image to pack all the dependencies that we passed as command line argument so far.
So what’s special with Dockerfiles. It’s just a simple text file containing instructions or commands for
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Here I am instructing to use java:8 image as a base image.
Adding some meta information.
Copy some file to container.
Expose 8080 port so that host machine can bind to it.
Run java command
Each instruction will be added as a layer and cached locally. Next time you rebuild the image the, Docker will use cached version of layer whenever possible.
Let’s build the image and and push it to Docker hub so that it will be available globally.
file list
check Dockerfile content
docker build -t imagename
docker history to check layers
Let me adjust the zoom so that it is readable
Lets push the image
Each layer is pushed to docker hub
Go to docker hub site
Real life application are not so simple. I have an application for managing TODO. I have divided the application into services which can be independently developed. Noticalble components are the backend, frontend and worker. Backend application uses MongoDB for persistence and spring framework for exposing REST endpoints to manage TODOs. Worker is responsible for sending notifications and depends on Backend service. Worker is a lightweight Java application using Quartz scheduler which sends notification in the form of email and SMS. The frontend application has been written using Nodejs and Angularjs and provides a nice UI to manage TODO. End user can access frontend app from browser. It interacts with backend service REST endpoints.
Docker-compose is a nice tool for managing this type of distributed application consisting of many small applications.
Docker-compose will take care of building image or pulling it from docker hub for each services described your docker-compose.yml file. It will take of linking of services by making necessary changes to /etc/hosts file so that the linked service can be referenced by name instead of hardcoded IP address.
You can see the docker-compose.yml file content on the slide. It describes 3 services: mongodb, backend and frontend. Mongodb is linked in backend service. Backend service is linked to frontend service. Notice backendServiceUrl environment variable which is using backend instead of IP address to refer to the backend application.
Dcoker is installed
Lets check version of docker client and server
Let’s downloaded images > none
Lets check containers if any running or in stopped state > none
java, mongo client, mongodb, nodejs > not installed
what files are available
check docker-compose file content
let’s compose it …. optional you can specify -d for detached
it will check fetch images from local cache or docker hub
once the image is ready it will create the container and bind the linked services if any
Docker-compose is suitable for small applications and is very handy for development.
Production environment is not that simple. You usually have cluster of services and you want a tool that can manage those clusters and keep running the required containers. Scaling of containers should be easily done irrespective of which machine is being used for running new containers.
There are many orchestration tools available. Docker swarm is one of them. You can explore those on your own.