Using Docker for local development
As we've discussed, microservices solve a particular set of problems but introduce some new challenges of their own. One challenge that engineers on your team will probably run into is doing local development. With a monolith, there are fewer moving parts that have to be managed—usually, you can get away with just running a database and an application server on your workstation to get work done. As you start to create new microservices, however, the situation gets more complicated.
Containers are a great way to manage this complexity. Docker is a popular, open source software containerization platform. Docker allows you to specify how to run your application as a container—a lightweight standardized unit for deployment. There are plenty of books and online documentation about Docker, so we won't go into too much detail here, just know that a container encapsulates all of the information needed to run your application. As mentioned, a monolith application will often require an application server and a database server at a minimum—these will each run in their own container.
Docker Compose is a tool for running multicontainer applications. Compose allows you to define your applications containers in a YAML configuration file. Using the information in this file, you can then build and run your application. Compose will manage all of the various services defined in the configuration file in separate containers, allowing you to run a complex system on your workstation for local development.
Getting ready
Before you can follow the steps in this recipe, you'll need to install the required software:
- Install Docker. Download the installation package from the Docker website (https://www.docker.com/docker-mac) and follow the instructions.
- Install
docker-compose
by executing the following command line on macOS X:
brew install docker-compose
On Ubuntu Linux, you can execute the following command line:
apt-get install docker-compose
With those two packages installed, you'll be ready to follow the steps in this recipe.
How to do it...
- In the root directory of your Rails application, create a single file called
Dockerfile
with the following contents:
FROM ruby:2.3.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /pichat WORKDIR /pichat ADD Gemfile /pichat/Gemfile ADD Gemfile.lock /pichat/Gemfile.lock RUN bundle install ADD . /pichat
- Create a file called
docker-compose.yml
with the following contents:
version: '3' services: db: image: mysql:5.6.34 ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root app: build: . environment: RAILS_ENV: development command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/pichat ports: - "3000:3000" depends_on: - db
- Start your application by running the
docker-compose up app
command. You should be able to access your monolith by enteringhttp://localhost:3000/
in your browser. You can use this approach for new services that you write.