Hva er Docker?

En "maskin"
Kjører virtuelt?
Lett å distribuere

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool.

Docker uses resource isolation features of the Linux kernel such as cgroups and kernel namespaces to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting virtual machines.

Kilde: http://en.wikipedia.org/wiki/Docker_(software)

Kilde: http://docker.com

Busswordz!
Images
Containers

Images

A Docker image is a read-only template. For example, an image could contain an Ubuntu operating system with Apache and your web application installed. Images are used to create Docker containers. Docker provides a simple way to build new images or update existing images, or you can download Docker images that other people have already created. Docker images are the build component of Docker.

FROM centos:centos6

MAINTAINER itas

RUN yum install -y git gpg curl which tar

RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN curl -sSL https://get.rvm.io | bash -s stable --ruby
FROM oslokommune/centos6-ruby

MAINTAINER itas

# Adding SSH-certificates
RUN mkdir -p /root/.ssh
ADD id_rsa_komponentoversikt /root/.ssh/id_rsa
ADD id_rsa_komponentoversikt.pub /root/.ssh/id_rsa.pub
RUN echo "github.oslo.kommune.no,10.134.183.140 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDP7S3lurqg2P1lyNsLHG+y877l7U+aCQOlr+vajGUzjolkKbv+dKlXpmWJigjYeyXgPnrqfdpOPgB+q46PoJ1ObEwwAJZ8e3nCs9H7qFERVLN+7618ATApMdKsa0nUN8VX1dxKp2SR74acrhzgVEG9PvlOvjuCVCrtxDNPuz+aWuiZ8skMPV+xFHDEy93EVXNJj8aRZHNMKqJvdIAHv7sDu2QxPSAfoMwmhgQh6FvkyuvJ58EAWt48T0xCb4urjGGIA5GyZzpkb8jjd8yd+OxcrDSoYcQJRnllpNJk+fvVCEkdunrt9Q2KTOi7i/4iS/yPnoYvOeXVbZ8g6BxkRMMD" > /root/.ssh/known_hosts
RUN chmod -R 0600 /root/.ssh/*

# Adding repo
RUN mkdir -p /root/app
ADD komponentoversikt /root/app

# Installing gems
RUN export PATH="$PATH:$HOME/.rvm/bin"
RUN /bin/bash -lc 'rvm use 2.2.0'
RUN /bin/bash -lc 'cd /root/app && gem install bundler'
RUN /bin/bash -lc 'cd /root/app && bundle install'

EXPOSE 4567
WORKDIR /root/app

# command to run on run
CMD /bin/bash -lc 'git pull'
CMD /bin/bash -lc 'ruby app.rb test'

Container

Docker containers are similar to a directory. A Docker container holds everything that is needed for an application to run. Each container is created from a Docker image. Docker containers can be run, started, stopped, moved, and deleted. Each container is an isolated and secure application platform. Docker containers are the run component of Docker.

Tid for kommandoer!

Alt må kjøres med sudo!

# docker build => bygg et image
$ sudo docker build --tag oslokommune/komsikt komponentoversikt/

# docker run => kjør et image i en kontainer
$ sudo docker run --name komsikt oslokommune/komsikt

# Uten å koble seg til
$ sudo docker run -d oslokommune/komsikt

# Interaktivt
$ sudo docker run -it oslokommune/komsikt

# Koble seg til en kjørende kontainer
$ sudo docker attach komsikt

# Uten at ctrl+c (og andre ting) slår av kontaineren
$ sudo docker attach --sig-proxy=false komsikt

# starter en kontainer
$ sudo docker start komsikt

# stopper en kontainer
$ sudo docker stop komsikt
# se images
$ sudo docker images

# se kjørende kontainere
$ sudo docker ps

# se alle kontainere
$ sudo docker ps -a

# slett en ikke kjørende kontainer
$ sudo docker rm <id>

# sletter alle ikke kjørende kontainere
$ sudo docker rm $(sudo docker ps -aq)

# sletter et image
$ sudo docker rmi <image>

# viser informasjon om en kontainer
$ sudo docker inspect <id>
# få hjelp
$ sudo docker help

# få hjelp om en kommando
$ sudo docker help run

Utskrift eksempler

[11:23:46][komponentoversikt]docker$ sudo docker images
[sudo] password for kyreri: 
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
oslokommune/centos6-ruby   latest              923d5fad7735        3 minutes ago       506.8 MB
centos                     centos6             f6808a3e4d9e        2 weeks ago         202.6 MB
[11:27:15][komponentoversikt]docker$ sudo docker build --tag oslokommune/komsikt komponentoversikt/
Sending build context to Docker daemon 8.956 MB
Sending build context to Docker daemon 
Step 0 : FROM oslokommune/centos6-ruby
 ---> 923d5fad7735
Step 1 : MAINTAINER itas
 ---> Running in 4e1619bffa83
 ---> 7d9f6dae301a
Removing intermediate container 4e1619bffa83
Step 2 : RUN mkdir -p /root/.ssh
 ---> Running in 693eef75bff9
 ---> 8909a284587f
Removing intermediate container 693eef75bff9
Step 3 : ADD id_rsa_komponentoversikt /root/.ssh/id_rsa
 ---> 05a6730d9e45
Removing intermediate container 765a0afcbac6
Step 4 : ADD id_rsa_komponentoversikt.pub /root/.ssh/id_rsa.pub
 ---> 8be10b6cb7d2
Removing intermediate container a72bc5754c78
Step 5 : RUN echo "github.oslo.kommune.no,10.134.183.140 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDP7S3lurqg2P1lyNsLHG+y877l7U+aCQOlr+vajGUzjolkKbv+dKlXpmWJigjYeyXgPnrqfdpOPgB+q46PoJ1ObEwwAJZ8e3nCs9H7qFERVLN+7618ATApMdKsa0nUN8VX1dxKp2SR74acrhzgVEG9PvlOvjuCVCrtxDNPuz+aWuiZ8skMPV+xFHDEy93EVXNJj8aRZHNMKqJvdIAHv7sDu2QxPSAfoMwmhgQh6FvkyuvJ58EAWt48T0xCb4urjGGIA5GyZzpkb8jjd8yd+OxcrDSoYcQJRnllpNJk+fvVCEkdunrt9Q2KTOi7i/4iS/yPnoYvOeXVbZ8g6BxkRMMD" > /root/.ssh/known_hosts
 ---> Running in ecb81eedc368
 ---> 3c58f29d47fa
Removing intermediate container ecb81eedc368
Step 6 : RUN chmod -R 0600 /root/.ssh/*
 ---> Running in c992899a41da
 ---> a2f8db55e56a
Removing intermediate container c992899a41da
Step 7 : RUN mkdir -p /root/app
 ---> Running in 36e2e66a9417
 ---> fd94ed8f02fb
Removing intermediate container 36e2e66a9417
Step 8 : ADD komponentoversikt /root/app
 ---> aa44b41c845b
Removing intermediate container 467907210ce2
Step 9 : RUN export PATH="$PATH:$HOME/.rvm/bin"
 ---> Running in 5512cfdac2ab
 ---> d41e6cf62877
Removing intermediate container 5512cfdac2ab
Step 10 : RUN /bin/bash -lc 'rvm use 2.2.0'
 ---> Running in 1e2977398120
Using /usr/local/rvm/gems/ruby-2.2.0
 ---> 8adfe6297cea
Removing intermediate container 1e2977398120
Step 11 : RUN /bin/bash -lc 'cd /root/app && gem install bundler'
 ---> Running in 531fd31337c9
Successfully installed bundler-1.8.5
Parsing documentation for bundler-1.8.5
Installing ri documentation for bundler-1.8.5
Done installing documentation for bundler after 2 seconds
1 gem installed
 ---> 1fa07b92e98d
Removing intermediate container 531fd31337c9
Step 12 : RUN /bin/bash -lc 'cd /root/app && bundle install'
 ---> Running in 61227e27448b
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Fetching gem metadata from http://rubygems.org/..........
Fetching version metadata from http://rubygems.org/..
Installing git 1.2.9.1
Installing tilt 1.4.1
Installing haml 4.0.6
Installing json 1.8.2
Installing net-ssh 2.9.2
Installing rack 1.6.0
Installing rack-protection 1.5.3
Installing sinatra 1.4.5
Using bundler 1.8.5
Bundle complete! 5 Gemfile dependencies, 9 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
Post-install message from haml:

HEADS UP! Haml 4.0 has many improvements, but also has changes that may break
your application:

* Support for Ruby 1.8.6 dropped
* Support for Rails 2 dropped
* Sass filter now always outputs <style> tags
* Data attributes are now hyphenated, not underscored
* html2haml utility moved to the html2haml gem
* Textile and Maruku filters moved to the haml-contrib gem

For more info see:

http://rubydoc.info/github/haml/haml/file/CHANGELOG.md

 ---> aa01b040357d
Removing intermediate container 61227e27448b
Step 13 : EXPOSE 4567
 ---> Running in a88d8fb2f58d
 ---> f2eaa8c81c38
Removing intermediate container a88d8fb2f58d
Step 14 : WORKDIR /root/app
 ---> Running in af7addbda821
 ---> c140c8c59b81
Removing intermediate container af7addbda821
Step 15 : CMD /bin/bash -lc 'git pull'
 ---> Running in 44e24b7a6204
 ---> c9cc75a3a08b
Removing intermediate container 44e24b7a6204
Step 16 : CMD /bin/bash -lc 'ruby app.rb test'
 ---> Running in 25722d010f76
 ---> 3f63834651c3
Removing intermediate container 25722d010f76
Successfully built 3f63834651c3
[11:08:29][komponentoversikt]docker$ sudo docker ps -a
CONTAINER ID        IMAGE                             COMMAND             CREATED             STATUS                   PORTS               NAMES
addfded5858c        oslokommune/centos6-ruby:latest   "bash"              2 hours ago         Exited (0) 2 hours ago                       happy_darwin        
b951eb882e57        oslokommune/centos6-ruby:latest   "bash"              2 hours ago         Exited (2) 2 hours ago                       angry_nobel
[11:08:50][komponentoversikt]docker$ sudo docker rmi oslokommune/centos6-ruby
Untagged: oslokommune/centos6-ruby:latest
Deleted: 2793b40f7e0284bcf197296d67b06eac6612f192530f5cf3ee00f26e5be61e24
Deleted: d2f782f68ce18c8cd69c17f00afc1dbed6c2bbf969c6a641be9e9c5b16beb352
Deleted: 6c6ed0e9b5a8213ad6259b4824823f3dfb875b59aeb82db9067f85e2ccc537b5
Deleted: 8a5e85e932114b1ba0a538e086bc7cae47f51ae5c3c814b197e7e91dc5c68d9c
Deleted: f6808a3e4d9e80a655ec625e38b869ed8a614611e4d0073aeff23be841c9fcff
Deleted: 5b12ef8fd57065237a6833039acc0e7f68e363c15d8abb5cacce7143a1f7de8a
Deleted: 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
itas@mgmt1test:~$ sudo docker run -p 4567:4567 komponentoversikt
[2015-03-19 10:03:36] INFO  WEBrick 1.3.1
[2015-03-19 10:03:36] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2015-03-19 10:03:36] INFO  WEBrick::HTTPServer#start: pid=1 port=4567
itas@mgmt1test:~$ sudo docker run -d -p 4567:4567 komponentoversikt
d5f5835b02f40a3cab112bb2b57a403bb4212ed0b6179a1aa7e84b38ba007f93

Docker in practise

By Kyrre Havik

Docker in practise

Kyrre har nå brukt Docker i hele tre dager! Bli med og se hva han har lært.

  • 216