Starting Over on Mac OS X

image

Practice makes perfect. Some readers may find going through this tutorial from the beginning helpful in solidifying their understanding of Docker. The following steps will restore a Mac OS X host to the state before the completion of this tutorial. We run through them regularly, so we may repeat the quick start guide ourselves as we make revisions and updates.

Continue reading Starting Over on Mac OS X

Developing with Docker Containers

image

For Docker to deliver on that promise of consistent development and deployment, a developer should be able to work on the same container that will be shipped to production. One way for a developer to accomplish this goal is to build the application’s Docker image, launch the resulting Docker image, and check out the dockerized application on the development machine every time the code changes. Although each round of the Docker image rebuild can be sped up dramatically with proper caching, this workflow remains tedious and awkward.

Continue reading Developing with Docker Containers

Linking a Dockerized Application to a Database on the Mac OS X Host

image

The database service is often not on the same host as the application. Earlier we launched PostgreSQL Service as a Docker Container on the Boot2Docker virtual machine and accessed it from the Mac OS X host. Now we will set up the PostgreSQL service on the Mac OS X host and connect to it from a dockerized Rails application on the Boot2Docker virtual machine.

Continue reading Linking a Dockerized Application to a Database on the Mac OS X Host

Building Images with Dockerfiles

image

As part of going through the steps outlined under Running a Rails App over Two Docker Containers, we pulled the learningdocker/docker_quick_start image from Docker Hub onto the Boot2Docker virtual machine. Instead of using an already made image, we will now build the Rails application’s Docker image on the Boot2Docker virtual machine via our Mac OS X host and push the final image onto our Docker Hub accounts.

Continue reading Building Images with Dockerfiles

Running a Rails App over Two Docker Containers

image

As exciting as running a PostgreSQL server inside a Docker container may be, what usually sells Docker to developers is seeing how Docker handles their favorite web applications. In this section, we will launch a fully functional Rails application over two Docker containers. One container will house the PostgreSQL server, and the other container will host the application code forked from a project developed and maintained by Michael Hartl for his self-paced learning program Ruby on Rails Tutorial: Learn Web Development with Rails.

Continue reading Running a Rails App over Two Docker Containers

Running PostgreSQL Service as a Docker Container

image

Docker, Inc. has published a detailed tutorial on Dockerizing PostgreSQL. To keep the length of this tutorial manageable, we will simply use a publicly available Docker image hosted on Docker Hub. We leave Docker’s official example as an exercise for readers who want a deeper understanding of how Docker containerizes the PostgreSQL service. The goal of this section is simply to introduce some basic Docker commands and frequently used command options.

Continue reading Running PostgreSQL Service as a Docker Container

Preflight Checks

