![]() # Replace this with your Ruby version and Linux flavor/version FROM ruby:2.6.6-stretch # Package dependencies RUN apt-get update & apt-get install -y curl build-essential gnupg mysql-client # Replace with newer bundler version if using RUN gem install bundler -no-document -v '1.17.3' # Replace this with your Node version if newer than 10.x, this is for client side tooling. This provides an early warning in case of environment issues such as something that works on a laptop (eg: Mac or Windows) but fails or behaves differently in production (eg: Linux). It's a good idea to have the base image be the same as the projects' production environment. This particular project uses Ruby version 2.6.6 and is deployed on Debian Stretch. Start by creating a Dockerfile at the root of the project as follows. Run Rails with changes on host machine reflected in container In order to proceed with the next steps in this post, make sure Docker is installed, this installation also comes with docker-compose. Here is a visual representation of what we will be creating: For example, it would not make sense for the container that seeds the database to start running before the container that houses the MySQL database is available.ĭocker Compose is a great tool to coordinate and synchronize multiple containers for this purpose. However, these containers will need to communicate with each other, and be coordinated such that they run at the appropriate time. ![]() Looking at what's needed to run a Rails app (server, webpacker, MySQL, Redis, etc), it becomes clear that each of these is a separate concern, therefore should be run in a separate container. For instance, a web application stack might consist of three separate containers, each with its own unique image, to manage the web application, database, and an in-memory cache in a decoupled manner. Decoupling applications into multiple containers makes it easier to scale horizontally and reuse containers. Introducing docker-composeĮach container should have only one concern. For those unfamiliar or new to Docker, I recommend the Docker Deep Dive course on Pluralsight. ![]() A quick note first - this post assumes knowledge of Docker concepts such as Images, Containers and Volumes. This post will walk through the setup, to achieve each of the above workflows steps. Being able to access an interactive Rails console.Running one off commands such as applying database migrations or running tests.Debugging with either a command line debugger ( binding.pry) or an IDE such as VS Code.Running a background job processor (this project uses Active Job, but Sidekiq could be used instead).Running a Webpack dev server so that changes in client side code made on host machine are automatically compiled in container and browser is automatically refreshed to reflect these changes.Running one-time setup tasks such as applying the schema to database and populating it with db/seeds.rb.Running the Rails app with changes to server side code made on host machine automatically reflected in container.Development WorkflowĪlthough there are many benefits to using Docker for development, there are also some challenges such as how to run tests? how to debug? In order for the Docker setup to be effective, it needs to cover the entire development workflow, including: This was a hedge in case something was found that could not be made to work with Docker. all dependencies installed directly on laptop. It was also important to ensure the project could still be run the "old fashioned" way, i.e. The goal of packaging everything with Docker was to make it easier and faster for new developers to get up and running. ![]() The existing setup involved developers installing all dependencies on their laptops and took some non-trivial amount of time to get working. This app has a React front end built with Webpacker, client side dependencies managed with Yarn, runs background jobs with Active Job, and uses a MySQL database and Redis. At work, I was recently tasked with Dockerizing a Rails monolith. ![]()
0 Comments
Leave a Reply. |