Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dockerflow

Rails Dockerflow

Simplify your life using docker for development and deployment


Resources

Installation

  • Ensure your machine has docker installed (if you're on a mac, check out Docker toolbox)
  • Ensure you have a server with docker installed
  • Have a rails project up and running, with centurion in the bundle list (gem 'centurion') within the development group
Dockerfile

This is an example Dockerfile for a simple rails project and setup. Ensure you have foreman and a procfile, otherwise replace that line with bundle exec rails s

FROM ruby:2.2.3
RUN apt-get update -qq && apt-get install -y build-essential vim

WORKDIR /tmp
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install

ADD . /myapp
WORKDIR /myapp
CMD ["foreman","start", "-p", "3000"]
Database configuration

In this guide (subject to change) the database is set up using postgres and not done through centurion. But prior to deploying the application.

In order to start a postgres database server via docker, use the following commands docker pull postgres docker run --name db -d postgres

This will download the latest version of the postgres image on the dockerhub. It will then run it in a container named db which is used to link to.

Sample database configuration for connecting to a postgres docker image.

Note the host is the name of the database docker container running

default: &default
  adapter: postgresql
  encoding: unicode
  username: postgres
  host: db
  pool: 5

production:
  <<: *default
  database: copirite_production
Local development setup (mac)

This guide uses docker-compose which is only available on mac/windows.

  • create a file called docker-composer.yml in your root directory
  • copy the following contents
      db: 
    		image: postgres
      	ports:
          	`- "5432"`
      web:
    		build: .
      	volumes:
      		`- .:/myapp`
      ports:
      	`- "3000:3000"`
      links:
          `- db`
  • run docker-compose up. This should build the docker box and necessary dependencies (in this case, the database box)
  • specifying the volume means that your files directly link to the files in the docker container
  • to enter the docker container, run the following command docker exec -it web bash that will run the command bash in the docker box and -it allows you to interact with the session-
  • once inside the docker container, you can run all the necessary commands in order to get the website up and running (rake commands etc).
Committing to the docker container
  • if you make changes to the docker box, run docker commit web <docker_url>
  • eg. docker commit web mattpatterson/copirite
  • to push the changes to the dockerhub, run docker push <docker_url>
  • eg. docker push mattpatterson/copirite
  • NOTE. If this is your first push to the dockerhub, it can be much quicker to do in on the production server. If this is possible for you, clone down the application to the production server, build a copy of the image and then push to the dockerhub.
Using centurion

Now that you have an idea of how to set up a local environment for your project, it is now time to move forward to the production side of things.

Centurion Configuration

Before centurion will work, it needs to be configured.

  • run centurionize -p <your_project> to set up the centurion configuration
  • open the new <your_project>.rake file that was created
    • within task :common
      • set your image: set :image, 'mattpatterson/copirite'
      • set your host server ip: host '104.236.156.32'
      • example:
      task :common do
        	set :image, 'mattpatterson/copirite'
        	host '104.236.156.32'
        end
    • within task :production => :common
      • set your env_vars: env_vars RAILS_ENV: 'production'
      • set your host port and container port: host_port 80, container_port: 3000
      • set your container hostname: set :container_hostname, 'web'
      • example:
      task :production => :common do
        	set_current_environment(:production)
        	env_vars RAILS_ENV: 'production'
            host_port 80, container_port: 3000
        	set :container_hostname, 'web'
        end
Deploying

Now that centurion has been configured, it is time to run it.

Ensure that the docker daemon is running on the production server and is open externally. this can be done by ssh'ing into the server and running the following:

docker daemon -H 0.0.0.0:2375 & 
  • run bundle exec centurion -p <project_name> -e production -a deploy
  • -e production ensures it uses the production environment (set in the centurion config)
  • -a deploy ensures it runs the deploy action.
  • If everything goes well, you should be able to access the website via the ip or domain name associated in the centurion configuration.
@mattpatterson94

This comment has been minimized.

Copy link
Owner Author

@mattpatterson94 mattpatterson94 commented Dec 1, 2015

Current issues

  • Handle builds via github on push
  • - tlsverify on docker daemon
  • Handling secure files
  • Handling assets with nginx
  • Link docker database to volume
  • Possible handling of project set up (db:setup etc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment