Skip to content

Instantly share code, notes, and snippets.

@zh
Forked from wsargent/docker_cheat.md
Created November 14, 2013 14:07
Show Gist options
  • Save zh/7467373 to your computer and use it in GitHub Desktop.
Save zh/7467373 to your computer and use it in GitHub Desktop.

Docker Cheat Sheet

Why

Here's Why

Prequisites

Homebrew

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Installation:

Install VirtualBox and Vagrant using Brew Cask: https://github.com/phinze/homebrew-cask

brew tap phinze/homebrew-cask
brew install brew-cask
brew cask install virtualbox
brew cask install vagrant

We use the pre-built vagrant box: http://blog.phusion.nl/2013/11/08/docker-friendly-vagrant-boxes/

mkdir mydockerbox
cd mydockerbox
vagrant init docker https://oss-binaries.phusionpassenger.com/vagrant/boxes/ubuntu-12.04.3-amd64-vbox.box   
vagrant up
vagrant ssh

In the Vagrant:

sudo su -
sh -c "curl https://get.docker.io/gpg | apt-key add -"
sh -c "echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
apt-get update
apt-get install -y lxc-docker

Verify:

docker run -i -t ubuntu /bin/bash

Instant Dev Environment

This is not intended to follow on from the previous script. This starts from scratch.

Read through http://blog.relateiq.com/a-docker-dev-environment-in-24-hours-part-2-of-2/

git clone https://github.com/relateiq/docker_public
cd docker_public
vagrant up
vagrant ssh

Note that they base everything off a private registry at server:4444 -- we need to set that up. Everything needs to be done as root:

sudo su - 

Install a private registry (the fast way), only map port 4444 on the host to port 5000 in the container and run it as a daemon:

(from https://github.com/dotcloud/docker-registry)

docker run -d -p 4444:5000 samalba/docker-registry

Alias server to localhost:

echo "127.0.0.1      server" >> /etc/hosts

Check the private repository exists and is running on port 4444:

apt-get install -y curl
curl --get --verbose http://server:4444/v1/_ping

The docker_public repository assumes you have oracle-java7 installed, so you have to install that.

http://docs.docker.io/en/latest/use/workingwithrepository/

docker pull barnybug/oracle-java7
docker images | grep oracle-java7 | grep latest # --> image_id
docker tag <image_id> server:4444/oracle-java7
docker push server:4444/oracle-java7

Now we have the private registry set up, pull the docker_public git repository INSIDE THE VM, since it has the images we need.

cd $HOME
apt-get install -y git
git clone https://github.com/relateiq/docker_public
cd docker_public/images

Then go through all the images and install them:

vi push_build

#!/bin/bash
for i in $(eval echo "zookeeper redis cassandra elasticsearch mongo kafka"); do
  cd $HOME/docker_public/images/$i
  docker build -t=server:4444/$i .
  docker push server:4444/$i
done;

NOTE: Cassandra's dockerfile is broken and uses 1.2.9 which isn't on the mirror. See https://github.com/relateiq/docker_public/pull/2

chmod +x ./push_build
./push_build

Exit back into your host OS and do a vagrant reload to get the guest additions up:

vagrant reload

Then start the devenv container with

./bin/devenv update
./bin/devenv start

You should see:

➜  docker_public git:(master) ✗ ./bin/devenv start
Started ZOOKEEPER in container b7af4a32fd11
Started REDIS in container f49c9910da35
Started CASSANDRA in container 52f3233972fb
Started ELASTICSEARCH in container 802a9f1139d9
Started MONGO in container 33e5df9178a4
Started KAFKA in container d3475cfe8161

You're all done!

Containers

  • docker run creates a container.
  • docker stop stops it.
  • docker start will start it again.
  • docker attach will connect to a running container.
  • docker ps shows running containers.
  • docker ps -a shows running and stopped containers.
  • docker rm deletes a container.
  • docker restart restarts a container.
  • docker cp copies into a container.
  • docker insert inserts from a URL.
  • docker import creates a container from a tarball.
  • docker inspect looks at all the info on a container (including IP address).

Images

TODO

Volumes

TODO

Docker Volumes: http://crosbymichael.com/advanced-docker-volumes.html

Exposing Services

If you are running a bunch of services in Docker and want to expose them through Virtualbox to the host OS, you need to do something like this in your Vagrant:

    (49000..49900).each do |port|
      config.vm.network :forwarded_port, :host => port, :guest => port
    end

Let's start up Redis:

docker pull johncosta/redis
docker run -p 6379 -d johncosta/redis

Then find the port:

docker ps
docker port <redis_container_id> 6379

Then connect to the 49xxx port that Virtualbox exposes.

Dockerfiles

Useful scripts

Cleanup

docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs docker rm

eliminate:

docker rm `docker ps -a -q`

Running from an existing volume

docker run -i -t -volumes-from 5ad9f1d9d6dc mytag /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment