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

Docker, Zlin, 8. 2. 2018

Docker, Zlin, 8. 2. 2018

Ondrej Sika

February 15, 2018
Tweet

More Decks by Ondrej Sika

Other Decks in Programming

Transcript

  1. Docker Ondrej Sika ondrej@ondrejsika.com, @ondrejsika CZ JUG Zlin, Zlin, 8.

    2. 2018
  2. https://sika.link/docker-18-02-08

  3. Agenda - Why Docker - What is Docker - Why

    Docker - Where can use Docker - Features - Practise - Containers - Images - Composites - Swarm
  4. Why Docker?

  5. Why Docker? - Simplify development, testing, production and make it

    faster - Save time & money - Save resources - Run on every platform - Easy to use - Large (& growing) community - Open Source (Docker CE)
  6. Where I can use Docker? - Development - Testing -

    Production - Distribution
  7. Docker in Development - Quick setup - Requiremensts (DB, cache,

    …) - Defined environment - Platform independent
  8. Docker in Testing - Deterministic tests & builds - CI

    ready (Gitlab CI, Travis CI) - Fast test setup & cleanup - Test exact same code and environment which 'll run in production
  9. Docker in Production - Easy setup of host - only

    Docker needed - Simple deployment - Simple scaling - Native cluster (Swarm) - Works with - Kubernetes - Openstack - Amazon Container Registry
  10. SW Distribution with Docker - Provide single image or composite

    - No client environment requirements (just Docker)
  11. What is Docker?

  12. 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.
  13. Virtualization vs Containers

  14. 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.
  15. 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.
  16. Advantages - Performance - Management - Containers distribution

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

  18. 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..
  19. Toolchain - Docker - Build & Run single container -

    Docker Registry - Store images - Docker Compose - Build & Run multi-container applications - Docker Swarm - Native cluster
  20. Questions?

  21. Use Docker!

  22. Installation

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

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

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

  26. Help docker docker help docker help <command> # eg. docker

    help run, docker help help
  27. Docker Images docker image ls # list all images docker

    image rm <tag> # remove image
  28. Docker Run docker run <image> <command> # Eg.: docker run

    hello-world docker run debian date docker run -ti debian
  29. 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
  30. Containers docker ps # list containers docker start <container> docker

    stop <container> docker restart <container> docker logs <container> # show output docker rm <container> # remove container
  31. 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
  32. Docker Volumes Volumes are persistent data storage for containers. Volumes

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

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

    host
  35. Port forwarding docker run -ti -p 8080:80 nginx

  36. Own Images

  37. .dockerignore Ignore files for docker build process. Similar to .gitignore

  38. .dockerignore .git .idea build/* # comment

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

    each layer of image by some command. To make image use command docker build
  40. 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
  41. 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
  42. Build Image from Dockerfile docker build <path> -t <image> docker

    build <path> -f <dockerfile> -t <image> docker tag <source image> <target image>
  43. Example

  44. app.py from flask import Flask from redis import Redis app

    = Flask(__name__) redis = Redis('redis') hostname = os.environ['HOSTNAME'] @app.route("/") def index(): counter = redis.incr('counter') return "%s %d\n" % (hostname, counter) if __name__ == "__main__": app.run(host='0.0.0.0', port='80')
  45. requirements.txt flask redis

  46. Dockerfile FROM python:2.7-alpine WORKDIR /app COPY requirements.txt /app/ RUN pip

    install -r requirements.txt COPY . /app EXPOSE 80 CMD ["python", "app.py"]
  47. Build & Run docker run redis:alpine docker build -t counter

    . docker run -p 8000:80 --link redis counter
  48. Docker Registry

  49. 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.
  50. 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.
  51. 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
  52. Run own registry docker run -d -p 5000:5000 \ --restart=always

    --name registry \ registry:2 More at: https://docs.docker.com/registry/deploying/
  53. Docker Compose

  54. 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.
  55. Install Docker Compose

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

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

    defines how Docker containers should behave in production.
  58. Example Compose File version: '3' services: web: build: . ports:

    - "5000:5000" redis: image: "redis:alpine"
  59. https://docs.docker.com/compose/compose-file/

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

    Compose.
  61. Build services: app: build: . services: app: build: context: ./app

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

  63. Expose & Port forwarding services: app: expose: - 80 -

    3333 ports: - 8000:80
  64. Volumes services: app: volumes: - /data1 - data:/data2 - ./data:/data3

  65. Command, Entrypoint services: app: entrypoint: 'python app.py' command: '80' services:

    app: entrypoint: ["python", "app.py"] command: ["80"]
  66. Deploy services: app: deploy: placement: constraints: [node.role == manager] services:

    app: deploy: mode: replicated replicas: 4
  67. Compose Commands

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

    will be downloaded or builded
  69. Manage Compose docker-compose logs [-f] [<service>] docker-compose start [<service>] docker-compose

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

  71. Example

  72. docker-compose.yml version: '3' services: counter: build: . image: counter ports:

    - 8000:80 redis: image: redis:alpine
  73. Build & Run docker-compose up

  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 192.168.99.100
  78. Add Worker to Swarm docker swarm join --token <token> <manager_ip>:2377

    # Eg.: docker swarm join \ --token SWMTKN-1-49nj1cmql0...acrr2e7c \ 192.168.99.100:2377
  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. Docker Swarm New Way

  82. Enable experimental mode # Run on all docker hosts sudo

    sh -c "echo '{\"experimental\": true}' > /etc/docker/daemon.json" sudo /etc/init.d/docker restart
  83. Update docker-compose.yml version: '3' services: counter: build: . image: reg.istry.cz/counter

    ports: - 8000:80 redis: image: redis:alpine
  84. Deploy App to Swarm # build docker-compose build # push

    to registry (reg.istry.cz) docker-compose push # deploy docker deploy --compose-file docker-compose.yml app
  85. Test App # on host run curl `docker-machine ip manager`

    curl `docker-machine ip worker1`
  86. Want more...

  87. skoleni-docker.cz

  88. skoleni-docker.cz I do: - inhouse training - open courses for

    individuals - i can do the course in Olomouc, if there 'll be demand Pristi termin workshopu: - 29. - 30. 1. 2018
  89. Thank you & Questions Ondrej Sika email: ondrej@ondrejsika.com twitter: @ondrejsika

    linkedin: /in/ondrejsika/ https://sika.link/docker-18-02-08 (slides & code) https://skoleni-docker.cz