image

  1. Make sure the Boot2Docker virtual machine is running.

    # Check the Boot2Docker virtual machine's status.
    Host% boot2docker status
    poweroff
    
    # Fire up the Boot2Docker virtual machine.
    Host% boot2docker start
    Waiting for VM and Docker daemon to start...
    ...........ooo
    Started.
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/ca.pem
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/cert.pem
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/key.pem
    Your environment variables are already set correctly.
    
  2. Make sure the Docker daemon is running on the Boot2Docker virtual machine.

    Host% docker info
    Containers: 0
    Images: 18
    Storage Driver: aufs
     Root Dir: /mnt/sda1/var/lib/docker/aufs
     Dirs: 18
    Execution Driver: native-0.2
    Kernel Version: 3.16.4-tinycore64
    Operating System: Boot2Docker 1.3.0 (TCL 5.4); master : a083df4 - Thu Oct 16 17:05:03 UTC 2014
    Debug mode (server): true
    Debug mode (client): false
    Fds: 10
    Goroutines: 11
    EventsListeners: 0
    Init Path: /usr/local/bin/docker
    Username: learningdocker
    Registry: [https://index.docker.io/v1/]
    
  3. In order for a terminal to connect to the Docker daemon, the DOCKER_HOST environment variable must be set properly. New terminals are not launched with the DOCKER_HOST environment variable initialized automatically, so they will not know how to connect to the Docker daemon by default.

    # If the DOCKER_HOST environment variable has not been set:
    Host% docker info
    Get http:///var/run/docker.sock/v1.13/info: dial unix /var/run/docker.sock: no such file or directory
    
    # The DOCKER_HOST environment variable is not defined:
    Host% echo $DOCKER_HOST
    
    # Set the DOCKER_HOST environment variable:
    Host% $(/usr/local/bin/boot2docker shellinit)
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/ca.pem
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/cert.pem
    Writing /Users/jimmyhuang/.boot2docker/certs/boot2docker-vm/key.pem
    
    Host% echo $DOCKER_HOST
    tcp://192.168.59.103:2376
    

Getting Inside Running Docker Containers without SSH

image

Jerome Petazzoni of Docker, Inc. published an article on his company’s blog to show how people can get inside running Docker containers with a lightweight tool called nsenter. By letting people enter the Linux kernel namespaces that Docker uses to build containers, nsenter effectively gives them shell access to running Docker containers without SSH daemon.

Continue reading Getting Inside Running Docker Containers without SSH

Resizing Boot2Docker Disk Volumes

image

Once developers really start containerizing their applications, they often generate a large number of images and quickly fill up the 20GB hard drive space allocated to the Boot2Docker virtual machine by default. To make sure the Boot2Docker virtual machine has plenty of disk space, we should resize /dev/sda1 to a number that is more reasonable.

Host% boot2docker ssh
                            ##        .
                      ## ## ##       ==
                   ## ## ## ##      ===
               /""""""""""""""""\___/ ===
          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
               \______ o          __/
                 \    \        __/
                  \____\______/
     _                 _   ____     _            _
    | |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
    | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
    | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
    |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
    Boot2Docker version 1.4.1, build master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014
    Docker version 1.4.1, build 5bc2ff8
docker@boot2docker:~$ df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                    1.8G     85.8M      1.7G   5% /
tmpfs                     1.8G     85.8M      1.7G   5% /
tmpfs                  1004.0M         0   1004.0M   0% /dev/shm
/dev/sda1                18.2G     58.2M     17.2G   0% /mnt/sda1
cgroup                 1004.0M         0   1004.0M   0% /sys/fs/cgroup
none                    930.7G    231.8G    699.0G  25% /Users
/dev/sda1                18.2G     58.2M     17.2G   0% /mnt/sda1/var/lib/docker/aufs

As documented on Docker’s official website, the solution is to resize the disk volume using a hard drive partitioning tool called Gnome Partition Editor (GParted). Available as a free bootable ISO, GParted is a free partition manager that enables users to resize, copy, and move partitions without data loss. The version of GParted used in this tutorial is gparted-live-0.20.0-2-i486.iso.

  1. Stop the Boot2Docker virtual machine.

    Host% boot2docker stop
    
  2. The Boot2Docker package installer ships with a VMDK volume, which VirtualBox’s native tools cannot resize. In order to resize the Boot2Docker disk volume, first clone the VDI volume from the default VMDK volume.

    Host% vboxmanage clonehd /full/path/to/boot2docker-vm.vmdk /full/path/to/boot2docker-vm.vdi --format VDI --variant Standard
    
    # For Boot2Docker installations using the default settings, "/full/path/to/"
    # is typically "~/VirtualBox\ VMs/boot2docker-vm/".  This tutorial will assume
    # "/full/path/to/" to be "~/VirtualBox\ VMs/boot2docker-vm/".
    
    Host% vboxmanage clonehd ~/VirtualBox\ VMs/boot2docker-vm/boot2docker-vm.vmdk ~/VirtualBox\ VMs/boot2docker-vm/boot2docker-vm.vdi --format VDI --variant Standard
    0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
    Clone hard disk created in format 'VDI'. UUID: f37d6330-c6f3-4a2d-914b-f61c3249ca0b
    
  3. Resize the newly cloned VDI volume to the desired capacity. We recommend at least 64GB.

    Host% vboxmanage modifyhd ~/VirtualBox\ VMs/boot2docker-vm/boot2docker-vm.vdi --resize <size in MB>
    
  4. Launch the VirtualBox application, and click on the “Settings” gear on top.

    image

  5. Click on the “Storage” icon. Remove the default VMDK volume.

    image

  6. Add a new IDE controller.

    image

  7. Mount the GParted ISO via the “Add CD/DVD Device” option.

    image

  8. Mount the cloned VDI volume via the “Add CD/DVD Device” option.

    image

  9. If you are running Docker on a laptop with a solid-state hard drive, please make sure the “Solid-state Drive” option is selected for the cloned VDI volume.

    image

  10. Click on the “System” icon to review the boot order. Please deselect the first CD/DVD checkbox to ensure the GParted ISO is booted first.

    image

  11. Click on the “Start” icon to boot up the Boot2Docker virtual machine, which will launch the GParted ISO. Select “GParted Live (Default settings)”.

    image

  12. Set the policy for handling keymaps to “Don’t touch keymap”.

    image

  13. Set language preference to option “33”, which maps to “US English”.

    image

  14. Select option “1” to run “Forcevideo” and configure X manually.

    image

  15. Keep the default resolution of “1024×760” by selecting option “2”.

    image

  16. Keep the default “vesa” as the VGA card.

    image

  17. Keep the default color depth of “24” by selecting option “0”.
    image

  18. Once GParted launches, click on the “Resize/Move” icon.
    image

  19. Set the new disk volume size to the maximum. In this example, the maximum size is 127,035MB. Click on the “Resize/Move” button to start the process.

    image

  20. Confirm the resizing operation by clicking on the “Apply” button.

    image

  21. Power off the machine after the resizing operation finishes.

    image

  22. Remove the GParted ISO.

    image

  23. Log into the Boot2Docker virtual machine to verify that the volume resizing was successful.

    Host% boot2docker ssh
                            ##        .
                      ## ## ##       ==
                   ## ## ## ##      ===
               /""""""""""""""""\___/ ===
          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
               \______ o          __/
                 \    \        __/
                  \____\______/
     _                 _   ____     _            _
    | |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
    | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
    | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
    |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
    Boot2Docker version 1.4.1, build master : 86f7ec8 - Tue Dec 16 23:11:29 UTC 2014
    Docker version 1.4.1, build 5bc2ff8
    docker@boot2docker:~$ df -h
    Filesystem                Size      Used Available Use% Mounted on
    rootfs                    1.8G     85.8M      1.7G   5% /
    tmpfs                     1.8G     85.8M      1.7G   5% /
    tmpfs                  1004.0M         0   1004.0M   0% /dev/shm
    /dev/sda1               122.0G     73.8M    115.7G   0% /mnt/sda1
    cgroup                 1004.0M         0   1004.0M   0% /sys/fs/cgroup
    none                    930.7G    232.8G    697.9G  25% /Users
    /dev/sda1               122.0G     73.8M    115.7G   0% /mnt/sda1/var/lib/docker/aufs
    

Running Boot2Docker on Mac OS X

image

Because the Docker engine relies on Linux-specific kernel features, it cannot run on Mac OS X directly yet. Users who wish to run Docker on Mac OS X must do so through a Linux virtual machine. This extra layer can make working with Docker on Mac OS X a bit unwieldy, espeically for people who are just getting started. To make Docker easier to use on Mac OS X, Docker, Inc. developed Boot2Docker to streamline this process.

Continue reading Running Boot2Docker on Mac OS X