This repo is focused on showing you how to setup a basic Rails application and configure Docker on top of it.
To get started, you'll need to have Rails installed locally on your machine. If you don't have Rails, you can download it by running bundle install rails
in your terminal.
To get started with this particular project, you can run the following commands to get started:
bundle
rake db:create; rake db:migrate; rake db:seed
rails s
Once you start your server, head over to localhost:3000/api/v1/users
to see if anything populates from running your seed file. Use control + c
to stop the server.
NOTE: This is not typically part of the workflow; this goal of this 'Setup' exercise was simply to let you see what the server/endpoints look like when running locally.
You can find an easy way to download Docker right here
Once you are all set up, head over to your terminal and type in docker
to see if Docker was installed correctly. You should see a large menu populate.
The Dockerfile is necessary to build an image. This file lets Docker know what dependencies the project needs - the image will have all of those dependencies.
Touch a new file at the root of the project directory called Dockerfile
(no extension).
The contents of the file should be:
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
This file is also necessary! It's job is to describe the services of you database and web app, how to get the Docker image of each one, and the configuration needed to get the web app and database app to talk to each other.
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Run docker-compose up
in your terminal. Be prepared to wait for a few minutes...
In another tab of your terminal, run:
docker-compose run web rake db:create
docker-compose run web rake db:migrate
docker-compose run web rake db:seed
Go to http://localhost:3000/api/v1/users
- you should see the same thing you saw when running locally!
There are some commands that you'll need to reuse over and over again. Some of the most important ones are the following:
docker-compose build
(builds an image)docker-compose up
(builds a container - if you don't runbuild
, this command will run both for you!)docker-compose down
(stops the container)docker images
(lists the running images)docker ps
(list the running containers)
You'll also need to make sure you are cleaning up after yourself when playing around with images and containers. If you don't you may see some unwanted side effects and your code being stale. It's a good habit to run these commands if you forcefully quit your docker containers.
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
docker rm $(docker ps -qa --no-trunc --filter "status=exited")