Category Archives: Docker

Template Rails app in Docker

If you are a developer it is usually very helpful to be able to keep portable templates to test code snippets or build apps more quickly. When using Ruby on Rails, of course you can always use the rails generators as a very powerful aid. But if you find yourself constantly adding the same gems, similar scaffolding, or other components, it’s best to save your work as a template and use Docker. The main reason for using Docker is to help in deploying to different environments without changing any code.

After you have started your app using the rails new command and added the gems you usually use, it’s a good idea to save this as a template. The next step should be to create a Dockerfile inside your project.

Here you can see a good example of a very basic template.

The Dockerfile is important because it makes the code portable. Notice that it defines the version of Ruby and also installs all the dependencies that are necessary. Simply issue the docker build command once and then the docker run command every time you wish to have the template at your disposal. To connect to it:

docker ps # grab the container id value

docker exec -it <container_id> bash