Roman Pushkin
Roman Pushkin

Reputation: 6079

Rails 5+, WebPacker and Docker development workflow

One of the advantages of using Docker is single environment for entire team. Some time ago I was using Vagrant to unify development environment in a team, and it worked pretty well. Our workflow was like:

Some folks use Docker for the similar development workflow, but I usually use docker-compose just to run satellite services. And I was always running Rails monolith inside of host operating system, like natively.

So my development workflow is pretty standard:

With Vagrant the above scheme works well as well. But with Docker things are different.

The development workflow some folks use with Docker is as follows:

In other words, with Docker-only approach we have the following cons:

So my question is: what's the best way to go with Docker-only approach? How you can take advantage of Docker while using WebPacker with Rails and avoid page refresh and application restart?

Upvotes: 2

Views: 645

Answers (1)

David Basalla
David Basalla

Reputation: 3106

I've been reading a good book on this recently (Docker for Rails developers). The gist seems to be that you run Rails in a Docker container and use a volume to 'link' your local files into the container, so that any file changes will take immediate effect. With that, you should not need to restart/rebuild the container. On top of that, you should run webpack-dev-server as a separate container (which also needs the local files mounted as a volume) which will do JavaScript hot reloading - so no need to reload the page to see JS updates.

Your docker-compose.yml file would look something like this (uses Redis and Postgres as well):

version: '3'

services:
  web:
    build: .
    ports: 
      - "3000:3000"
    volumes: 
      - .:/usr/src/app
    env_file:
      - .env/development/web
      - .env/development/database
    environment:
      - WEBPACKER_DEV_SERVER_HOST=webpack_dev_server
  webpack_dev_server:
    build: .
    command: ./bin/webpack-dev-server
    ports:
      - 3035:3035
    volumes:
      - .:/usr/src/app
    env_file:
      - .env/development/web
      - .env/development/database
    environment:
      - WEBPACKER_DEV_SERVER_HOST=0.0.0.0
  redis:
    image: redis
  database:
    image: postgres
    env_file:
      - .env/development/database
    volumes:
      - db_data:/var/lib/postgresql/data
volumes:
  db_data:

Upvotes: 2

Related Questions