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

Docker, Zlin, 8. 2. 2018

Ondrej Sika
February 15, 2018

Docker, Zlin, 8. 2. 2018

Ondrej Sika

February 15, 2018
Tweet

More Decks by Ondrej Sika

Other Decks in Programming

Transcript

  1. Agenda - Why Docker - What is Docker - Why

    Docker - Where can use Docker - Features - Practise - Containers - Images - Composites - Swarm
  2. 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)
  3. Docker in Development - Quick setup - Requiremensts (DB, cache,

    …) - Defined environment - Platform independent
  4. 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
  5. Docker in Production - Easy setup of host - only

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

    - No client environment requirements (just Docker)
  7. 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.
  8. 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.
  9. 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.
  10. 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..
  11. Toolchain - Docker - Build & Run single container -

    Docker Registry - Store images - Docker Compose - Build & Run multi-container applications - Docker Swarm - Native cluster
  12. Test the installation docker run hello-world ... Hello from Docker!

    This message shows that your installation appears to be working correctly.
  13. Docker Run docker run <image> <command> # Eg.: docker run

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

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

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

    -v my-volume:/data debian docker run -ti -v $(pwd)/my-data:/data debian
  19. Dockerfile Dockerfile is preferred way to create images. Dockerfile defines

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

    build <path> -f <dockerfile> -t <image> docker tag <source image> <target image>
  23. 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')
  24. 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"]
  25. Build & Run docker run redis:alpine docker build -t counter

    . docker run -p 8000:80 --link redis counter
  26. 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.
  27. 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.
  28. 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
  29. Run own registry docker run -d -p 5000:5000 \ --restart=always

    --name registry \ registry:2 More at: https://docs.docker.com/registry/deploying/
  30. 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.
  31. Compose File A docker-compose.yml file is a YAML file that

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

    - "5000:5000" redis: image: "redis:alpine"
  33. Manage Compose docker-compose logs [-f] [<service>] docker-compose start [<service>] docker-compose

    stop [<service>] docker-compose restart [<service>] docker-compose kill [<service>]
  34. 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.
  35. 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
  36. 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>
  37. Enable experimental mode # Run on all docker hosts sudo

    sh -c "echo '{\"experimental\": true}' > /etc/docker/daemon.json" sudo /etc/init.d/docker restart
  38. 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
  39. 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
  40. Thank you & Questions Ondrej Sika email: [email protected] twitter: @ondrejsika

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