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.

  1. Please go through the steps outlined under Preflight Checks to ensure the system is properly configured.

  2. Install PostgreSQL on the Mac OS X host. To keep the length of this tutorial manageable, we leave the installation and the configuration of PostgreSQL on Mac OS X to the readers.

  3. As a result of the way the Rails application is booted inside the Docker container, the environment will be development by default. Review the development section of config/database.yml.

    development:
      adapter: postgresql
      encoding: unicode
      database: <%= ENV['DOCKERDB_ENV_POSTGRESQL_DB'] %>
      pool: 5
      username: <%= ENV['DOCKERDB_ENV_POSTGRESQL_USER'] %>
      password: <%= ENV['DOCKERDB_ENV_POSTGRESQL_PASS'] %>
      host: <%= ENV['DOCKERDB_PORT_5432_TCP_ADDR'] %>
      port: <%= ENV['DOCKERDB_PORT_5432_TCP_PORT'] %>
    
  4. 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 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 to be started...
    .......
    Started.
    To connect the Docker client to the Docker daemon, please set:
        export DOCKER_HOST=tcp://192.168.59.103:2375
    
  2. Continue reading Preflight Checks

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.

  1. Install nsenter and docker-enter into /var/lib/boot2docker/ on the Boot2Docker virtual machine.

    Host% boot2docker ssh
                            ##        .
                      ## ## ##       ==
                   ## ## ## ##      ===
               /""""""""""""""""\___/ ===
          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
               \______ o          __/
                 \    \        __/
                  \____\______/
     _                 _   ____     _            _
    | |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
    | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
    | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
    |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
    
      boot2docker with VirtualBox guest additions version 4.3.14
    
    boot2docker: 1.2.0
                 master : e75396e - Fri Aug 22 06:03:48 UTC 2014
    
    docker@boot2docker:~$ docker run --rm \
    -v /var/lib/boot2docker:/target jpetazzo/nsenter
    
    Installing nsenter to /target
    Installing docker-enter to /target
    
  2. docker-enter is simply a small wrapper shell script that calls the nsenter binary. docker-enter takes a container ID or a container name as a parameter to look up the container’s process ID (PID) on the Boot2Docker virtual machine. Once it has the container’s PID, docker-enter can invoke nsenter and shell into the running container.

    # What docker-enter does behind the scenes
    docker@boot2docker:~$ PID=$(docker inspect \
    --format {{.State.Pid}} <container_name_or_ID>)
    
    docker@boot2docker:~$ nsenter --target $PID \
    --mount --uts --ipc --net --pid
    
  3. Add the docker-enter shell function to your .profile, so docker-enter may be called directly from the Mac OS X host; otherwise, Mac OS X users would have to first ssh into their Boot2Docker virtual machines before they may shell into running containers.

    Host% more ~/.profile
    ...
    docker-enter() {
        boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter $@
    }
    

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