Extend reading of: Using Docker to create & start Rails development

Rick WANG
2 min readApr 13, 2018

--

Previous reading link

What happend if we want to use one container for both create and start dev server?

Here is the source code at my github

Idea:

Based on the basic Dockerfile.template, use shell to do some runtime replacement and make it much easier to use.

It is more about shell scripting.

Workflow

  1. create starting image still same as before.
docker build -f Dockerfile.template -t day4_rails-docker-for-rails-all-in-one .

Dockerfile.template contains very minimal steps to get rails environment.

2. sh run.sh

let’s have quick look what it is doing.

sh run.sh init my_app1

it will actually exec:

docker run -it — rm — user “$(id -u):$(id -g)” -v “$PWD”:/rails_app_dir -w /rails_app_dir day4_rails-docker-for-rails-all-in-one rails new “$2” — skip-bundle

pretty much same as before, use arg from shell for project name. Once its complete, it will generate my_app1 folder.

Then we can do

sh run.sh start my_app1

it will actually exec

echo “ — — now starting dev server for: $2 — -” 
cat dockerfile.template > $docker_yml_cache_file_name
echo “COPY $2/Gemfile Gemfile” >> $docker_yml_cache_file_name
echo “RUN bundle install” >> $docker_yml_cache_file_name
sed “s/#app_dir_name#/$2/g” docker-compose.template >> $docker_compose_cache_file_name

Looks a lot, but eventually just some string append and replacement. It add extra job for Docker.

  1. COPY Gemfile into container.
  2. RUN bundle install

Note: “$2/Gemfile” will be interpreted to “my_app1/Gemfile”

sed “s/#app_dir_name#/$2/g” docker-compose.template

sed will do string replace and generate docker-compose.yml file. In our example, it will replace ‘app_dir_name’ with ‘my_app1’.

2 config files for docker to use: Dockerfile_for_yml.cache and docker-compose.yml

And finally

docker-compose up — build

Then done.

--

--