Docker QuickStart (Mac Tutorial)
Docker is an application that allows you to package up an OS and deploy it to a machine. You could put your entire application on it along with all the files required to run it. However this fact plays a large part in the design of your application. This presses for a more modular design and allows you to package entire servers into a simple deployable. Examples are web frontends, backend apis, and maintenance scripts. Persistent datastores like databases or file stores should now be hosted externally (Think writing to S3 rather than the local files system).
Now if your infrastructure is under heavy load, spin up more instances of your frontend or apis to handle the load. Rip them down when you're done. Your docker instances don't have state so that shouldn't be a problem.
Mile high overview: Easily create images which are self contained configured OS you can run on a machine. Use docker-machine to point to a machine (a server) and run the docker command that is on it.
Installation and setup
Install brew, a package manager for mac
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install cask, to easily install full applications into /Applications
brew tap caskroom/cask
Get the docker machine image, this will have the docker daemon on it and allow you to put images on the machine
curl -L https://github.com/docker/machine/releases/download/v0.6.0/docker-machine-`uname -s`-`uname -m` > ~/bin/docker-machine && chmod +x ~/bin/docker-machine
Put the binary on your path, in ~/.bash_profile
export PATH=$PATH:~/bin
Install the apps
brew cask install virtualbox
brew install docker
brew install docker-machine
Create a machine called local
docker-machine create --driver virtualbox local
Login to the machine
docker-machine ssh local
To restart the machine
docker-machine stop local
docker-machine start local
From here you can open VirtualBox and see a new machine called "local". You can also run this command and docker-machine will show it has been created.
$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS local - virtualbox Running tcp://192.168.99.100:2376 v1.10.2
List the configuration parameters to point docker-machine at it and allow us to run the docker instance on it
$ docker-machine env local export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/johnminchuk/.docker/machine/machines/local" export DOCKER_MACHINE_NAME="local" # Run this command to configure your shell: # eval $(docker-machine env local)
As this output suggest run this command. Now you're pointing at docker running on that machine
eval $(docker-machine env local)
Let's put hello-world on that machine
docker run hello-world
Cool, now le'ts try putting a vanilla ubuntu image on the machine and use these switches to connect to it using bash
docker run -it ubuntu bash
You are now using the image which is sitting on the machine you're pointing at
$ docker run -it ubuntu bash root@6ff5c3e4b1ef:/#
Run this command and you can see the images we just installed on that machine
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 07c86167cdc4 6 days ago 188 MB hello-world latest 690ed74de00f 4 months ago 960 B
Create your own docker image
On our local machine create a new folder
mkdir jmdockerimage
Create a new file with
vim Dockerfile
And add this information, here we're saying use docker/whalesay:latest as the base image, provision with these commands, then run the command at the end
FROM docker/whalesay:latest RUN apt-get -y update && apt-get install -y fortunes CMD /usr/games/fortune -a | cowsay
Now let's build the image and place it on the machine we're pointed at
docker build -t docker-whale .
And put it on that machine
docker run docker-whale
Great! Now we've created a new image from some configuration files on our local machine, we could even check this into source control if we wanted.
We should now see it listed as an image on that machine
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker-whale latest 3d5122471074 About an hour ago 274.4 MB ubuntu latest 07c86167cdc4 6 days ago 188 MB hello-world latest 690ed74de00f 4 months ago 960 B
Let's do one more for practice
mkdir jmdocker1
cd jmdocker1
vim Dockerfile
Add this code to the Dockerfile
FROM ubuntu:latest RUN apt-get install -y lynx? CMD echo "do we have lynx" && whereis lynx
Now lets build that image
docker build -t jmdocker1 .
Let's now list the images on that machine, remember this is not saved locally, it's placed on the machine you configured youreself to point at
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jmdocker1 latest 920e65feb6b9 3 minutes ago 191.2 MB
docker-whale latest 3d5122471074 25 minutes ago 274.4 MB
ubuntu latest 07c86167cdc4 6 days ago 188 MB
hello-world latest 690ed74de00f 4 months ago 960 B
docker/whalesay latest 6b362a9f73eb 9 months ago 247 MB
Now let's run that image we just put on there
docker run jmdocker1
Output...
do we have lynx?
lynx: /usr/bin/lynx /usr/share/man/man1/lynx.1.gz
Reconnecting
If we close down our terminal we're going to loose the environment variables we just setup. We will need to reconnect to the machine. Close down terminal, and open it back up.
list available machiens to put docker images on. I've since added another machine called "default".
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.101:2376 v1.10.2
local - virtualbox Running tcp://192.168.99.100:2376 v1.10.2
See parameters for one of those machines
docker-machine env default
Point this terminal session at that machine
eval $(docker-machine env local)
Now we can use the docker command on the machine we just pointed to, let's see what images are on it
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jmdocker1 latest 920e65feb6b9 30 minutes ago 191.2 MB
docker/whalesay latest 6b362a9f73eb 9 months ago 247 MB