https://github.com/jay-johnson/docker-rails-app
Rails App - Phase 2 - With Docker Integration for Demonstrating Travis CI
https://github.com/jay-johnson/docker-rails-app
Last synced: 9 months ago
JSON representation
Rails App - Phase 2 - With Docker Integration for Demonstrating Travis CI
- Host: GitHub
- URL: https://github.com/jay-johnson/docker-rails-app
- Owner: jay-johnson
- Created: 2015-11-24T20:56:21.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-25T22:34:20.000Z (about 10 years ago)
- Last Synced: 2025-02-12T18:51:29.899Z (11 months ago)
- Language: Ruby
- Size: 64.5 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Local Rails Development running in Docker and integrated with Travis
[](https://travis-ci.org/jay-johnson/docker-rails-app.svg)
This repository is an example for showing how to setup a local development environment with docker hosting a rails application running inside a container image. It also uses Travis CI (https://travis-ci.org/jay-johnson/docker-rails-app) for Docker container building and then running the tests against the rails application from inside the container.
### Install
Make sure your environment has these components:
```
$ rails -v
Rails 4.2.4
$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
$
```
### How to build
```
docker build -t demo-rails .
```
### How to use the existing image
The latest Docker image is also stored publicly on Docker Hub: https://hub.docker.com/r/jayjohnson/rails-app/
Pull the latest with:
```
docker pull jayjohnson/rails-app
```
### How to run
```
docker-rails-app$ docker run -it -d --publish 0.0.0.0:3000:3000 --name=demo-rails demo-rails
ed72180c1c65fd4a2bd9be8f26437a5b19f04c1d7dba5715fde9661412092641
docker-rails-app$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed72180c1c65 demo-rails "/bin/sh -c /opt/acti" 6 seconds ago Up 4 seconds 0.0.0.0:3000->3000/tcp demo-rails
docker-rails-app$
```
### How to ssh into the container
```
docker exec -t -i demo-rails /bin/bash
```
### How to stop
```
docker-rails-app$ docker stop demo-rails
demo-rails
docker-rails-app$
```
### How to stop, remove, build, and start
```
$ docker stop demo-rails; docker rm demo-rails; docker build -t demo-rails .; docker run -it -d --publish 0.0.0.0:3000:3000 --name=demo-rails demo-rails
```
### Confirm the application is hosting the welcome page
```
docker-rails-app$ curl http://localhost:3000/welcome/index | grep Hello
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1429 100 1429 0 0 21544 0 --:--:-- --:--:-- --:--:-- 21651
Hello World!
docker-rails-app$
```
### Pulling the latest from inside a running container
```
root@b4f1ab122d82:/opt/webapp# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/jay-johnson/docker-rails-app
63aba06..ca307ad master -> origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded master to ca307ada38bdae1a07a96d808166dac7381da630.
root@b4f1ab122d82:/opt/webapp#
```
### View the App
Open a browser for:
```
http://localhost:3000/welcome/index
```
### Running the Hello World Rake Test:
```
bundle exec rake test TEST=test/controllers/hello_world_test.rb
```
### Setting up Travis CI with an after_success hook
1. Authentication
Travis needs to know how to authenticate with the repository account on Docker Hub. You can either set these as environment variables on the repository in Travis or encrypt them:
```
$ gem install travis
$ travis encrypt DOCKER_EMAIL=email@gmail.com
$ travis encrypt DOCKER_USERNAME=username
$ travis encrypt DOCKER_PASSWORD=password
```
1. Add the credentials to the .travis.yml file as
```
env:
global:
- secure: "UkF2CHX0lUZ...VI/LE=" # DOCKER_EMAIL
- secure: "Z3fdBNPt5hR...VI/LE=" # DOCKER_USERNAME
- secure: "F4XbD6WybHC...VI/LE=" # DOCKER_PASSWORD
- COMMIT=${TRAVIS_COMMIT::8}
```
1. Add the after_success to the .travis.yml
```
after_success:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
- export REPO=/
- export TAG=`if [ "$TRAVIS_BRANCH" == "master" ]; then echo "latest"; else echo $TRAVIS_BRANCH ; fi`
- docker build -f Dockerfile -t $REPO:$COMMIT .
- docker tag $REPO:$COMMIT $REPO:$TAG
- docker tag $REPO:$COMMIT $REPO:travis-$TRAVIS_BUILD_NUMBER
- docker push $REPO
```