58

Docker up and Running For Web Developers

  • Upload
    badr

  • View
    79

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Docker up and Running For Web Developers
Page 2: Docker up and Running For Web Developers

Docker Up and Running for Web Developers

Amr Fawzy Mohammed

Page 3: Docker up and Running For Web Developers

Outline

● What is Docker ?● Docker Overview● Why Docker ?● What is the Docker platform ?● Union file systems● What happens when you run a container ?● Install Docker● Managing Images and Containers● Docker Networking● Get source code into a Container● Volumes● Communication between Containers

Page 4: Docker up and Running For Web Developers

● First we need to know what Containers actually are or what they bring to the table, in order to do this we need to briefly review the history of an application's runtime environment.

What is Docker ?

Page 5: Docker up and Running For Web Developers

● In the beginning we used to build applications directly on physical servers in 1:1 ratio.

Application's Runtime Environment

Page 6: Docker up and Running For Web Developers

● Disadvantages of this technique:○ Every application requires a ton of infrastructure.○ It takes a lot of time to deploy or scale an app.○ Financially, that was a highly expensive. ○ It causes massive waste of resources.

■ Most of the time these servers run at a tiny fraction of their capabilities.

Application's Runtime Environment cont.

Page 7: Docker up and Running For Web Developers

● This solution was not meant to be last for a long time.

● Virtualization takes place.

Application's Runtime Environment cont.

Page 8: Docker up and Running For Web Developers

● Virtualization technology enables to build multiple virtual machines on top of a physical machine which means running multiple applications on top of a single physical server.

Application's Runtime Environment cont.

Page 9: Docker up and Running For Web Developers

Application's Runtime Environment cont.

Page 10: Docker up and Running For Web Developers

● Each virtual machine has it's own OS which is considered a huge overhead and a massive waste of the host physical machine's resources.

Application's Runtime Environment cont.

Page 11: Docker up and Running For Web Developers

● The efficient solution was using Containers.

Application's Runtime Environment cont.

Page 12: Docker up and Running For Web Developers

● Containers are way more lightweight than Virtual machines (no Guest OS).

● Each Container consumes less CPU,RAM,Disk space than a VM, But still provides a secure isolated runtime environment for the application.

Application's Runtime Environment cont.

Page 13: Docker up and Running For Web Developers

Application's Runtime Environment cont.

Page 14: Docker up and Running For Web Developers

● Technologies that allow you to package and isolate applications from the rest of the system.

● Containers make it easy to Deploy Applications without

massive headaches, rewriting, and break-fixing.

What are containers ?

Page 15: Docker up and Running For Web Developers

● Chroot system call○ Seeds for today’s containers were planted in 1979 with the

addition of the chroot system call to Version 7 Unix.

● FreeBSD jail○ In 2000, FreeBSD 4.0 was released with a new command called

jail which expanded chroot’s capabilities.

A brief history of containers

Page 16: Docker up and Running For Web Developers

● Solaris Zones○ In 2004, Sun released an early build of Solaris 10, which

included Solaris Containers, and later evolved into Solaris Zones.

● HP-UX Containers○ In 2007, HP released Secure Resource Partitions for HP-UX later

renamed to HP-UX Containers.

A brief history of containers cont.

Page 17: Docker up and Running For Web Developers

● Linux Containers (LXC)○ In 2008, Linux Containers (LXC) were released in version 2.6.24

of the Linux kernel.

● Docker○ In 2013, the phenomenal growth of Linux Containers starts to

grow with the inclusion of user namespaces in version 3.8 of the Linux Kernel and the release of Docker one month later.

A brief history of containers cont.

Page 18: Docker up and Running For Web Developers

● Docker is a platform for Developers and System Admins to develop, ship, and run applications.

● Docker provides the ability to package and run an application in a loosely isolated secured environment called a container.

Docker Overview

Page 19: Docker up and Running For Web Developers

● Faster delivery of your applications.

● Faster deployment makes for easier management.○ Docker speeds up the work flow, so it gets easier to make lots of

small changes instead of huge updates.○ Smaller changes mean reduced risk and more uptime.

Why Docker ?

Page 20: Docker up and Running For Web Developers

● Docker helps developers to care about their applications inside containers while sysadmins work on running the container in your deployment.

● “This separation of duties streamlines and simplifies the management and deployment of code”

Why Docker ? Cont.

Page 21: Docker up and Running For Web Developers

● Deploy and scale more easily.

● Docker containers run (almost) everywhere.

● Ensure consistency between environments.

Why Docker ? Cont.

Page 22: Docker up and Running For Web Developers

● We can run more containers on a given hardware combination than if you were using virtual machines.

Why Docker ? Cont.

