I am learning Docker and have a little confusion that I would greatly appreciate some advice on.
When creating a new rails application. Following the guidelines of Docker's tutorial Quickstart: Compose & Rails. They're steps are as follows.
- Create Dockerfile & Entrypoint
- Create Gemfile with source and rails gem listed.
- Create an empty Gemfile.lock
- Create a docker-compose.yml and name services.
- Generate a new rails app using docker-compose run.
The rails new command.
docker-compose run --no-deps web rails new . --force -d mysql
This is my simple Dockerfile:
FROM ruby:3.1.1-alpine3.15
RUN apk update -q
RUN apk add bash yarn git build-base nodejs mariadb-dev tzdata
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
RUN gem install bundler
RUN gem update bundler
RUN bundle install
COPY entrypoint.sh /usr/bin
RUN chmod x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
This is my docker-compose.yml:
version: "3.9"
services:
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
As you can see I only have one service listed in my docker-compose.yml, because I do not want any other services or containers.
Everything works fine when I use the command docker-compose run rails new ... it generates a new rails app and persists the files to my local source folder. So I can open and edit the files to develop the application. Perfect.
But when I try and generate a new Rails app without using docker-compose.yml and only the Dockerfile. The generated files are not persisted in my local source folder, only on the container. I can only assume because I am forced to build the image before running it in a container to generate the new rails app.
When using without docker-compose.yml. I have to build the image first.
docker build -t image_name .
Then I can run the container and generate the rails app.
docker run image_name rails new . --force -d mysql
Obviously, this doesn't persit the new files in my local source folder.
How can I generate a new Rails app with just my Dockerfile without docker compose and still have the newly generated files persist in my local source folder?
I must be missing something, but I've done a lot of research and can't find an answer.
CodePudding user response:
In docker-compose.yml you have a 'volumes' key as seen below.
volumes:
- .:/app
But when using it in the CLI you miss passing these volumes.
You can pass these volumes as in the code below.
docker run -d --name=rails --volume /your/workdir:/app image_name
If you want to learn more about volumes, you can find out here.