May 22, 2021 Docker From entry to practice
We will now use Fig to configure and run a Rails/PostgreSQL app. Before you begin, make sure that Fig is already installed.
Three necessary documents need to be set up before all work can begin.
First, because the app is going to run in a Docker container that meets all the environmental dependencies, we can specify what the Docker container will install by editing the
Dockerfile
file. H
ere's what it's all about:
FROM ruby
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
RUN bundle install
ADD . /myapp
The above specifies that the app will use a mirror with Ruby, Bundler, and its dependents installed. F
or more information on how to write a Dockerfile file, you can
view Mirror Creation
and
Dockerfile for use.
N
ext, we need a boot-loaded Rails file
Gemfile
After a while it will be
rails new
the rails new command.
source 'https://rubygems.org'
gem 'rails', '4.0.2'
Finally,
fig.yml
is the most magical place.
fig.yml
file associates everything.
It describes the composition of the app (a web service and a database), the source of each mirror (the database runs using a predefined PostgreSQL image, the web app side is created from the local directory), the connection between the images, and the ports open to the service.
db:
image: postgres
ports:
- "5432"
web:
build: .
command: bundle exec rackup -p 3000
volumes:
- .:/myapp
ports:
- "3000:3000"
links:
- db
When all the files are ready, we're ready to build the skeleton of the app by using the
fig run
command.
$ fig run web rails new . --force --database=postgresql --skip-bundle
Fig first uses
Dockerfile
a mirror for the web service, and then uses the image
rails new
subsequent commands in the container.
Once this command is run, you should be able to see that a brand new app has been generated.
$ ls
Dockerfile app fig.yml tmp
Gemfile bin lib vendor
Gemfile.lock config log
README.rdoc config.ru public
Rakefile db test
In
Gemfile
file, we remove the comment that loads the line
therubyracer
so that we can run the environment using Javascript:
gem 'therubyracer', platforms: :ruby
Now that we have a new
Gemfile
we need to re-create the image.
(This step changes the Dockerfile file itself and only needs to be rebuilt once.)
$ fig build
The app is ready to start now, but the configuration is not yet complete. T
he database target that Rails reads by default
localhost
and we need to manually specify the db of
db
S
imilarly, the user name needs to be modified to match the postgres image intended. O
pen the newly generated
database.yml
file.
Replace with the following:
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
host: db
test:
<<: *default
database: myapp_test
You're ready to launch your app now.
$ fig up
If all goes well, you should be able to see the output of PostgreSQL, and after a few seconds you can see this repeated message:
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick 1.3.1
myapp_web_1 | [2014-01-17 17:16:29] INFO ruby 2.0.0 (2013-11-22) [x86_64-linux-gnu]
myapp_web_1 | [2014-01-17 17:16:29] INFO WEBrick::HTTPServer#start: pid=1 port=3000
Finally, what we need to do is create a database, open another terminal, and run:
$ fig run web rake db:create
This web app has already started listening to 3000 ports in your docker daemon (if you have a boot2docker,
boot2docker ip
you'll see its address).