This api application visualizes the national budget of the Republic of Georgia.
- Setup .env file
cp .env.example .env
- Add Secrets (use
rake secret
to generate values) - Use
postgres
as the value forDB_USER
andTEST_DB_USER
- Set database names for
DB_NAME
andTEST_DB_NAME
, such as 'budget_dev' and 'budget_test' - Install docker and docker-compose
docker-compose build
(takes a while) TODO ubuntu related issuedocker-compose up
docker-compose run api rake db:create db:migrate db:seed
- Add budget data to database. Two options:
- Restore the dev database from a db dump:
1.
docker-compose ps
---> get the name of thedb
container 1.docker cp /path/to/db/file db_container_name:/tmp/backup.sql
1.docker-compose exec db pg_restore --clean --no-owner -d "dev_db_name" -U "postgres" /tmp/backup.sql
- If you don't have a dump file to restore from, you can run the budget uploader (consult separate section below).
- Go to localhost:3000 or start using the API :)
- Sync the local budget files repo with master:
docker-compose run api rake budget_data:sync_with_repo
- Run the uploader (takes a long time):
docker-compose run api rake budget_data:upload
The api container is not configured by default to work for deploying, so you will have to do a little bit of configuration in order to do so. This section is intended to make that configuration easier.
Ideally, this is only a temporary solution until we figure out how to deploy with docker.
NOTE: The below commands will only work if the container you are setting up to deploy from is named georgianbudgetapi_api_1
. If it has a different name, then use it below in the docker cp
commands.
- Copy your global gitignore into the
api
container (called from host):
docker cp ~/.gitignore_global georgianbudgetapi_api_1:/root/
(on ubuntu .gitignore_global step is skipped)
2. Run these commands from within the api
container(called from api container):
git config --global core.excludesfile /root/.gitignore_global
bundle_path=$(which bundle)
sed -i -e "s/activate_bin_path/bin_path/g" $bundle_path
mkdir -p /root/.ssh
apt-get -y install rsync
In order: configure git to use .gitignore_global; fix bundle issue described here; create .ssh directory if it doesn't exist; install rsync, which is a dependency of the deploy process.
- Make sure your local user has an ssh key at ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. (called from host)
docker cp ~/.ssh/id_rsa georgianbudgetapi_api_1:/root/.ssh/
docker cp ~/.ssh/id_rsa.pub georgianbudgetapi_api_1:/root/.ssh/
- Add your ssh key to the container's ssh-agent so that it doesn't ask for your passphrase when you deploy. You will have to enter your ssh key's passphrase. (called from api container)
eval "$(ssh-agent -s)"
eval ssh-agent
ssh-add ~/.ssh/id_rsa
- Continue with deploy as usual
- Find the database container name:
docker-compose ps
- Log into the db container:
docker exec -i -t {container-id} /bin/bash
- Dump the database:
pg_dump -Fc -U postgres -O budget_staging_dev > tmp/budget_dev.sql
- Exit the db container:
exit
- Copy out of db container:
docker cp {container-id}:tmp/budget_dev.sql tmp/
- Copy to server:
scp tmp/budget_dev.sql {user_name}@{server_name}:tmp/
- Restore:
pg_restore --clean --no-owner -d "budget-staging" -U "budget-staging" tmp/budget_dev.sql
This is a cheat sheet for JumpStart's Rails docker projects. If you want to truly understand what's going on here, consult the Docker documentation and work through their tutorials.
docker-compose build
docker-compose up
If you're missing a gem, you don't want to rebuild the docker api image — that takes too long, as it forces docker to install all the gems. Instead:
docker-compose run api bundle install
Or to update:
docker-compose run api bundle update
If you find yourself typing a lot of commands prefixed by docker-compose run api
, you can open a bash session in the api
container and just type them there.
docker-compose run api rake db:create
docker-compose run api rake db:migrate
docker-compose run api rake db:seed
docker-compose run api rspec
docker-compose run api rails g migration AddColumnDockerKnowledgeToBrain
Can be run like this:
docker-compose run api bash
# then, in bash
rake db:create
rake db:migrate
rake db:seed
rspec
rails g migration AddColumnDockerKnowledgeToBrain
# and when you're done
exit
docker-compose ps
If you want to view the output or interact with a running container, you can attach to it like below. (This is helpful if you use binding.pry to debug the server.)
docker-compose ps
# find the name of the container you want to attach to
# below we will attach to georgianbudgetapi_api_1
docker attach georgianbudgetapi_api_1
You'll be typing docker
and docker-compose
a lot, so I recommend creating bash aliases for these commands. For example, you can add the following lines to your ~/.bash_profile (or ~/.bashrc):
alias do='docker'
alias doco='docker-compose'
Then, you can substitute doco
for docker-compose
and do
for docker
in all of these commands.