I have stumbled upon a new tools called Fig. This is written by Docker to utilise container technology for isolating development environment (so you no longer need to install postgresql, nginx, etc on your own OS anymore).
Although the tutorial for developing Rails in Fig website is easy to follow for beginner, it has some big caveats that are not suited for development:
- Each time you add a new gem, you have to build the image all over again, which is very time consuming.
- If you run fig
run rails generate modelor something like that, the generated files are owned by root, which makes you have to run chown after that.
Using that tutorial as a base, I have create my own Dockerfile and fig.yml to solve those two problems.
db: image: postgres ports: - "5432" data: build: backend command: bash web: build: backend command: bundle exec rails server -p 3000 volumes_from: - data volumes: - .:/home/deployer/app ports: - "3000:3000" links: - db
FROM ruby:2.2 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN apt-get -qqy install sudo # Create local user for development, remember to replace `1000` to your user's uid RUN adduser --disabled-password --gecos '' --uid 1000 deployer RUN adduser deployer sudo RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers USER deployer # Create local bundle installation RUN mkdir /home/deployer/.bundle ENV GEM_HOME /home/deployer/.bundle ENV BUNDLE_APP_CONFIG /home/deployer/.bundle ENV PATH /home/deployer/.bundle/bin:$PATH # Using a separate data volume to persist this on different `fig run` VOLUME /home/deployer/.bundle # Copy gem bundler to local installation RUN echo "cp -r /usr/local/bundle/* /home/deployer/.bundle" >> /home/deployer/.bashrc # Linking volumes makes `.bundle` owned by root so we need to fix this before each run command RUN echo "sudo chown -R deployer:deployer /home/deployer/.bundle" >> /home/deployer/.bashrc # Create a folder for syncing the app RUN mkdir /home/deployer/app WORKDIR /home/deployer/app
Now each time you want to run
bundle install again, just execute this command:
fig run --rm web bundle install
Problems now solved:
- Bundled gems are persisted between each run by using a separate container.
- All commands are runned under
deployeruser, which has the same uid as your host user.