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
    [email protected], @ondrejsika
    CZ JUG Zlin,
    Zlin, 8. 2. 2018

    View full-size slide

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

    View full-size slide

  3. Agenda
    - Why Docker
    - What is Docker
    - Why Docker
    - Where can use Docker
    - Features
    - Practise
    - Containers
    - Images
    - Composites
    - Swarm

    View full-size slide

  4. 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)

    View full-size slide

  5. Where I can use
    Docker?
    - Development
    - Testing
    - Production
    - Distribution

    View full-size slide

  6. Docker in
    Development
    - Quick setup
    - Requiremensts (DB, cache, …)
    - Defined environment
    - Platform independent

    View full-size slide

  7. 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

    View full-size slide

  8. Docker in
    Production
    - Easy setup of host
    - only Docker needed
    - Simple deployment
    - Simple scaling
    - Native cluster (Swarm)
    - Works with
    - Kubernetes
    - Openstack
    - Amazon Container Registry

    View full-size slide

  9. SW Distribution
    with Docker
    - Provide single image or
    composite
    - No client environment
    requirements (just Docker)

    View full-size slide

  10. What is Docker?

    View full-size slide

  11. 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.

    View full-size slide

  12. Virtualization vs Containers

    View full-size slide

  13. 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.

    View full-size slide

  14. 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.

    View full-size slide

  15. Advantages - Performance
    - Management
    - Containers distribution

    View full-size slide

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

    View full-size slide

  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..

    View full-size slide

  18. Toolchain
    - Docker
    - Build & Run single container
    - Docker Registry
    - Store images
    - Docker Compose
    - Build & Run multi-container
    applications
    - Docker Swarm
    - Native cluster

    View full-size slide

  19. Installation

    View full-size slide

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

    View full-size slide

  21. Test the installation
    docker run hello-world
    ...
    Hello from Docker!
    This message shows that your installation
    appears to be working correctly.

    View full-size slide

  22. Help
    docker
    docker help
    docker help
    # eg. docker help run, docker help help

    View full-size slide

  23. Docker Images
    docker image ls # list all images
    docker image rm # remove image

    View full-size slide

  24. Docker Run
    docker run
    # Eg.:
    docker run hello-world
    docker run debian date
    docker run -ti debian

    View full-size slide

  25. Common Docker Run Params
    --name
    -d - run as daemon
    -ti - map TTY a STDIN (for bash eg.)
    -e = - set ENV variable
    -h - set hostname
    -u - run command by specific user

    View full-size slide

  26. Containers
    docker ps # list containers
    docker start
    docker stop
    docker restart
    docker logs # show output
    docker rm # remove container

    View full-size slide

  27. Docker Exec
    docker exec
    -d - run command as daemon
    -e = - set ENV variable
    -ti - map TTY a STDIN (for bash eg.)
    -u - run command by specific user
    # Eg.:
    docker exec my-debian ls

    View full-size slide

  28. Docker Volumes
    Volumes are persistent data storage
    for containers.
    Volumes can be shared between
    containers and data are write
    directly to host.

    View full-size slide

  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

    View full-size slide

  30. Port Forwarding Docker can forward specific port
    from container to host

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  34. Dockerfile
    Dockerfile is preferred way to
    create images.
    Dockerfile defines each layer of
    image by some command.
    To make image use command
    docker build

    View full-size slide

  35. Dockerfile
    FROM - define base image
    MAINTAINER - set maintainers name &
    email
    RUN - run command and save as layer
    COPY - copy file or
    directory to image layer
    ADD - instead of copy,
    archives added by add are extracted

    View full-size slide

  36. Dockerfile
    ENV - set ENV variable
    USER - switch user
    WORKDIR - change working directory
    VOLUME - define volume
    ENTRYPOINT - command, run on container
    starts
    CMD - parameters for entrypoint

    View full-size slide

  37. Build Image from Dockerfile
    docker build -t
    docker build -f -t
    docker tag

    View full-size slide

  38. 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')

    View full-size slide

  39. requirements.txt
    flask
    redis

    View full-size slide

  40. 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"]

    View full-size slide

  41. Build & Run
    docker run redis:alpine
    docker build -t counter .
    docker run -p 8000:80 --link redis counter

    View full-size slide

  42. Docker Registry

    View full-size slide

  43. 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.

    View full-size slide

  44. 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.

    View full-size slide

  45. Docker registry
    docker login - Login to Docker Registry
    docker logout - Logout from Docker Registry
    docker pull - download image from registry
    docker push - upload image to registry

    View full-size slide

  46. Run own registry
    docker run -d -p 5000:5000 \
    --restart=always --name registry \
    registry:2
    More at: https://docs.docker.com/registry/deploying/

    View full-size slide

  47. Docker Compose

    View full-size slide

  48. 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.

    View full-size slide

  49. Install Docker Compose

    View full-size slide

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

    View full-size slide

  51. Compose File
    A docker-compose.yml file is a
    YAML file that defines how Docker
    containers should behave in
    production.

    View full-size slide

  52. Example Compose File
    version: '3'
    services:
    web:
    build: .
    ports:
    - "5000:5000"
    redis:
    image: "redis:alpine"

    View full-size slide

  53. https://docs.docker.com/compose/compose-file/

    View full-size slide

  54. Service Service is a container running and
    managed by Docker Compose.

    View full-size slide

  55. Build
    services:
    app:
    build: .
    services:
    app:
    build:
    context: ./app
    dockerfile: Dockerfile-prod
    image: myapp

    View full-size slide

  56. Image
    services:
    app:
    image: redis:alpine

    View full-size slide

  57. Expose & Port forwarding
    services:
    app:
    expose:
    - 80
    - 3333
    ports:
    - 8000:80

    View full-size slide

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

    View full-size slide

  59. Command, Entrypoint
    services:
    app:
    entrypoint: 'python app.py'
    command: '80'
    services:
    app:
    entrypoint: ["python", "app.py"]
    command: ["80"]

    View full-size slide

  60. Deploy
    services:
    app:
    deploy:
    placement:
    constraints: [node.role == manager]
    services:
    app:
    deploy:
    mode: replicated
    replicas: 4

    View full-size slide

  61. Compose Commands

    View full-size slide

  62. Run Compose
    docker-compose up
    docker-compose up -d
    # Missing images will be downloaded or
    builded

    View full-size slide

  63. Manage Compose
    docker-compose logs [-f] []
    docker-compose start []
    docker-compose stop []
    docker-compose restart []
    docker-compose kill []

    View full-size slide

  64. Remove Compose
    docker-compose down
    # stop and remove compose

    View full-size slide

  65. docker-compose.yml
    version: '3'
    services:
    counter:
    build: .
    image: counter
    ports:
    - 8000:80
    redis:
    image: redis:alpine

    View full-size slide

  66. Build & Run
    docker-compose up

    View full-size slide

  67. Docker Swarm

    View full-size slide

  68. 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.

    View full-size slide

  69. Create a Swarm

    View full-size slide

  70. Initialize Swarm
    docker swarm init --advertise-addr
    # Eg.:
    docker swarm init --advertise-addr 192.168.99.100

    View full-size slide

  71. Add Worker to Swarm
    docker swarm join --token :2377
    # Eg.:
    docker swarm join \
    --token SWMTKN-1-49nj1cmql0...acrr2e7c \
    192.168.99.100:2377

    View full-size slide

  72. Manage Swarm

    View full-size slide

  73. Manage Swarm Nodes
    docker node ls - list nodes
    docker node rm - remove node from
    swarm
    docker node inspect
    docker node ps []- list swarm task
    docker node update ARGS

    View full-size slide

  74. Docker Swarm New Way

    View full-size slide

  75. Enable experimental mode
    # Run on all docker hosts
    sudo sh -c "echo '{\"experimental\": true}'
    > /etc/docker/daemon.json"
    sudo /etc/init.d/docker restart

    View full-size slide

  76. Update docker-compose.yml
    version: '3'
    services:
    counter:
    build: .
    image: reg.istry.cz/counter
    ports:
    - 8000:80
    redis:
    image: redis:alpine

    View full-size slide

  77. 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

    View full-size slide

  78. Test App
    # on host run
    curl `docker-machine ip manager`
    curl `docker-machine ip worker1`

    View full-size slide

  79. Want more...

    View full-size slide

  80. skoleni-docker.cz

    View full-size slide

  81. 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

    View full-size slide

  82. 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

    View full-size slide