Skip to content

Instantly share code, notes, and snippets.

@lobster1234
Last active October 5, 2016 04:53
Show Gist options
  • Save lobster1234/192d208b12f1ca741bc854eea1450d76 to your computer and use it in GitHub Desktop.
Save lobster1234/192d208b12f1ca741bc854eea1450d76 to your computer and use it in GitHub Desktop.
Notes for docker setup

Installation of Docker Platform (MacOS Sierra)

  • Head to https://docs.docker.com/docker-for-mac/
  • Download the latest docker binary from https://download.docker.com/mac/stable/Docker.dmg
  • Install it (drag and drop)
  • Go to applications, and click on the Docker icon, it will walk you through the rest of the setup.
  • You can notice the docker whale animating in the toolbar at the top right of your mac as it initalizes.
  • You can click that whale to see the current status of docker engine.
  • What has been installed is the docker engine (to manage and run containers), docker machine (the host) and docker compose.
  • We will also need virtualbox, so install it from https://www.virtualbox.org/wiki/Downloads

Setting up a (virtual) Docker Host

Now we prepare our Mac to build a (or many) docker host(s). These host(s) can run many docker containers. This is where virtualbox comes in, as it is the "driver" we will use to run our virtual host. Interestingly, as we've installed the docker platform on the Mac, it has also become a host. We will not really be using the hosts being created below to run containers, as its much easier (for the purpose of this tutorial) to use our Mac as the host and run containers in it. But this will help you get an overview of real world deployments, where the containers themselves run in VMs. In this case the VM will be hosted in Virtualbox.

  • Lets list the docker machines that already exist, this should turn up empty for now.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine ls
NAME   ACTIVE   DRIVER   STATE   URL   SWARM   DOCKER   ERRORS
  • Now we will set up a host VM (boot2docker), an ISO run within virtualbox. Basically it is a super lightweight linux distro (tinycore), optimized to run containers. If interested, you can read more about it here - https://github.com/boot2docker/boot2docker.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine create --driver virtualbox default
Running pre-create checks...
(default) Default Boot2Docker ISO is out-of-date, downloading the latest release...
(default) Latest release for github.com/boot2docker/boot2docker is v1.12.1
(default) Downloading /Users/mpandit/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v1.12.1/boot2docker.iso...
(default) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(default) Copying /Users/mpandit/.docker/machine/cache/boot2docker.iso to /Users/mpandit/.docker/machine/machines/default/boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env default
  • Now we can verify that our host is up and running. Note that this command gave us an empty list. Interstingly, you can also see this VM running in virtualbox - if we look at the Virtualbox console, we can see a machine with name "default" running.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.1   
  • We can create mutliple such hosts by changing the name, and you'll see them all listed here.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine create --driver virtualbox new-default
  • And it will show up (and also in Virtualbox console)
Manishs-MacBook-Pro:tmp mpandit$ docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default       -        virtualbox   Running   tcp://192.168.99.100:2376           v1.12.1   
new-default   -        virtualbox   Running   tcp://192.168.99.101:2376           v1.12.1   
  • We can see that both the hosts are running. We can inspect these as well. If you do not pass a name, the default of default is assumed. However, it is a good idea to specify one, I prefer it personally. The output JSON contains a lot of details about this machine like networking, CPU/memory, storage, security, etc.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine inspect default
Manishs-MacBook-Pro:tmp mpandit$ docker-machine inspect new-default
  • Next, lets ssh into one of these hosts and look around. Please note that there is no traditional login credentials, the auth happens via SSH keys that get generated when this host was created.
Manishs-MacBook-Pro:tmp mpandit$ docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.12.1, build HEAD : ef7d0b4 - Thu Aug 18 21:18:06 UTC 2016
Docker version 1.12.1, build 23cf638
docker@default:~$ 
  • Type busybox to see what all commands are available. You can read more about busybox here - https://busybox.net/. It is a set of popular unix/linux commands packaged together with size in mind, optimized for embedded distributions. You'll notice the host VM using busybox.
docker@new-default:/$ nslookup
BusyBox v1.24.2 (2016-05-16 13:28:30 UTC) multi-call binary.

Usage: nslookup [HOST] [SERVER]

Query the nameserver for the IP address of the given HOST
optionally using a specified DNS server

Running containers

  • In order to run a container, we will need an "image" that the container will run off of. This image can be a standard, dev/production quality Linux distribution, as that is what the container will be. This image is different than the tinylinux which is running on our hosts, as we'd need this to run our application code. We can do all of this on our virtual hosts (default and new-default), or we can use our Mac. The commands below will work on those two hosts as well - I will use the Mac as the host for the containers for the rest of the tutorial.

