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 Slide

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

    View Slide

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

    View Slide

  4. Why Docker?

    View Slide

  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)

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

  11. What is Docker?

    View Slide

  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.

    View Slide

  13. Virtualization vs Containers

    View Slide

  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.

    View Slide

  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.

    View Slide

  16. Advantages - Performance
    - Management
    - Containers distribution

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. Questions?

    View Slide

  21. Use Docker!

    View Slide

  22. Installation

    View Slide

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

    View Slide

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

    View Slide

  25. Basic Usage

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

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

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  36. Own Images

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  43. Example

    View Slide

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

    View Slide

  45. requirements.txt
    flask
    redis

    View Slide

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

    View Slide

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

    View Slide

  48. Docker Registry

    View Slide

  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.

    View Slide

  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.

    View Slide

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

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

    View Slide

  53. Docker Compose

    View Slide

  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.

    View Slide

  55. Install Docker Compose

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  67. Compose Commands

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  71. Example

    View Slide

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

    View Slide

  73. Build & Run
    docker-compose up

    View Slide

  74. Docker Swarm

    View Slide

  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.

    View Slide

  76. Create a Swarm

    View Slide

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

    View Slide

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

    View Slide

  79. Manage Swarm

    View Slide

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

  81. Docker Swarm New Way

    View Slide

  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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  86. Want more...

    View Slide

  87. skoleni-docker.cz

    View Slide

  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

    View Slide

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