Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Getting Started with Docker

639 views

Published on

Introduction to docker Containerization vs Virtualization Command Reference Best Practices Getting Started Guide Deployment Workflow Diagrams

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Getting Started with Docker

  1. 1. Getting started with Docker Geeta Vinnakota
  2. 2. Next 45 minutes  What is docker and how can it help us?  What is containerization and why does that matter?  How is containerization different from virtualization? How to install docker on Linux? What is an image? How do you build an image?  What is a Dockerfile and why is it necessary? How is a container different from an image?  How does a docker deployment work flow with look like? Command Reference 2/4/2016 1
  3. 3. Why Docker? • Eliminate “But it works on my computer!” nightmares during deployments, by exactly mirroring development and production environments • Support quick replication of new production servers ~ within minutes • Allows quick rollbacks ~ within minutes • Advanced multi-container dockers play well with micro-service architecture by enabling de-coupling and portability ( Keep an eye out and ask yourself, how these benefits are realized in the workflow, as you proceed with the deck ) 2/4/2016 2
  4. 4. What is Docker? • What is Docker? • Docker manages containers • Docker allows shipping applications, code and services in containers • What is a container? • Containers can be compared to virtual machines, but much light-weight, faster with less overhead • Containers are isolated, independent, portable and easily reproducible • Containerization vs Virtualization • For an abstract view, containerization can be compared with virtualization 2/4/2016 3
  5. 5. Virtualization 2/4/2016 4 Image by Laura Frank, Sr Developer at Codeship • Here is a host computer with an installed hypervisor • Hypervisor supports virtual machines on a host computer • There are 3 virtual machines installed on a host in this diagram • They share the resources of the host computer
  6. 6. Containerization 2/4/2016 5 Image by Laura Frank, Sr Developer at Codeship • Here is another host computer • Notice, that there is no hypervisor or guest OS
  7. 7. Containerization 2/4/2016 6 Image by Laura Frank, Sr Developer at Codeship • Instead of a hypervisor, there is a container engine • 3 containers are running on the container engine Docker ecosystem provides us with a container engine along with a set of tools that help us build and manage containers
  8. 8. Installation 2/4/2016 7
  9. 9. Installating docker on Linux (ubuntu) sudo apt-get update get latest package version curl –sSL https://get.docker.com/ | sh install docker docker -v check version sudo usermod –aG docker ubuntu add default user ubuntu to docker group docker info docker history • Linux – Package Installation described above • Mac/Windows – Docker Toolbox. ( boot2docker is old and deprecated )
  10. 10. Images 2/4/2016 9
  11. 11. What is an image? • An image is a template for a container • It could be a simple command or complex software • An image layer is a file system that is read-only • A R-W file system is installed on top of the image layers 2/4/2016 10
  12. 12. How image builds work? • After each RUN docker looks for a matching image in the build cache • Build command compresses the context into an archive/tar and sends it to the daemon 2/4/2016 11
  13. 13. How is an image different from a container? Image • Image is similar to a class in programming • Image ~ Blueprint • Image is what you store on docker hub & pass around Container • Container is similar an instance of the class • Container ~ Actual Object • You do not store containers anywhere or pass them around 2/4/2016 12 • Consider the following differences and analogies to avoid confusion between images and containers.
  14. 14. Types of Images Service Image Project Base Image Official Image • Offers a service out of the box • You have to do very little to get it functioning • Nginx, Postgres, MySQL • No service is offered out of the box • Ruby, Language based images • Add your own code on top of the image • Maintained by the organization or company itself • An official image can serve as a service or project based image 2/4/2016 13
  15. 15. Image Command Reference Build an Image docker build –t repo_name/img_name:tag . build images from Dockerfile and context -t Tag the image with a repo name Repo Name – username/imagename Rename docker tag local_repo:tag docker_hub_repo:tag Rename an image docker tag img_id docker_hub_repo:tag Rename an image List docker images List images Remove docker rmi –f img_id/img_name Remove image Inspect Details docker inspect img_id/img_name List port bindings, config Login to Docker Hub docker login –username=uname – password=pw –email=email Pull Image from Hub docker pull username/image_name:tag
  16. 16. What is Dockerfile? • A Dockerfile lets you create your own image • It is a series of instructions to build your own image • INSTRUCTIONS are typically upper case • Best Practices • Split long commands with backslashes • Prefer COPY to ADD • Minimize image layers by combining multiple RUN commands to avoid multiple intermediate images, keeping readability in mind. 2/4/2016 15
  17. 17. Dockerfile Instruction Reference Instruction Example Details FROM FROM nginx Mandatory. First instruction. Specifies base image RUN RUN rm /etc/nginx/defa ult.conf Execute the command while on the container WORKDIR WORKDIR /users/dan Specify the working directory for the RUN commands By default they are run in the root directory CMD ping localhost Specify the default command of the image, that is run, when the container is launched. If there are multiple, the last one takes precedence Process that runs with the container
  18. 18. Dockerfile Instruction Reference EXPOSE EXPOSE 80 Expose ports on the container. Does not map them to hosts VOLUME VOLUME /var/log/nginx Creates a mount point with the specified name. Marks it as holding volumes from native host Does not map any volumes from the host COPY COPY build/artifacts /usr/share/nginx/html Copies files from host to container ADD Similar to COPY, but has additional capabilities of uncompressing tar files, fetching files from remote locations Prefer COPY to ADD unless specifically required ENTRYPOINT Similar to CMD
  19. 19. Containers 2/4/2016 18
  20. 20. What is a container? • Typically containers run a single process(default command). Containers can be thought of, as the process they run. • Container runs only as long as the default command is running • Container itself is a process in the host machine. Any process run by the container is a child process • Containers have internal IP address 2/4/2016 19
  21. 21. How is a container different from a Virtual Machine • Container looks and operates like a Virtual Machine, but it is not a VM • Containers are light weight. Spin up faster • A host can accommodate more containers than VMs • Containers run on top of docker engine 2/4/2016 20
  22. 22. Container Best Practices & Usecases • Best Practices • Specify names for containers to avoid strange auto generated names • Containers are not suitable for persistent data, as they are ephemeral and should allow stopping, destroying and recreating • Do not run ssh servers in containers unless needed • Use docker commands to check logs, restart processes, tweak configuration, run upgrades • Usecases • Deploy web front-end applications • Deploy web APIs • Run Maintenance scripts 2/4/2016 21
  23. 23. Container Command Reference Purpose Example Details Run a container docker run –name docker-nginx –p 80:80 –d nginx Creates a container with name docker-nginx from image nginx Flags for run -d Detached mode. Container runs in the background and keeps running until manually stopped -p 80:80 Map local machine port 80 to container port 80 Inspect docker inspect container_id | grep IpAddress List docker ps -a List containers including those that are stopped
  24. 24. Troubleshooting a Container Stop a container docker stop c_id Stop container with id c_id Remove docker rm c_name Remove container with name c_name Enter a container docker exec –it c_id bash Takes you inside of a running container to the bash -it interactive ( also works with run ) View logs from container docker logs c_id print logs of pid 1 process from the container docker logs –f c_id Follow logs from the container docker logs –f –tail 10 c_id Follow logs from the last 10 lines
  25. 25. What is Docker Hub? • Place to store and distribute docker images • Think of it as github but for docker images • Docker logo or prefix of library ‘library/nginx’ denotes official images in the hub • 70+ official, 100K+ regular, 300M+ downloads 2/4/2016 24
  26. 26. Traditional Deployment Workflow 2/4/2016 25
  27. 27. 2/4/2016 26 • In a traditional deployment flow, you start with the host machine Traditional Deployment Workflow – Step 1 of 5
  28. 28. 2/4/2016 27 • Install the necessary tools and services • Start with Installing Nginx Nginx Traditional Deployment Workflow – Step 2 of 5
  29. 29. 2/4/2016 28 • Then more software provisioning and installations • Ruby platform Nginx Ruby Traditional Deployment Workflow – Step 3 of 5
  30. 30. 2/4/2016 29 • Install the necessary dependencies Nginx Ruby Dependencies Traditional Deployment Workflow – Step 4 of 5
  31. 31. 2/4/2016 30 • Finally deploy the application code and tie everything together into an ecosystem Nginx Ruby Dependencies Application Traditional Deployment Workflow – Step 5 of 5
  32. 32. Deployment Workflow with Docker 2/4/2016 31
  33. 33. 2/4/2016 32 • In Docker deployment flow, we start with installing docker enginer on the host computer • Cloud providers like AWS provide machines pre-provisioned with docker, ready to use Docker Engine Deployment Workflow with Docker – Step 1 of 2
  34. 34. 2/4/2016 33 • Ship the entire ecosystem, as a container from your development environment ( as opposed to provisioning each software separately ) Nginx Ruby Dependencies Application Docker Engine Deployment Workflow with Docker – Step 2 of 2
  35. 35. Production Server Docker Image Prd Deploy Package Staging Server QA Deploy Package Development Machine Image = Application Code + Software Needed to Run the code Deploy Package = Location of Image on Hub + Access Credentials • Image is nothing but a versioned artifact • Notice how all 3 versions run the same image • Which is what leads to no surprise deploys ( exact mirroring of development, staging and production servers
  36. 36. 2/4/2016 35 Code Change Create new docker image docker build –t repo/username:tag . Push image to docker hub docker push repo/username:tag Update image tag version in dockerrun.aws.json Web or CLI Create a zip file (use gulp) with dockerrun.aws.json & .ebextensions. Upload it via webconsole CLI ’eb deploy’ Uploads a zip of dockerrun.aws.json & .ebextensions Web CLI Deploy Workflow Docker using EB (Elastic Beanstalk) Tells Elastic Beanstalk the location of the docker image Eliminate unrequired files in image build by adding .dockerignore Tell EB to to pick only dockerrun.aws.json while create zip via .ebignore
  37. 37. Advanced Features 2/4/2016 36
  38. 38. Volumes • Volumes are mounted when creating a container • Data in this volume is persistent even if the container is deleted • Volumes are independent of the container lifecycle • Volumes can be shared between containers • Specify volumes in dockerfile using VOLUME instruction • Best Practices: • Mounting volumes from host for persistent data is not recommended as it binds you to that host • Cannot map volumes from host using dockerfile as it is intended to work on any host 2/4/2016 37
  39. 39. Container Linking • Container connection without using any network ports • Step-1: Create the source container • Step-2: Create the recipient container, with link to the source. It creates an entry in the recipient with an alias and IP of the source • Usecase: when you have separate web and database containers • Source and recipient Containers can be on the same or different hosts 2/4/2016 38

×