Intro to Docker

On this week's episode, Chris is joined by thoughtbot CTO Joe Ferris to discuss Docker, the open platform for building and running containerized applications. What is Docker? Docker is a platform built to support working with containers for isol...
This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/intro-to-docker
2 Likes

Thanks, that was great explanation of some Docker concepts Iā€™ve found tricky to understand. Itā€™s encouraged me to try containerize one of own apps.

Certainly the biggest caveat in dev is performance.

It is perfectly fine when used on Linux.
But on a mac since it runs through boot2docker (a small vm) and since you have to share your projet repo with boot2docker and then boot2docker shares them with docker containersā€¦ It slows everything down

It works with small projects but when you begin to have a lot of assets it becomes really really slow.

Make sure to share your directories with boot2docker through nfs.
An even better solution is to create a custom vm with vagrant. Install docker on this vm and sync your directories with rsync (rsync is available as an option with vagrant and shared directories).

Thanks Chris, Joe, and Thom. This motivated me to noodle around a bit with docker. I did run into an issue with one of the commands from the show notes. I figured Iā€™d add some documentation here about my experience for anyone else following along and having trouble on OS X.

Environment details:

OS X with a Virtualbox guest provisioned with docker-machine

VirtualBox 5.0.4-102546

=> docker-machine --version
docker-machine version 0.4.1 (HEAD)

=> docker --version
Docker version 1.8.2, build 0a8c2e3

Starting the upcase rails server with the following command fails:

docker run --link=db -it upcase -p 3000 bin/rails server -b 0.0.0.0

Error response from daemon: Cannot start container e82e64ed06bb1dc476d5f0a2b6156afd56d458a96fb95daf6be23411649de663: [8] System error: exec: "-p": executable file not found in $PATH

This indicates that the container is trying to run the port option as a command within the container. Ok no problem, just move the -p 3000 option before the -it upcase option.

Now the container starts, but requests to localhost:3000 fail with the following error:

Failed to load resource: Could not connect to the server.

This indicates that the host OS canā€™t connect to the Virtualbox Docker VM running our container. So forward the VM ports through to the host.

  1. Open Virtualbox
  2. Open the Settings for the docker-machine VM
  3. Choose the Network tab
  4. Click the Port Forwarding button
  5. Add a rule with the following values:
  6. Name: Webrick
  7. Protocol: TCP
  8. Host IP: 127.0.0.1
  9. Host Port: 3000
  10. Guest IP: (intentionally blank)
  11. Guest Port: 3000
  12. Click OK
  13. Click OK

Now when we navigate to localhost:3000 from the host OS weā€™re faced with the following error:

Failed to load resource: The network connection was lost.

Simple enough, now we need the docker container to setup the host-guest port forwarding. So stop the upcase docker container and start it again with the following command ā€“ notice the changes to the -p 3000:3000 parameter:

=> docker run --link=db -p 3000:3000 -it upcase bin/rails server -b 0.0.0.0

Now visit localhost:3000 and you should see a response from the rails process running inside the upcase container.

Hope this helps someone else whoā€™s having a hard go of it on OS X.

1 Like

Any suggestions on using docker in development and still being able to do BDD / TDD. I like being able to run specs from my editor, and I like that spring has made running specs and getting feedback super fast, but I am not sure how to achieve that using docker.

Iā€™ve been doing TDD with Docker. Iā€™m pretty swamped today, so I canā€™t give a cohesive answer, but here are some tips:

  • You can override the command for a service using docker-compose run, so you can run commands like docker-compose run web rspec <file>.
  • If youā€™re using vim-rspec, you can override the command to include docker-compose run. You can also create new binstubs which include the docker-compose commands.

Iā€™ll try to post a more comprehensive guide soon.

Huge help! Thanks for taking the time to write such a detailed explanation. (=

1 Like

@jferris Iā€™m having issues with Docker and capybara-webkit. Didnā€™t find much help on google. Using the headless gem as suggested here didnā€™t help.

The error Iā€™m getting is:

Capybara::Webkit::ConnectionError:
       /usr/local/bundle/gems/capybara-webkit-1.6.0/bin/webkit_server failed to start.

My Dockerfile looks like:

FROM ruby:2.3.0
RUN apt-get update -qq \
  && apt-get install -y --no-install-recommends \
  dbus-1-dbg \
  libpq-dev \
  libqt5webkit5-dev \
  libqtwebkit-dev \
  nodejs \
  qt5-default \
  xvfb

RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/
COPY Gemfile.lock /app/
RUN bundle install
COPY . /app/

Any ideas?

Have you tried using xvfb-run? Iā€™ve had good luck with that. Looks like you already have xvfb installed, so you can do xvfb-run -a rake, for example. Check out the man page for more info.

If you run webkit_server directly, it may provide more information about what went wrong. You can use something like: docker-compose run web /usr/local/bundle/gems/capybara-webkit-1.6.0/bin/webkit_server.

Out of interest, does anyone using Rails with Docker also have a private gem server they need to install Gems from? If so how do you manage that in your Dockerfile? By default bundler stores the credentials for a private repo in ~/.bundler/config on the host machine. A Docker build cannot see that file by design, so I canā€™t copy it into a Docker image so that private gems will be resolved.