Simplify your life using docker for development and deployment
- Docker 1.8.3
- Centurion https://github.com/newrelic/centurion
- 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
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"]
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.
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.ymlin your root directory
- copy the following contents
db: image: postgres ports: `- "5432"` web: build: . volumes: `- .:/myapp` ports: `- "3000:3000"` links: `- db`
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 bashthat will run the command bash in the docker box and
-itallows 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>
docker commit web mattpatterson/copirite
- to push the changes to the dockerhub, run
docker push <docker_url>
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.
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.
Before centurion will work, it needs to be configured.
centurionize -p <your_project>to set up the centurion configuration
- open the new
<your_project>.rakefile that was created
- set your image:
set :image, 'mattpatterson/copirite'
- set your host server ip:
task :common do set :image, 'mattpatterson/copirite' host '188.8.131.52' end
- set your image:
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'
task :production => :common do set_current_environment(:production) env_vars RAILS_ENV: 'production' host_port 80, container_port: 3000 set :container_hostname, 'web' end
- set your env_vars:
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 &
bundle exec centurion -p <project_name> -e production -a deploy
-e productionensures it uses the production environment (set in the centurion config)
-a deployensures 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.