Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Running Docker in Development & Production (DevSum 2015)

8,608 views

Published on

Running Docker in Development & Production presented at DevSum 2015, Stockholm, 25th May 2015

Published in: Software
  • Be the first to comment

Running Docker in Development & Production (DevSum 2015)

  1. 1. Who? @Ben_Hall Tech Support > Tester > Developer > Founder > Freelancer
  2. 2. Agenda • Introduction To Docker & Containers • Dockerizing Dockerising Applications • Docker As Development Environment • Testing Containers • Production
  3. 3. A Load Balanced ASP.NET/NancyFX/Node.js Website running inside Docker https://www.dropbox.com/s/gbcifo094c9v8ar/nancy-lb-demo-optimised.gif?dl=0
  4. 4. WHAT ARE CONTAINERS?
  5. 5. Virtual Machine https://www.docker.com/whatisdocker/
  6. 6. Container https://www.docker.com/whatisdocker/ Container
  7. 7. Container Own Process Space Own Network Interface Own Root Directories Sandboxed Like a lightweight VM. But it’s not a VM.
  8. 8. Container Native CPU Native Memory Native IO No Pre-Allocation Instant On Zero Performance Overheard
  9. 9. Build, Ship and Run Any App, Anywhere Docker - An open platform for distributed applications for developers and sysadmins.
  10. 10. RUNNING CONTAINERS
  11. 11. ElasticSearch Before Docker > curl -L -O http://download.elasticsearch.org/PATH/TO/VERSION.zip > unzip elasticsearch-$VERSION.zip > cd elasticsearch-$VERSION The only requirement for installing Elasticsearch is a recent version of Java. Preferably, you should install the latest version of the official Java from www.java.com. 1. Download the jre-8u40-macosx-x64.dmg file. 2. Review and agree to the terms of the license agreement before downloading the file. 3. Double-click the .dmg file to launch it 4. Double-click on the package icon to launch install Wizard 5. The Install Wizard displays the Welcome to Java installation screen. Click Next 6. Oracle has partnered with companies that offer various products. After ensuring the desired programs are selected, click the Next button to continue the installation. 7. After the installation has completed, a confirmation screen appears. Click Close to finish the installation process. > ./bin/elasticsearch
  12. 12. > docker run -d #Run In Background dockerfile/elasticsearch #Image Name https://www.dropbox.com/s/fbe8briq6ayycrh/start-elastic.gif?dl=0
  13. 13. > docker run -d -p 9200:9200 -p 9300:9300 #Bind Ports dockerfile/elasticsearch
  14. 14. curl b2d:9200 ? > boot2docker ip 192.168.59.103 > echo “192.168.59.103 b2d” >> /private/etc/hosts > cat /private/etc/hosts 192.168.59.103 b2d for i in {49000..49900}; do VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i"; VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i"; done
  15. 15. Installing In Development https://github.com/boot2docker/
  16. 16. https://github.com/docker/machine
  17. 17. DOCKERISING YOUR APPLICATIONS
  18. 18. Dockerfile Dockerfile & App Source Build Image https://docs.docker.com/reference/builder/
  19. 19. FROM FROM ubuntu:14.01 # Base Image FROM ubuntu:latest # Caution
  20. 20. COPY / WORKDIR / RUN COPY . /src # Copy current directory into image WORKDIR /src # Set working directory RUN apt-get update # Run a shell command
  21. 21. EXPOSE EXPOSE 3000 # Allow binding to port 3000 EXPOSE 7000-8000 # Expose range of ports
  22. 22. CMD CMD ./bin/www # Default command when container starts
  23. 23. Complete .NET/Mono Dockerfile FROM benhall/docker-mono COPY . /src WORKDIR /src RUN xbuild Nancy.Demo.Hosting.Docker.sln EXPOSE 8080 CMD ["mono", "src/bin/Nancy.Demo.Hosting.Docker.exe"]
  24. 24. benhall/docker-mono Dockerfile FROM ubuntu:14.01 MAINTAINER Ben Hall "ben@benhall.me.uk" RUN apt-get update -qq && apt-get -yqq install mono-complete && apt-get -yqq clean
  25. 25. > docker build #Build Image -t scrapbook/app:20150520 #Image Name:Tag . #Directory https://www.dropbox.com/s/k7h0tdu28160nil/scrapbook-node-build-optimised.gif?dl=0
  26. 26. > cat .dockerignore #Ignore file in root all_the_passwords.txt .git/ node_modules/ bower_components/
  27. 27. > docker run -it #Run In Foreground scrapbook/app:20150520 #Image Name:Tag
  28. 28. > docker run -it -p 3000 #Bind Random Port to Port 3000 scrapbook/app:20150520
  29. 29. > docker run -it -p 3000:3000 #Bind Known Port scrapbook/app:20150520
  30. 30. > docker ps #List Running Processes -a #Include Stopped CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e5b37b0a2bc scrapbook/app:20150520133000 "npm start" 4 minutes ago Up 4 minutes 0.0.0.0:49176->3000/tcp mad_fermi
  31. 31. > docker logs # Stream logs 1e5b37b0a2bc # ContainerID
  32. 32. > docker run -d -p 9200:9200 -p 9300:9300 --name es # Friendly Name dockerfile/elasticsearch > docker run –it –p 3000 --link es:elasticsearch # Link container:alias scrapbook/app:20150520 > cat /etc/hosts 172.17.0.79 elasticsearch > env HOSTNAME=2f3b959b13a0 ELASTICSEARCH_PORT=tcp://172.17.0.79:9200 ELASTICSEARCH_PORT_9200_TCP=tcp://172.17.0.79:9200 ELASTICSEARCH_PORT_9200_TCP_ADDR=172.17.0.79 ELASTICSEARCH_PORT_9200_TCP_PORT=9200 ELASTICSEARCH_PORT_9200_TCP_PROTO=tcp ELASTICSEARCH_NAME=/scrapbookv2_web_1/es NODE_ENV=production
  33. 33. > docker run –it –p 3000 --link es:elasticsearch -e SQLSERVER=10.10.20.50 #Environment Variable scrapbook/app:20150520 > env SQLSERVER.10.10.20.50 HOSTNAME=2f3b959b13a0 ELASTICSEARCH_PORT=tcp://172.17.0.79:9200 ELASTICSEARCH_PORT_9200_TCP=tcp://172.17.0.79:9200 ELASTICSEARCH_PORT_9200_TCP_ADDR=172.17.0.79 ELASTICSEARCH_PORT_9200_TCP_PORT=9200 ELASTICSEARCH_PORT_9200_TCP_PROTO=tcp ELASTICSEARCH_NAME=/scrapbookv2_web_1/es NODE_ENV=production
  34. 34. http://12factor.net/
  35. 35. http://blog.benhall.me.uk/2015/05/using-make-to-manage-docker-image-creation/ > make build NAME = benhall/docker-make-demo default: build build: docker build -t $(NAME) . debug: docker run --rm -it $(NAME) /bin/bash run: docker run --rm $(NAME) push: docker push $(NAME) release: build push
  36. 36. > cat docker-compose.yml web: # Container Name build: . # Build links: # Links - elasticsearch ports: # Ports - 3000 environment: # Environment VIRTUAL_HOST: 'app.joinscrapbook.com' NODE_ENV: 'production' elasticsearch: # 2nd Container Name # Use Image image: dockerfile/elasticsearch:latest ports: # Ports - 9200:9200
  37. 37. > docker-compose up # Start containers –d # In background Recreating scrapbookv2_nginx_1... Recreating scrapbookv2_redis_1... Recreating scrapbookv2_db_1... Recreating scrapbookv2_elasticsearch_1... Recreating scrapbookv2_web_1… > docker-compose stop # Stop containers Stopping scrapbookv2_web_1... Stopping scrapbookv2_elasticsearch_1... Stopping scrapbookv2_db_1... Stopping scrapbookv2_redis_1... Stopping scrapbookv2_nginx_1...
  38. 38. Sidekick Container For Testing > docker run –d # No need to bind ports --name es # Friendly Name dockerfile/elasticsearch > docker run –it --link es:es # Link Container:alias benhall/curl # Curl Image curl http://es:9200 # Ping service > echo $? # Exit Code 0 # Success
  39. 39. Schema Management Containers > docker run –d # No need to bind ports --name es # Friendly Name dockerfile/elasticsearch > docker run –rm --link es:es # Link Container:alias myapp/schema:latest # Schema Image
  40. 40. Tagging ubuntu 15.04 2427658c75a1 12 weeks ago 117.5 MB ubuntu vivid 2427658c75a1 12 weeks ago 117.5 MB ubuntu vivid-20150218 2427658c75a1 12 weeks ago 117.5 MB ubuntu 14.10 78949b1e1cfd 12 weeks ago 194.4 MB ubuntu utopic-20150211 78949b1e1cfd 12 weeks ago 194.4 MB ubuntu utopic 78949b1e1cfd 12 weeks ago 194.4 MB ubuntu latest 2d24f826cb16 12 weeks ago 188.3 MB ubuntu trusty 2d24f826cb16 12 weeks ago 188.3 MB ubuntu 14.04 2d24f826cb16 12 weeks ago 188.3 MB ubuntu 14.04.2 2d24f826cb16 12 weeks ago 188.3 MB ubuntu trusty-20150218.1 2d24f826cb16 12 weeks ago 188.3 MB ubuntu 12.04 1f80e9ca2ac3 12 weeks ago 131.5 MB ubuntu precise 1f80e9ca2ac3 12 weeks ago 131.5 MB ubuntu precise-20150212 1f80e9ca2ac3 12 weeks ago 131.5 MB ubuntu 12.04.5 1f80e9ca2ac3 12 weeks ago 131.5 MB ubuntu 14.04.1 5ba9dab47459 3 months ago 188.3 MB
  41. 41. > docker push # Push Image To Remote Registry benhall/nancy-demo:latest # Image Name:Tag
  42. 42. > docker export # Export Image to Tar containerid # Container ID > container.tar # Name of Tar
  43. 43. > docker run -p 5000:5000 registry:2.0 # Docker Registry Container > docker push b2d:5000/aspnet:beta5
  44. 44. Recap • Docker Client / Boot2docker • Docker Daemon • Docker Images • Docker Container • Docker Hub / Registry
  45. 45. DOCKER AS A DEVELOPMENT ENVIRONMENT
  46. 46. ASP.NET vNext DNX > cat Makefile restore: docker run -it -v /Users/ben/.dnx:/home/dev/.dnx -v $(shell pwd)/WebApplication:/app -w="/app" benhall/aspnet-vnext-npm dnu restore build: docker run -v /Users/ben/.dnx:/home/dev/.dnx -v $(shell pwd)/WebApplication:/app -w="/app" benhall/aspnet-vnext-npm dnu build run: docker run -it -v /Users/ben/.dnx:/home/dev/.dnx -v $(shell pwd)/WebApplication:/app -w="/app" -p 5001 benhall/aspnet-vnext-npm dnx . kestrel
  47. 47. > docker run –it --name scrapbook-iojs # Name it for future -v $(pwd):/app -v $(pwd)/iojs:/app/node_modules # Move location -w="/app” # Set working directory --entrypoint /bin/bash # Override entrypoint iojs
  48. 48. GoLang > cat Dockerfile FROM golang:onbuild > cat Makefile NAME = ocelotuproar/docker-outdated build: docker build -t $(NAME) . run: docker run --rm --name $(INSTANCE) $(NAME) > make build # Run Golang Compiler & Build container > make run # Run built application
  49. 49. Run Unit Tests Inside Containers
  50. 50. Run UI Inside Containers > docker run -d -p 4444:4444 --name selenium-hub selenium/hub:2.45.0 > docker run -d --link selenium-hub:hub selenium/node- chrome:2.45.0 > docker run -d --link selenium-hub:hub selenium/node- firefox:2.45.0
  51. 51. Private NPM Repository https://github.com/BenHall/docker-local-npm-registry > docker run -d -v $(pwd)/config.yaml:/opt/sinopia/config.yaml -p 4873:4873 keyvanfatehi/sinopia:latest > npm set registry http://b2d:4873 > npm adduser --registry http://b2d:4873
  52. 52. RStudio • docker run -d -p 8787:8787 rocker/rstudio
  53. 53. PRODUCTION
  54. 54. > docker pull # Pull Image To Remote Registry benhall/nancy-demo:latest # Image Name:Tag Always include tag otherwise pulls everything
  55. 55. > cat docker-compose.yml web: build: . links: - elasticsearch volumes: # Mount Directories Outside Container - /opt/docker/scrapbook/db:/usr/src/app/ocelite-db - /opt/docker/scrapbook/uploads:/usr/src/app/uploads - /opt/docker/scrapbook/tmp:/usr/src/app/tmp elasticsearch: image: dockerfile/elasticsearch:latest volumes: # Host:Container - /opt/docker/scrapbook_elasticsearch:/data Persisting Data
  56. 56. Port 80
  57. 57. Problematic Approach > docker run -d --name nginx_root --link blog_benhall-1:blog_benhall-1 --link scrapbook-1:scrapbook-1 --link scrapbook_web_1:scrapbook_web_1 --link brownbag_web_1:brownbag_web_1 -p 80:80 -v /opt/docker/nginx/www:/data -v /opt/docker/nginx/sites:/etc/nginx/sites-enab -v /opt/docker/nginx/logs:/var/log/nginx dockerfile/nginx
  58. 58. Problems • Static link – /etc/hosts • Adding new website? Reboot everything • Requires nginx config for each site
  59. 59. Nginx Proxy https://github.com/jwilder/nginx-proxy https://www.dropbox.com/s/2f6y2frfjafc409/nginx-proxy-optimised.gif?dl=0
  60. 60. • VIRTUAL_HOST=my.container.com • -v /var/run/docker.sock:/tmp/docker.sock
  61. 61. 1) Docker raises events when containers start / stop 2) Registrator listens to events adds the new container’s details into Consul 3) Consul links container’s IP / Ports to DNS names & discovery API > ping redis.service.consul 4) Nginx uses Consul API to write & load config
  62. 62. A Load Balanced ASP.NET/NancyFX/Node.js Website running inside Docker https://www.dropbox.com/s/gbcifo094c9v8ar/nancy-lb-demo-optimised.gif?dl=0
  63. 63. Installing In Production 'curl -sSL https://get.docker.com/ | sh'
  64. 64. > docker run -d --restart=always # Restart if exits redis
  65. 65. Ports > docker run –p 3000:3000 nodejs node.app > sudo apt-get install ufw && sudo ufw disable > sudo ufw default deny > sudo ufw allow 22 > sudo ufw allow 80 > sudo ufw enable Docker manages IPTables, ufw won’t block > echo "DOCKER_OPTS=”--iptables=false"" > /etc/default/docker > docker run –p 127.0.0.1:3000 nodejs node.app
  66. 66. Memory / CPU Usage > docker run –m 128m –cpu 50 mysql Space Usage Linux cgroups… Bandwidth Usage > iptables -A OUTPUT -p tcp --sport 80 -m state -- state ESTABLISHED,RELATED -m quota –quota 1310720 -j ACCEPT
  67. 67. Log Files 5.8M Mar 29 07:17 /var/lib/docker/containers/0e3bcd1 157M Mar 29 14:25 /var/lib/docker/containers/1922c7a 1.8M Mar 6 07:23 /var/lib/docker/containers/2774be7 32K Jan 14 16:18 /var/lib/docker/containers/38e7c4ae 183K Mar 17 10:00 /var/lib/docker/containers/4a207c6 955M Mar 29 14:25 /var/lib/docker/containers/5408f6a 1.3M Mar 6 10:17 /var/lib/docker/containers/6e41977 1.3M Mar 6 10:11 /var/lib/docker/containers/756f64b 71 Jan 28 11:50 /var/lib/docker/containers/b1a2d887e 509M Mar 29 14:25 /var/lib/docker/containers/c5784ce 16K Feb 2 18:26 /var/lib/docker/containers/daa45ceb 488K Mar 6 10:43 /var/lib/docker/containers/ec80d6a
  68. 68. Handling Machine Name Changes • Couchbase • InfluxDB
  69. 69. THE FUTURE?
  70. 70. Docker and Microsoft Partnership
  71. 71. SQL Server as a Container?
  72. 72. Spoon.NET
  73. 73. Visual Studio as a Container?
  74. 74. Docker as a Platform
  75. 75. http://www.joinscrapbook.com
  76. 76. IN SUMMARY…
  77. 77. Only tool I use for deployment • Close gap between development and production • Everything is a container! • Running platforms like Logstash, ElasticSearch, Redis, EventStore, RavenDB, NancyFX etc? Consider containers for deployment.
  78. 78. @Ben_Hall Ben@BenHall.me.uk Blog.BenHall.me.uk Thank you http://www.joinscrapbook.com

×