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
Tweet

More Decks by Ondrej Sika

Other Decks in Programming

Transcript

  1. Agenda - What is a Docker? - Usage - Containers

    - Images - Docker Registry - Composites - Swarm
  2. 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.
  3. 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.
  4. 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.
  5. History of containers - 1979 - Chroot - 2000 -

    BSD Jails - 2001 - VServer - 2004 - Solaris Containers - 2005 - OpenVZ - 2006 - Process Containers - 2008 - LXC - 2013 - Docker
  6. Usage - Almost everywhere - Development, Testing, Production - Better

    deployment process - Separates running applications
  7. Test the installation docker run hello-world ... Hello from Docker!

    This message shows that your installation appears to be working correctly.
  8. 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..
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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)
  15. 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
  16. Docker Logs docker logs [-f] <container> # Eg.: docker logs

    my-debian docker logs -f my-debian # following
  17. Docker Volumes Volumes are persistent data storage for containers. Volumes

    can be shared between containers and data are written 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 -t <image> <path> docker

    build -f <dockerfile> -t <image> <path> docker tag <source image> <target image> # eg.: docker build -t my-image .
  23. 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='0.0.0.0', port='80')
  24. Run docker build -t teched18 . docker run --name redis

    -d redis:alpine docker run -p 80:80 --link redis teched18
  25. 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.
  26. 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.
  27. 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
  28. 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
  29. 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.
  30. Compose File A docker-compose.yml file is a YAML file that

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

    reg.istry.cz/counter ports: - 80:80 redis: image: redis:alpine
  32. 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>]
  33. 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
  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. Deploy App to Swarm # run docker stack deploy \

    --compose-file docker-compose.yml \ teched18