Page 23: Docker up and Running For Web Developers

● Eliminate applications conflicts.● Less OS maintenance (only the host OS).● Ultimate portability between multiple environment.● Setup development environment very quickly.● Ship software faster.● Fast deployment.● Allows very easy scalability.● Opens massive opportunities for automated testing.● Moves control of the environment to the development

team.

Summarizing Docker Benefits

Page 24: Docker up and Running For Web Developers

● Docker provides a platform to manage the lifecycle of your containers:○ Encapsulate your applications into Docker containers.○ Distribute and ship those containers to your teams for further

development and testing.○ Deploy those applications to your production environment.

What is the Docker platform ?

Page 25: Docker up and Running For Web Developers

● Docker Engine ○ lightweight and powerful open source container virtualization

technology combined with a workflow for building and containerizing applications.

● Docker Registries

Docker Platform Components

Page 26: Docker up and Running For Web Developers

Docker Platform architecture

Page 27: Docker up and Running For Web Developers

● A client-server application with these major components:○ Server (docker daemon)○ A REST API ○ Client (CLI)

Docker Engine

Page 28: Docker up and Running For Web Developers

● The Docker daemon runs on a host machine.

● The user uses the Docker client to interact with the daemon.

● The daemon creates and manages Docker objects, such as images, containers, networks, and data volumes.

Docker Daemon

Page 29: Docker up and Running For Web Developers

● In the form of the docker binary.● It accepts commands from the user and communicates

with the Docker daemon.● One client can communicate with multiple unrelated

daemons.

Docker Client

Page 30: Docker up and Running For Web Developers

● Union file systems allow files and directories of separate file systems, known as branches, to be transparently overlaid, forming a single coherent file system.

● Docker uses union file systems to combine these layers into a single image.

Union file systems

Page 31: Docker up and Running For Web Developers

Union file systems cont.

● These layers are one of the reasons Docker is so lightweight.○ When you change a Docker image, a new layer is built and

replaces only the layer it updates.○ To distribute the update, you only need to transfer the updated

layer.

● Layering speeds up distribution of Docker images.

Page 32: Docker up and Running For Web Developers

● A docker image is a read-only template.

● Docker images are the basis of the Docker containers.

● Docker images are the build component of Docker.

● You can build images from scratch or download and use images created by others.

Docker images

Page 33: Docker up and Running For Web Developers

● Dockerfile is a text document that contains all the commands and instructions that Docker can use to automatically build images.

● Every image starts from a base image such as ubuntu, fedora or an image of your own such as Apache, Nginx, Ruby, etc.

Dockerfile

Page 34: Docker up and Running For Web Developers

Dockerfile cont.

● Each instruction in the Dockerfile creates a new layer in the image.

● Each image consists of a series of layers.

● Creating Image from Dockerfile○ sudo docker build -t $image_name .

Page 35: Docker up and Running For Web Developers

● FROM : Specify the base image ● MAINTAINER : Specify the image maintainer● RUN : Run a command● ADD : Add a file or directory● EXPOSE : expose ports to be accessed ● ENV : Create an environment variable● CMD : What process to run when launching a container

from this image.

Some Dockerfile instructions

Page 36: Docker up and Running For Web Developers

● A Docker container is a runnable instance of a Docker image.

● You can run, start, stop, move, or delete a container using Docker CLI commands.

● Docker containers are the run component of Docker.

Docker containers

Page 37: Docker up and Running For Web Developers

Images, Containers and layers

Page 38: Docker up and Running For Web Developers

● A docker registry is a library of images.

● A registry can be public or private.

● Can be on the same server as the Docker daemon or Docker client, or on a totally separate server.

● Docker registries are the distribution component of Docker.

Docker registries

Page 39: Docker up and Running For Web Developers

● $ docker run -i -t ubuntu /bin/bash ● This command tells the Docker daemon to create and

start a container using the ubuntu image, run it in an interactive mode (-i),Allocate a pseudo-TTY and to run the /bin/bash command.

● So, what actually the Engine does behind the scene ?!

What happens when you run a container ?

Page 40: Docker up and Running For Web Developers

● Pulls the ubuntu image.● Creates a new container.● Allocates a filesystem and mounts a read-write layer.● Allocates a network / bridge interface.● Sets up an IP address.● Executes the /bin/bash executable.● Captures and provides application output.

○ (due to interactive mode)

What happens when you run a container? Cont.

Page 41: Docker up and Running For Web Developers

● Docker Engine is supported on Linux, Cloud, Windows, and macOS.

● For Linux use the script on this link :− https://get.docker.com/

● For Windows or Mac Docker can be installed using Docker Toolbox

Install Docker

