← tetsuya.dev

Docker on a Mac with Boot2Docker

Installing Boot2Docker on Mac OS, running CentOS in a container, taking it through start, attach, stop, and remove, and cleaning up afterward.

What is Docker?

Docker is an open-source tool for running applications in containers. dotCloud, a PaaS provider, announced it in March 2013. That October, the company renamed itself to Docker, Inc. and announced it would focus on the Docker business.

Unlike a traditional virtual machine, a Docker container doesn’t boot its own OS kernel. Containers are isolated processes that share the host’s Linux kernel, so they start almost instantly and carry far less overhead than a full VM. An image bundles an application with everything it needs to run, so it behaves the same on a laptop as in production, which is how Docker addresses the “works on my machine” problem.

Goal

My goal is to boot CentOS on Mac OS using Boot2Docker and Docker, then remove the containers when I’m done.

Docker needs a Linux kernel, and Mac OS doesn’t have one. Boot2Docker fills the gap: it’s a tiny Linux distribution that runs in VirtualBox and hosts the Docker daemon. The docker client on the Mac talks to that daemon over TCP.

Installing the tools

  • Homebrew
  • Homebrew Cask
  • VirtualBox
  • Boot2Docker
  • Docker

Homebrew and Homebrew Cask

If you don’t have Homebrew yet, install it first. Homebrew Cask extends it to install regular Mac OS apps such as VirtualBox.

shell
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install caskroom/cask/brew-cask

VirtualBox

shell
brew cask install virtualbox
==> Downloading http://download.virtualbox.org/virtualbox/4.3.14/VirtualBox-4.3.14-95030-OSX.dmg
######################################################################## 100.0%
==> Running installer for virtualbox; your password may be necessary.
==> installer: Package name is Oracle VM VirtualBox
==> installer: Installing at base path /
==> installer: The install was successful.
🍺  virtualbox installed to '/usr/local/Caskroom/virtualbox/4.3.14-95030' (4 files, 117M)

Boot2Docker and Docker

You can run brew install docker boot2docker, but brew install boot2docker alone is enough because it pulls in docker as a dependency.

shell
brew install boot2docker
==> Installing boot2docker dependency: docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/docker-1.1.2.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring docker-1.1.2.mavericks.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
 
zsh completion has been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/docker/1.1.2: 9 files, 9.8M
==> Installing boot2docker
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/boot2docker-1.1.2.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring boot2docker-1.1.2.mavericks.bottle.tar.gz
🍺  /usr/local/Cellar/boot2docker/1.1.2: 2 files, 7.0M

boot2docker init downloads the Boot2Docker ISO, generates an SSH key for the VM, and creates the VirtualBox VM.

shell
boot2docker init
2014/07/27 11:22:06 Downloading boot2docker ISO image...
2014/07/27 11:22:08 Latest release is v.1.1.2
2014/07/27 11:22:40 Success: downloaded https://github.com/boot2docker/boot2docker/releases/download/v.1.1.2/boot2docker.iso
  to /Users/tetsuya/.boot2docker/boot2docker.iso
