DEV Community

SINAPTIA
SINAPTIA

Posted on • Originally published at sinaptia.dev

Docker in development: Episode 3

The last episode was about dockerizing our Ruby on Rails app. In this episode, we’ll be exploring how to perform everyday tasks in our containers.

Running rake tasks and rails commands
Running rake tasks is easy. Once you’ve built your images, you can use docker-compose to run commands in them. For example, if you want to see the routes of your app:

$ docker-compose run web rails routes
Similarly, if you want to create the db, migrate, and seed:

$ docker-compose run web rails db:create db:migrate db:seed
If, instead, you want to run your test suite, you’ll have to create the test db:

$ docker-compose run web rails db:create db:migrate RAILS_ENV=test
and then run the test suite (assuming rake defaults to rake test):

$ docker-compose run web rake
Tip: create your custom scripts/aliases
I’ve run docker-compose run web rails ... a hundred times a day, so to make things easier I put this script in my $PATH:

!/bin/bash

docker-compose run web rails "$@"
Note that this script assumes your docker-compose.yml file has a web service. If you don’t, it won’t work.

Performing other tasks
So far, all commands are quite straightforward, you just run them within the web service. What happens with somewhat harder tasks such as loading a pre-existing database into your container’s database? This is one of the tasks that took me longer to figure out.

In postgres, there are 2 ways of doing this and it depends on the dump’s format. In the past we had to deal with --format=c dumps and regular dumps.

Assuming you have a latest.dump file that contains a postgres dump in c format, and you want to load it in your (running) container, you first have to figure out what your container id is. You can do that by running:

$ docker container ls
or

$ docker ps
Once you have the container id (in this example we’ll use 80f8041db4b4), you can run this command to restore the dump in the container:

$ docker exec -i 80f8041db4b4 pg_restore -d app_development -U postgres < latest.dump
If instead, you have a regular dump (eg. latest.sql), you can run this command to restore it:

$ docker exec -i 80f8041db4b4 psql -d app_development -U postgres < latest.sql
If you’re using docker-compose, things get easier:

$ docker-compose exec -T db pg_restore -d app_development -U postgres < latest.dump
Dockerize everything!
I use elastic beanstalk a lot. I usually install it with Homebrew, but it installs a bunch of dependencies of its own, like Python, SQLite, etc. I don’t want all of that in my system, especially because I always have trouble with Python versions. Instead, I dockerized it: docker-awsebcli.

Join us in the next episode!

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay