Upload
marjorie-cole
View
216
Download
0
Embed Size (px)
Citation preview
Docker for BeginnersKat Passen
Docker?
Open source platform to manage deployment
Acts as a layer between an OS and an application
Images not dependent on system – similar to the JVM
Provides a “global standard” to code against
But virtual machines?
VMs require apps to be containered within the guest OS Potentially 1:1 on VM:application
Docker sits between the app and the host OS Takes place of the hypervisor Runs images as an isolated process Only the app and its libraries are
compartmentalized instead of requiring the guest OS share that space as well
How to Docker
Installation: Available on a variety of platforms:▪ Your favorite *nix▪ Windows▪ Rackspace, EC2, G-Cloud, etc
How to Docker
Requirements: iptables git procps or similar XZ Utils A cfgroups hierarchy
Kernel requirements vary by distribution, general recommendation is v3.8+
How to Docker
Important: Docker runs as rootGiving non-root accessThe docker daemon always runs as the root user, and
the docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root, and so, by default, you can access it with sudo.
If you (or your Docker installer) create a Unix group called docker and add users to it, then the docker daemon will make the ownership of the Unix socket read/writable by the docker group when the daemon starts. The docker daemon must always run as the root user, but if you run the docker client as a user in the docker group then you don't need to add sudo to all the client commands.
Docker Parts
Docker Engine Client-server application Handles running images, registries and
containers Docker Hub
Public Docker registry Holds a collection of existing images to
be used publicly Image examples: Ubuntu, Node.js,
MongoDB, WordPress
Docker Hub
Integrates with Github and Bitbucket Operates similar to them as well
Hosts images that can be used and contributed to by the Docker community Location that Docker pulls and pushes to
Using Docker
Tutorial: https://www.docker.com/tryit/
Operates similar to any other *nix command line application
Advanced Docker-ing
Interactive containers: $ sudo docker run -t -i ubuntu:14.04 /bin/bash▪ -t creates a pseudo-terminal▪ -i captures STDIN
Results (example): root@af8bae53bdd3:/# pwd/ root@af8bae53bdd3:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
Advanced Docker-ing
Hello world daemon $ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done“▪ -d runs containers in the background (daemonizes
them) This example returns a container ID, which
you can use to interact with the container running that command
To stop a container, use docker stop $container_name
Running a webapp in Docker Docker’s example runs a Python Flask
app: $ sudo docker run -d -P training/webapp python app.py▪ -P: map required network ports inside the
container to the host Check results with sudo docker ps –l:▪ CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse
▪ -l: tells docker ps to return the last container started
Running a webapp in Docker
Ports: super malleable -P is a shortcut for –p 5000 Docker generally picks a high external
port to map to Can run 1:1 on your port mappings, but
this way you can have multiple apps thinking they’re on the same port
Lookup: use docker port $container_name $port
More Docker commands
View logs: docker log $container_name Add -f and it behaves the same as tail –f
Other neat things: docker top – same as standard top docker inspect – returns a JSON hash
of config and status info docker tag – set a tag on an image
(e.g. ‘devel’)
More Docker commands
Start/stop/restart docker start $container_name docker stop $container_name docker restart $container_name
Removing things Containers:▪ docker rm $container_name▪ Container in question has to be stopped first
Images:▪ docker rmi $image_name
All deletions are final
Roll your own
Updating and committing to an existing image: Create a container from the image you
want to update Add what you want to the container, exit Commit changes:▪ $ sudo docker commit -m="Added json gem" -a="Kate Smith" 0b2616b0e5a8 ouruser/sinatra:v2
▪ Params after flags: source container ID and user/target_name:tag
Roll your own
Build an image from a Dockerfile Dockerfile: set of instructions telling Docker
how to create an image Takes the format of INSTRUCTION statement▪ # This is a commentFROM ubuntu:14.04MAINTAINER Kate Smith <[email protected]> RUN apt-get update && apt-get install -y ruby ruby-dev RUN gem install sinatra
Roll your own
Build an image from a Dockerfile (cont) In the same directory, run docker build –t=“$image_name” $path▪ $ sudo docker build -t="ouruser/sinatra:v2" .
Now you can create a container from the new image! Size caveat: images have a maximum of
127 layers
Alternatives
In no particular order: Dist::Zilla Rocket Bamboo Jenkins Chef Puppet Rex