Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TechEd 2018, Introduction to Docker

TechEd 2018, Introduction to Docker

Ondrej Sika

May 17, 2018

More Decks by Ondrej Sika

Other Decks in Programming


  1. Introduction to Docker #TechEdDevCon Ondrej Sika [email protected]ejsika.com @ondrejsika TechEd, Prague,

    17. 5. 2018
  2. Agenda - What is a Docker? - Usage - Containers

    - Images - Docker Registry - Composites - Swarm
  3. What is Docker?

  4. What is Docker? Docker is an open-source project that automates

    the deployment of applications inside software containers. ... Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server.
  5. Containers vs virtualization

  6. Virtualization A VM is an abstraction of physical hardware. Each

    VM has a full server hardware stack from virtualized BIOS to virtualized network adapters, storage, and CPU. That stack allows run any OS on your host but it takes some power.
  7. Containers Containers are abstraction in linux kernel, just proces, memory,

    network, … namespaces. Containers run in same kernel as host - it is not possible use different OS or kernel version, but containers are much more faster than VMs.
  8. History of containers - 1979 - Chroot - 2000 -

    BSD Jails - 2001 - VServer - 2004 - Solaris Containers - 2005 - OpenVZ - 2006 - Process Containers - 2008 - LXC - 2013 - Docker
  9. Advantages - Performance - Management - Containers distribution

  10. Disadvantages - Security - One kernel / "Linux only"

  11. Usage - Almost everywhere - Development, Testing, Production - Better

    deployment process - Separates running applications
  12. Work with Cluster managements - Swarm (Native) - Open Stack

    - Kubernetes
  13. Installation

  14. https://docs.docker.com/engine/installation/

  15. Test the installation docker run hello-world ... Hello from Docker!

    This message shows that your installation appears to be working correctly.
  16. Basic Usage

  17. Image and Container An image is an inert, immutable, file

    that's essentially a snapshot of a container. Images are created with the build command, and they'll produce a container when started with run. Images are stored in a Docker registry..
  18. Help docker docker help docker help <command> # eg. docker

    help run, docker help help
  19. System wide info docker version # print version docker system

    info # system vide information docker system df # docker disk usage docker system prune # cleanup unused data
  20. Docker Images docker image ls # list all images docker

    image ls -q # quiet output, just IDs docker image rm <image> # remove image docker image pull <image> # pull from registry docker image push <image> # push to registry docker tag image <old> <new> # add new tag
  21. Docker Run docker run <image> [<command>] # Eg.: docker run

    hello-world docker run debian:8 cat /etc/os-release docker run debian:9 cat /etc/os-release docker run -ti debian
  22. Common Docker Run Params --name <name> -d - run as

    daemon -ti - map TTY a STDIN (for bash eg.) -e <variable>=<value> - set ENV variable -h <hostname> - set hostname -u <user> - run command by specific user --rm - remove container after stop
  23. Containers docker ps - list containers docker start <container> docker

    stop <container> docker restart <container> docker logs <container> # show STDOUT & STDERR docker rm <container> # remove container
  24. List containers docker ps - list running containers docker ps

    -a - list all containers docker ps -a -q - list IDs of all containers # Eg.: docker rm -f $(docker ps -a -q)
  25. Docker Exec docker exec <container> <command> -d - run command

    as daemon -e <variable>=<value> - set ENV variable -ti - map TTY a STDIN (for bash eg.) -u <user> - run command by specific user # Eg.: docker exec my-debian ls
  26. Docker Logs docker logs [-f] <container> # Eg.: docker logs

    my-debian docker logs -f my-debian # following
  27. Docker Inspect Get lots of information about container in JSON.

    docker inspect <container>
  28. Docker Volumes Volumes are persistent data storage for containers. Volumes

    can be shared between containers and data are written directly to host.
  29. Volumes docker run -ti -v /data debian docker run -ti

    -v my-volume:/data debian docker run -ti -v $(pwd)/my-data:/data debian
  30. Port Forwarding Docker can forward specific port from container to

  31. Port forwarding docker run -p <host port>:<cont. port> <image> #

    eg.: docker run -ti -p 8080:80 nginx
  32. Own Images

  33. Dockerfile Dockerfile is preferred way to create images. Dockerfile defines

    each layer of image by some command. To make image use command docker build
  34. Dockerfile FROM <image> - define base image MAINTAINER <maintainer> -

    set maintainers name & email RUN <command> - run command and save as layer COPY <local path> <container path> - copy file or directory to image layer ADD <source> <container path> - instead of copy, archives added by add are extracted
  35. Dockerfile ENV <variable> <value> - set ENV variable USER <user>

    - switch user WORKDIR <path> - change working directory VOLUME <path> - define volume ENTRYPOINT <command> - command, run on container starts CMD <command> - parameters for entrypoint
  36. .dockerignore Ignore files for docker build process. Similar to .gitignore

  37. .dockerignore # comment */temp* */*/temp* temp?

  38. Build Image from Dockerfile docker build -t <image> <path> docker

    build -f <dockerfile> -t <image> <path> docker tag <source image> <target image> # eg.: docker build -t my-image .
  39. Example

  40. app.py import os from flask import Flask from redis import

    Redis app = Flask(__name__) redis = Redis('redis') hostname = os.environ.get('HOSTNAME') @app.route("/") def index(): return '%s %d' % (hostname, redis.incr('counter')) app.run(host='', port='80')
  41. requirements.txt flask redis

  42. Dockerfile FROM python:3.6-alpine COPY requirements.txt /app/requirements.txt WORKDIR /app RUN pip

    install -r requirements.txt COPY . /app CMD python app.py
  43. https://github.com/ondrejsika/teched18

  44. Run docker build -t teched18 . docker run --name redis

    -d redis:alpine docker run -p 80:80 --link redis teched18
  45. Docker Registry

  46. What is Docker Registry? A service responsible for hosting and

    distributing images. The default registry is the Docker Hub. GitLab contains Docker Registry from version 8.
  47. What is Docker Hub? Docker Hub is default public docker

    registry. You can host unlimited free images. Docker Hub is source of our base images.
  48. Docker registry docker login - Login to Docker Registry docker

    logout - Logout from Docker Registry docker pull <image> - download image from registry docker push <image> - upload image to registry
  49. Image name format # Official image from Docker Inc on

    Docker Hub debian # Image in user namespace on Docker Hub ondrejsika/debian # Image on own Docker Registry (reg.istry.cz) reg.istry.cz/debian
  50. Docker Compose

  51. What is Docker Compose? Compose is a tool for defining

    and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services.
  52. Install Docker Compose

  53. https://docs.docker.com/compose/install/ https://docs.docker.com/compose/completion/

  54. Compose File A docker-compose.yml file is a YAML file that

    defines how Docker containers should behave in production.
  55. Example Compose File version: '3' services: counter: build: . image:

    reg.istry.cz/counter ports: - 80:80 redis: image: redis:alpine
  56. https://docs.docker.com/compose/compose-file/

  57. Service Service is a container running and managed by Docker

  58. Build services: app: build: . services: app: build: context: ./app

    dockerfile: Dockerfile-prod image: myapp
  59. Image services: app: image: redis:alpine

  60. Port forwarding services: app: ports: - 8000:80

  61. Volumes services: app: volumes: - /data1 - data:/data2 - ./data:/data3

  62. Environment variables services: app: environment: RACK_ENV: development SHOW: 'true' SESSION_SECRET:

    services: app: environment: - RACK_ENV=development
  63. Command, Entrypoint services: app: entrypoint: ["python", "app.py"] command: ["80"]

  64. Deploy services: app: deploy: placement: constraints: [node.role == manager] services:

    app: deploy: mode: replicated replicas: 4
  65. Create a Composite

  66. Create docker-compose.yml version: '3' services: app: build: . image: reg.istry.cz/teched18

    ports: - 80:80 redis: image: redis:alpine
  67. Compose Commands

  68. Basic Compose Commands docker-compose config docker-compose help docker-compose ps docker-compose

    exec <service> <command> docker-compose version docker-compose logs [-f] [<service>]
  69. Build Compose docker-compose build docker-compose build --no-cache

  70. Run Compose docker-compose up docker-compose up -d # Missing images

    will be downloaded or builded
  71. Compose Up Arguments -d - run as daemon --force-recreate -

    always create new cont. --build - build on every run --no-build - don't build, even images not exist --remove-orphans --abort-on-container-exit
  72. Manage Compose docker-compose start [<service>] docker-compose stop [<service>] docker-compose restart

    [<service>] docker-compose kill [<service>]
  73. Remove Compose docker-compose down # stop and remove compose

  74. Docker Swarm

  75. What is Docker Swarm? A native clustering system for Docker.

    It turns a pool of Docker hosts into a single, virtual host using an API proxy system. It is Docker's first container orchestration project that began in 2014. Combined with Docker Compose, it's a very convenient tool to manage containers.
  76. Create a Swarm

  77. Initialize Swarm docker swarm init --advertise-addr <manager_ip> # Eg.: docker

    swarm init --advertise-addr
  78. Add Worker to Swarm docker swarm join --token <token> <manager_ip>:2377

    # Eg.: docker swarm join \ --token SWMTKN-1-49nj1cmql0...acrr2e7c \
  79. Manage Swarm

  80. Manage Swarm Nodes docker node ls - list nodes docker

    node rm <node> - remove node from swarm docker node inspect <node> docker node ps [<node>]- list swarm task docker node update ARGS <node>
  81. Deploy to Swarm

  82. Build & Push # Build docker-compose build # Push docker-compose

  83. Deploy App to Swarm # run docker stack deploy \

    --compose-file docker-compose.yml \ teched18
  84. Test It!

  85. Manage Services docker stack ls docker stack services <stack> docker

    stack ps <stack> docker stack rm <stack>
  86. Scale the Service docker service scale <service>=<n> # Eg.: docker

    service scale teched18_app=5
  87. Test It!

  88. Thank you Ondrej Sika email: [email protected] twitter: @ondrejsika linkedin: /in/ondrejsika/

    slides: https://ondrej-sika.cz/blog/2018/teched