Generating public/private rsa key pair.
Your identification has been saved in /Users/tetsuya/.ssh/id_boot2docker.
Your public key has been saved in /Users/tetsuya/.ssh/id_boot2docker.pub.
The key fingerprint is:
The key's randomart image is:
+--[ RSA 2048]----+
|           .+=+ E|
|            =+. .|
|           o =o..|
|            . ...|
|        S    .   |
|     . o .  o    |
|      + +  . .   |
|       . =..     |
|        +oo      |
+-----------------+
2014/07/27 11:22:41 Creating VM boot2docker-vm...
2014/07/27 11:22:41 Apply interim patch to VM boot2docker-vm (https://www.virtualbox.org/ticket/12748)
2014/07/27 11:22:41 Setting NIC #1 to use NAT network...
2014/07/27 11:22:41 Port forwarding [ssh] tcp://127.0.0.1:2022 --> :22
2014/07/27 11:22:41 Port forwarding [docker] tcp://127.0.0.1:2375 --> :2375
2014/07/27 11:22:41 Setting NIC #2 to use host-only network "vboxnet2"...
2014/07/27 11:22:41 Setting VM storage...
2014/07/27 11:22:48 Done. Type `boot2docker up` to start the VM.

Starting Boot2Docker

shell
boot2docker up
2014/07/27 11:22:59 Waiting for VM to be started...
...........
2014/07/27 11:23:32 Started.
2014/07/27 11:23:32 To connect the Docker client to the Docker daemon, please set:
2014/07/27 11:23:32     export DOCKER_HOST=tcp://192.168.59.103:2375

Checking the status

shell
boot2docker status
running

Pulling the CentOS image

shell
docker pull centos
2014/07/27 11:24:10 Post http:///var/run/docker.sock/images/create?fromImage=centos&tag=: dial unix /var/run/docker.sock: no such file or directory

The pull failed because I’d skimmed past this part of the boot2docker up output:

output
2014/07/27 11:23:32 To connect the Docker client to the Docker daemon, please set:
2014/07/27 11:23:32     export DOCKER_HOST=tcp://192.168.59.103:2375

Without DOCKER_HOST, the docker client looks for a daemon on the local Unix socket /var/run/docker.sock, and there isn’t one on the Mac because the daemon runs inside the VM. I keep my environment variables in ~/.bash_exports, which ~/.bash_profile loads, so I added the line there and reloaded the profile:

shell
echo 'export DOCKER_HOST=tcp://192.168.59.103:2375' >> ~/.bash_exports
source ~/.bash_profile

Now docker version reports both the client and the server:

shell
docker version
Client version: 1.1.2
Client API version: 1.13
Go version (client): go1.3
Git commit (client): d84a070
Server version: 1.1.2
Server API version: 1.13
Go version (server): go1.2.1
Git commit (server): d84a070
 
docker info
Containers: 0
Images: 0
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Dirs: 0
Execution Driver: native-0.2
Kernel Version: 3.15.3-tinycore64
Debug mode (server): true
Debug mode (client): false
Fds: 10
Goroutines: 10
EventsListeners: 0
Init Path: /usr/local/bin/docker
Sockets: [unix:///var/run/docker.sock tcp://0.0.0.0:2375]

Now the pull goes through. Pulling centos fetches every tag in the repository: centos7 and latest point to the same image ID, and centos6 is a separate image.

shell
docker pull centos
Pulling repository centos
cd934e0010d5: Download complete
1a7dc42f78ba: Download complete
511136ea3c5a: Download complete
34e94e67e63a: Download complete
 
docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos7             1a7dc42f78ba        2 weeks ago         236.4 MB
centos              latest              1a7dc42f78ba        2 weeks ago         236.4 MB
centos              centos6             cd934e0010d5        2 weeks ago         206.9 MB

Running CentOS

docker run creates a new container from an image and runs a command in it. --interactive keeps stdin open and --tty allocates a terminal, so together they give us an interactive shell. First, CentOS 6:

shell
docker run --interactive --tty centos:centos6 /bin/bash
bash-4.1# cat /etc/redhat-release
CentOS release 6.5 (Final)
bash-4.1# exit
exit

Without a tag, centos means centos:latest, which is CentOS 7:

shell
docker run --interactive --tty centos /bin/bash
bash-4.2# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
bash-4.2# exit
exit

The CentOS 7 container started almost instantly. Typing exit ends /bin/bash, and since that was the container’s main process, the container stops too.

The container lifecycle

Listing containers

docker ps lists only running containers. Add --all to include stopped ones. Both containers we just exited are still there, with status Exited. Since we didn’t pass --name, Docker gave them random names: hopeful_yonath and pensive_mayer.

shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0cfd8af88b83        centos:centos7      /bin/bash           1 minutes ago       Exited (0) 1 minutes ago                        hopeful_yonath
614c80e55e30        centos:centos6      /bin/bash           10 minutes ago      Exited (0) 10 minutes ago                       pensive_mayer

Restarting a stopped container

docker start restarts an existing container with its original command. Changes made to its filesystem are still there. It runs in the background, and because the container has a TTY, /bin/bash keeps running and waits for input. Restart the CentOS 7 container:

shell
docker start 0cfd8af88b83
0cfd8af88b83
shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0cfd8af88b83        centos:centos7      /bin/bash           2 minutes ago       Up 14 seconds                                   hopeful_yonath
614c80e55e30        centos:centos6      /bin/bash           11 minutes ago      Exited (0) 11 minutes ago                       pensive_mayer

Attaching and detaching

docker attach connects your terminal to the container’s running process. Press Enter to get the prompt back. To leave without stopping the container, press Ctrl-P then Ctrl-Q to detach. Typing exit would end bash, and the container with it.

shell
docker attach 0cfd8af88b83
bash-4.2#
 
 

After detaching, the container is still Up:

shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0cfd8af88b83        centos:centos7      /bin/bash           3 minutes ago       Up 1 minutes                                    hopeful_yonath
614c80e55e30        centos:centos6      /bin/bash           12 minutes ago      Exited (0) 12 minutes ago                       pensive_mayer

Removing a container

To remove every container at once, pass all the IDs from docker ps --all --quiet (--quiet prints only the IDs) to docker rm:

shell
docker rm `docker ps --all --quiet`
Error response from daemon: Impossible to remove a running container, please stop it first or use -f
614c80e55e30
2014/07/27 11:41:03 Error: failed to remove one or more containers

The stopped CentOS 6 container 614c80e55e30 was removed, but Docker won’t remove a running container, so the CentOS 7 one is still there:

shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0cfd8af88b83        centos:centos7      /bin/bash           4 minutes ago       Up 2 minutes                            hopeful_yonath

So stop it first (or force removal with docker rm -f, as the error suggests). docker stop sends SIGTERM, waits 10 seconds, then sends SIGKILL. Bash running as PID 1 ignores SIGTERM, so it got killed after the timeout. That’s why the exit code is -1 instead of 0.

shell
docker stop 0cfd8af88b83
0cfd8af88b83
shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
0cfd8af88b83        centos:centos7      /bin/bash           5 minutes ago        Exited (-1) 7 seconds ago                       hopeful_yonath

Now docker rm works, and the container is gone for good, along with any changes made inside it:

shell
docker rm 0cfd8af88b83
0cfd8af88b83
shell
docker ps --all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Cleaning up

Removing images

Images are removed with docker rmi. Removing centos only untags centos:latest. The image itself stays, because centos:centos7 still points to it:

shell
docker rmi centos
Untagged: centos:latest
shell
docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos7             1a7dc42f78ba        2 weeks ago         236.4 MB
centos              centos6             cd934e0010d5        2 weeks ago         206.9 MB

Removing the last tag deletes the image. Removing centos6 also deletes its parent layers, since no other image uses them anymore:

shell
docker rmi centos:centos7
Untagged: centos:centos7
Deleted: 1a7dc42f78ba213ec1ac5cd04930011334536214ad26c8000f1eec72e302c041
 
docker rmi centos:centos6
Untagged: centos:centos6
Deleted: cd934e0010d5a63c3137a6d0d6b1cdeca68a02bafd2a51554be61dfd6b6dda80
Deleted: 34e94e67e63a0f079d9336b3c2a52e814d138e5b3f1f614a0cfe273814ed7c0a
Deleted: 511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158
shell
docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

Shutting down Boot2Docker

boot2docker stop powers off the VM. The images and containers stored in it are kept for the next boot2docker up.

shell
boot2docker stop

boot2docker destroy deletes the VM entirely:

shell
boot2docker destroy
shell
boot2docker status
2014/07/27 11:48:12 Failed to get machine "boot2docker-vm": machine does not exist

The downloaded ISO (~/.boot2docker/boot2docker.iso) and the SSH key (~/.ssh/id_boot2docker) are left behind. Delete them manually if you want a completely clean slate.