Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up CD for a Ruby on Rails project with Bitbucket Pipelines and Docker

I'd love to set up continuous deployment in Bitbucket Pipelines for a Ruby on Rails / PostgreSQL / Sidekiq project, but I'm struggling to get my head around how it all fits together, and specifically how to get postgres working inside a Docker image. I'm very new to Docker and Pipelines.

In my Googling, Docker talks about using docker-compose to create a bundle, so I'd have a Postgres container and a Sideqik container, then link them with the app container. But I'm not sure what the difference is between a bundle and an image, and if Bitbucket Pipelines supports bundles. Eventually I want to set up deployments to a staging environment on Heroku, but for now just getting rspec spec to work in Pipelines would be nice.

Is there an existing public image that has Ruby + PostgreSQL already set up that I can use? If not, where do I start? My current Dockerfile looks like this:

FROM postgres:9.4
FROM ruby:2.3.1-onbuild
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs postgresql-client

I then run docker build . and docker run -it my-image /bin/bash and the following commands:

root@a84ad0e7c16b:/usr/src/app# postgres
bash: postgres: command not found
root@a84ad0e7c16b:/usr/src/app# psql 
psql: could not connect to server: No such file or directory
  Is the server running locally and accepting
  connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
like image 802
Benjamin Humphrey Avatar asked Aug 21 '16 13:08

Benjamin Humphrey


People also ask

How do I create a docker image in Bitbucket pipeline?

Enable Docker BuildKit. To use Docker BuildKit in a Bitbucket Pipeline, set the DOCKER_BUILDKIT=1 environment variable in the pipeline configuration (bitbucket-pipelines. yml). For information on Docker BuildKit, visit: Docker Docs — Build images with BuildKit.

Does Bitbucket allow automation of CI CD pipeline?

Bitbucket Pipelines is an integrated CI/CD service built into Bitbucket. It allows you to automatically build, test, and even deploy your code based on a configuration file in your repository.


1 Answers

Taking the advice from https://bitbucket.org/spittet/ruby-postgresql you could easily setup your bitbucket-pipelines.yml like this:

image: spittet/ruby-postgresql

pipelines:
  default:
    - step:
        script:
          - bundle install
          - /etc/init.d/postgresql start
          - sudo -u postgres sh -c 'createuser root --createdb'
          - rails db:setup RAILS_ENV=test
          - rspec

As you may see I needed to create a user with permissions for creating databases.

For debugging you could try locally first:

run -i -t -v <local_directory_of_your_rails_app>:<directory_on_docker> spittet/ruby-postgresql /bin/bash
cd <directory_on_docker>
bundle install...
like image 61
Mario Pérez Avatar answered Oct 14 '22 07:10

Mario Pérez