Docker with Ruby on Rails on a development environment

Viewed 125

I'm learning Docker and I'm trying to configure a Ruby on Rails project to run on it (on development environment). But I'm having some trouble.

I managed to configure docker-compose to start a container with the terminal open, so I can do bundle install, start a server or use rails generators. However, every time I run the command to start, it starts a new container, where I have to do bundle install again (it takes a while).

So I'd like to know if there is a way to reuse components already created.

Here is my Dockerfile.dev

FROM ruby:2.7.4-bullseye

WORKDIR '/apps/gaia_api'

EXPOSE 3000

RUN gem install rails bundler

CMD ["/bin/bash"]

And here is my docker-compose file:

version: "3.8"
services:
  gaia_api:
    build:
      dockerfile: Dockerfile.dev
      context: "."
    volumes:
      - .:/apps/gaia_api
    environment:
      - USER_DB_RAILS
      - PASSWORD_DB_RAILS
    ports:
      - "3000:3000"

The command I'm using to run is: docker-compose run --service-ports gaia_api.

I tried to use the docker commands build, create and start, however the volume mapping doesn't work. On the terminal of the container, the files of the volume are not there.

The commands I tried.

docker build -t gaia -f Dockerfile.dev .
docker create -v ${pwd}:/apps/gaia_api -it -p 3000:3000 gaia
docker start -i f36d4d9044b08e42b2b9ec1b02b03b86b3ae7da243f5268db2180f3194823e48

There is probably something I still don't understand. So I ask: Whats the best way to configure docker for ruby on rails development? And will it be possible to add new services later (I plan once I get the first part to work, to add postgres and a vue project).

EDIT: Forgot to say that I'm on Mac OS Big Sur

EDIT 2: I found what was wrong with the volumes, I was tying -v ${pwd}:/apps instead of $(pwd):/apps.

0 Answers
Related