We will pull ubuntu image from the docker hub public image repository. This command will download the latest version, but you may specify a particular version by appending it at the end separated by a colon, like so - docker pull ubuntu:15.10

Manishs-MacBook-Pro:tmp mpandit$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
cad964aed91d: Pull complete 
3a80a22fea63: Pull complete 
50de990d7957: Pull complete 
61e032b8f2cb: Pull complete 
9f03ce1741bf: Pull complete 
Digest: sha256:28d4c5234db8d5a634d5e621c363d900f8f241240ee0a6a978784c978fe9c737
Status: Downloaded newer image for ubuntu:latest

We can verify that the image is now available to run containers from.

Manishs-MacBook-Pro:tmp mpandit$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              c73a085dc378        4 days ago          127.1 MB

We can run commands on this container too!

Manishs-MacBook-Pro:tmp mpandit$ docker run ubuntu ls

However, this is not as much fun. We'd need to be able to get into an interactive session with this container. In order to do so, we run bash and attach a terminal to it.

Manishs-MacBook-Pro:tmp mpandit$ docker run -t -i ubuntu 
root@35e6446ba95f:/# 
root@35e6446ba95f:/# uname -a
Linux 35e6446ba95f 4.4.20-moby #1 SMP Thu Sep 15 12:10:20 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

What!? You now have a running container!

Do not be surprised to see basic commands like curl, wget, git not available. To get them on this container, you'll need to run apt-get update, followed by apt-get install <whatever you want to>.

Open another terminal window and see this container listed like so -

Manishs-MacBook-Pro:tmp mpandit$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
35e6446ba95f        ubuntu              "/bin/bash"         2 minutes ago       Up 2 minutes                            cocky_curie

If you do not like cocky_curie, you can rename it.

Manishs-MacBook-Pro:tmp mpandit$ docker rename 35e6446ba95f  first_bash
Manishs-MacBook-Pro:tmp mpandit$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
35e6446ba95f        ubuntu              "/bin/bash"         4 minutes ago       Up 4 minutes                            first_bash

I am sure you've noticed that we ran a container right off our Mac, and not from the hosts (default and new_default) that we had created earlier. This is where Docker shines. Our Mac is also a host, by the virtue of the docker engine running on it.

We can now stop our container, and make sure it's no longer showing up in ps.

Manishs-MacBook-Pro:tmp mpandit$ docker stop first_bash
first_bash

Manishs-MacBook-Pro:tmp mpandit$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Lets start our container again, just to make a very important point. We will create a simple text file, and save it as /welcome.txt.

Manishs-MacBook-Pro:tmp mpandit$ docker run -it  ubuntu bash
root@25580337de27:/# pwd
/
root@25580337de27:/# echo "Welcome to Docker, I am in a container" > /welcome.txt 
root@25580337de27:/# cat /welcome.txt
Welcome to Docker, I am in a container

Next, we exit this container.

root@25580337de27:/# exit
exit
Manishs-MacBook-Pro:tmp mpandit$ docker run -it  ubuntu bash
root@f158471eb7c5:/# cat /welcome.txt
cat: /welcome.txt: No such file or directory

What happened? Why did the file disappear? The answer lies in a very important docker concept - the containers are ephermal. In other words, when we exit the container, it is gone. The image (ubuntu:latest) is same, but not the containers themselves. If you notice, the second time we ran docker run, the container-id was different than the one we used to create our file in (25580337de27 vs f158471eb7c5). This is a very important notion to understand.

If we want to see the details of all the containers we had ever started and exited, or running, we use docker ps -a, basically same as before except we add a -a to it. We can see pretty much a history of our containers, the images used for them, and how long they lived for.

Manishs-MacBook-Pro:tmp mpandit$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
f158471eb7c5        ubuntu              "bash"                   6 minutes ago       Exited (0) 2 seconds ago                             jolly_dijkstra
25580337de27        ubuntu              "bash"                   8 minutes ago       Exited (0) 6 minutes ago                             distracted_keller

Let's see how we can get back to the container so we do not lose any work we do - or make it practical, like running a tomcat with our webapp on it.

We can attach to a container via its id. Let us attach to the container where we created our welcome.txt file.

Manishs-MacBook-Pro:tmp mpandit$ docker attach 25580337de27
root@25580337de27:/# 
root@25580337de27:/# cat /welcome.txt
Welcome to Docker, I am in a container

There we have it - we are back into a container that we exited. The container-id gives us a unique handle to manage our container's lifecycle.

However, we do not want to be doing this all the time. In real world situations, the containers will be virtual server running tomcat, nginx, apache or any business application, and need to be "available". Think of running in the background, or as deamons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment