Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Docker for your Rails Development Environment

Using Docker for your Rails Development Environment


Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. While there's a lot of information about using Docker for production Rails apps, I have been using it in development as well. Docker brings a lot of advantages for web application development, but it also creates some problems.

In this talk, I am going to share the benefits of using Docker, how to create a development environment with it, and how I have dealt with some of the challenges I've encountered.

Hibariya Hi

April 03, 2019

More Decks by Hibariya Hi

Other Decks in Technology


  1. Hi there • Gentaro Terada (@hibariya) • Works at ESM,

    Inc. • A college student (engineering) • Likes Ruby, Internet, and Programming • https://hibariya.org
  2. What is Docker With Docker, we can: • Create/run containers

    • Create/share container images (kind of snapshots)
  3. Why To Use Docker Docker makes it much easier: •

    Build once, run anywhere • Less “It works on my machine” ◦ The same condition (Runtimes, Libraries, Server Applications, and so on) • Re-creating environments easily/quickly • Various environment can live together on the same host machine
  4. The First Step A development environment for a small Rails

    application: 1. Create a Dockerfile to build containers 2. Create a docker-compose.yml to organize containers
  5. Creating a new Rails app On the host: $ docker

    build -f Dockerfile -t rails-docker-example . $ docker container run -it --rm \ --volume=$(pwd):/work --workdir=/work \ rails-docker-example bash On the container: # gem install rails --no-document # rails new example --database=postgresql --skip-bundle …
  6. Running a command inside a container Use a running container:

    $ docker-compose exec rails bin/rails c Create and run a new container: $ docker-compose run --rm rails bin/rails c
  7. For more details https://github.com/esminc/rails-docker-examples • Example Rails app + PostgreSQL

    + Webpacker • Configurations for Docker Compose • Configurations for Kubernetes (w/ GKE)
  8. Problems I Faced • Testing with browsers • Dockerfile doesn’t

    ensure the same image • Too large images • Generated files are owned by root • Long / complicated commands • Could not we use K8s for development?
  9. How to run browser testing? • Plan A: Run a

    Selenium container and use it remotely (I recommend) ◦ Easy debugging; Can view/inspect running browser via VNC ◦ Does not add another dependency to the Rails container • Plan B: Install a browser on the Rails container and use it in headless mode ◦ Less custom settings (probably) ◦ Needs browser installation on the Rails container
  10. Dockerfile doesn't ensure the same image • Updates on base

    image • Repository changes for package management system ◦ e.g., EPEL-5 repository EOL (March 2017) ◦ So the following Dockerfile does not work anymore
  11. Rails application images tend to be large We could: •

    Use smaller base image • Install only mandatory packages • Remove caches at the same time • Use multi-stage builds for assets compilation • Avoid using Git repositories as gem sources ◦ as long as you can
  12. The More You Layer, the More It Gets Bigger Deleting

    existing files does not decrease the whole size Exploring the layers of an image with github.com/wagoodman/dive
  13. Drop build-time deps w/ multi-stage build Node.js, NPM and node_modules

    can be removed in the same way https://docs.docker.com/develop/develop-images/multistage-build/
  14. Plan A: Add a user Add a user with the

    same UID as the user on the host
  15. A kind of typing game? More than twice as long

    as the original command! $ docker-compose exec rails bin/rails g model User email:string $ docker-compose exec rails bin/rails db:migrate $ docker-compose exec rails bin/rails c
  16. Exec vs. Run; Which should I use? It depends... •

    docker-compose exec ◦ Uses an already running container ◦ Can’t publish additional ports • docker-compose run ◦ Creates and uses a dedicated container ◦ Without `--use-aliases`, couldn’t resolve some of the host names in some cases ◦ Without `--rm`, the created container will remains after exit
  17. Crane Cosy interface to develop • Reads docker-compose.yml • Grouping

    services • There’s only `run` ◦ Works nicely by the situation
  18. Is it possible to develop apps w/ K8s? Yup. •

    Good news: We can use the same tool for development • Bad news: We will have to manage a lot of YAML files ◦ Because of the difference of the API ◦ Because K8s projects tend to be large ◦ For not only production, but also development
  19. Example Repository https://github.com/esminc/rails-docker-examples • Example Rails app + PostgreSQL +

    Webpacker • Configurations for Docker Compose • Configurations for Kubernetes (w/ GKE)