20
Docker for Beginners Kat Passen

Kat Passen. Open source platform to manage deployment Acts as a layer between an OS and an application Images not dependent on system – similar

Embed Size (px)

Citation preview

Page 1: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

Docker for BeginnersKat Passen

Page 2: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 3: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 4: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

How to Docker

Installation: Available on a variety of platforms:▪ Your favorite *nix▪ Windows▪ Rackspace, EC2, G-Cloud, etc

Page 5: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

How to Docker

Requirements: iptables git procps or similar XZ Utils A cfgroups hierarchy

Kernel requirements vary by distribution, general recommendation is v3.8+

Page 6: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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.

Page 7: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 8: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 9: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

Using Docker

Tutorial: https://www.docker.com/tryit/

Operates similar to any other *nix command line application

Page 10: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 11: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 12: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 13: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 14: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 15: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 16: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 17: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 18: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

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

Page 19: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

Further reading

http://docs.docker.com/userguide/

Page 20: Kat Passen.  Open source platform to manage deployment  Acts as a layer between an OS and an application  Images not dependent on system – similar

Alternatives

In no particular order: Dist::Zilla Rocket Bamboo Jenkins Chef Puppet Rex