Page 42: Docker up and Running For Web Developers

● Consists of :○ Docker Client○ Docker Machine○ Docker kitematic○ Docker Compose

Docker toolbox

Page 43: Docker up and Running For Web Developers

● Docker machine is a tool that can be used to ○ Create Docker hosts on your Mac or Windows box, on your

company network, in your data center, or on cloud providers.○ Manage this host (start,stop,restart,..etc).○ Upgrade the Docker client and daemon.○ Configure a Docker client to talk to your host.

Docker Machine

Page 44: Docker up and Running For Web Developers

● docker-machine ls● docker-machine start vm-name● docker-machine stop vm-name● docker-machine env vm-name● docker-machine ip vm-name

Docker Machine cont.

Page 45: Docker up and Running For Web Developers

● sudo docker search $image_name○ Search for an image on Docker hub repository.

● sudo docker pull $image_name○ Pull the required image from docker hub repository.○ Update the installed images if there is any new updates.

● sudo docker images○ List all images on the system.

● sudo docker rmi $image_name○ Remove an image.

Managing Images and Containers

Page 46: Docker up and Running For Web Developers

● sudo docker run --name $cont_name -d $image_name ○ Spin up a container from an image and make it run in the

background.● sudo docker ps

○ List the currently running containers.● sudo docker ps -a

○ List all the containers whether they are running or not.

Managing Images and Containers cont.

Page 47: Docker up and Running For Web Developers

● sudo docker rm $container_name○ Remove a stopped container.

● sudo docker rm -f $container_name○ Force remove a container.

● sudo docker stop $container_name○ Stop a running container.

● sudo docker inspect $container_name OR $image_name○ This displays all the information available in Docker for a given

container or image.

Managing Images and Containers cont.

Page 48: Docker up and Running For Web Developers

● sudo docker exec -it $container_name <command>○ Attach to the the running container.○ Modify this running container.

● sudo docker commit $container_name $image_name○ Commit the modified container into image.

Creating Image from a modified container

Page 49: Docker up and Running For Web Developers

● When Docker creates a container, it creates two virtual interfaces, one of which sits on the server-side and is attached to the docker0 bridge, and one that is exposed into the container’s namespace.

Docker Networking

Page 50: Docker up and Running For Web Developers

● sudo docker run -d -p 8080 $image_name○ Port mapping.○ Publish port 8080 from inside the container to the docker Host

on a random port (say 1234), So that the app running on port 8080 inside the container will be accessed through the host IP on that random port.

● sudo docker run -d -p 3000:8080 $image_name○ Port redirection.○ Publish port 8080 from inside the container to the docker Host

on port 3000.

Docker Networking cont.

Page 51: Docker up and Running For Web Developers

Docker Networking cont.

● sudo docker run -d --net=host $image_name○ Host Networking.○ Share the host IP address.

● sudo docker inspect $container_name | grep IPAddress○ Get the IP Address of the running container.

Page 52: Docker up and Running For Web Developers

Inbound and Outbound Traffic

Page 53: Docker up and Running For Web Developers

● Create a container volume that points to the source code.

● Add your source code into a custom image that is used to create a container.

Get source code into a Container

Page 54: Docker up and Running For Web Developers

● A special type of directories in a container typically referred to as a “data volume”.

● Can be shared and reused among containers.

Volumes

Page 55: Docker up and Running For Web Developers

Volumes cont.

● sudo docker create --name mydataContainer -v /usr/share/nginx/html ubuntu:latest

● sudo docker run --name

mywebserverContainer --volumes-from mydataContainer -d nginx

● Sudo docker run --name myApp -p 8080:3000 -v /local/fs/path/:/container/fs/path -w “/var/www” -d node npm start

Page 56: Docker up and Running For Web Developers

Communication between Containers

● Linking containers with names by using --link option ○ sudo docker run --name myMongodbContainer -d mongo○ sudo docker run --name railsAppContainer -p

3005:3000 --link myMongodbContainer:mongodbContainer -d afawzy/integrationImage

Page 57: Docker up and Running For Web Developers

Communication between Containers

● Create a custom bridge network and add containers into it.○ sudo docker network create --driver bridge

isolated_network○ sudo docker run --name mongodbContainer

--net=isolated_network -d mongo○ sudo docker run --name railsAppContainer

-p3005:3000 --net=isolated_network -d afawzy/integrationImage

Page 58: Docker up and Running For Web Developers

References

● https://docs.docker.com/● man docker● Docker: Up & Running: Shipping Reliable Containers in Production● Docker: Intro - CBT Nuggets● Docker for Web Developers - Pluralsight● Docker Deep Dive - Pluralsight● Learn how to deploy Docker applications to production - udemy