Skip to content

Commit 188c855

Browse files
Merge pull request #31 from mfojtik/centos-base-tweaks
Refactoring of the centos-ruby-* images to share code and avoid duplication
2 parents 8877f10 + a89f4de commit 188c855

File tree

15 files changed

+112
-154
lines changed

15 files changed

+112
-154
lines changed

centos-ruby-builder/Dockerfile

+7-14
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616
FROM openshift/centos-ruby
1717
MAINTAINER Michal Fojtik <[email protected]>
1818

19-
# Set the user we want the Ruby application run under and set the
20-
# user HOME directory as Docker reset this to '/' [1]
21-
#
22-
# [1] https://github.com/dotcloud/docker/issues/2968
23-
#
24-
USER ruby
25-
ENV HOME /opt/ruby
26-
ENV PATH $HOME/bin:$PATH
27-
28-
# Set the initial command to `build`. After the application image is built
29-
# then the command is replaced by `run`
30-
#
31-
ADD ./bin/build /opt/ruby/bin/start
32-
ADD ./bin/run /opt/ruby/bin/run
19+
ADD ./bin /opt/ruby/bin
3320
ADD ./etc /opt/ruby/etc
3421

22+
# The initial 'start' command will trigger an application build when it runs in
23+
# 'self-contained' builder context. After application is built, then this
24+
# command is replaced in final application image by the 'run' command.
25+
#
26+
RUN chown -R ruby:ruby /opt/ruby/bin
3527

28+
USER ruby
3629
ENTRYPOINT ["/opt/ruby/bin/start"]

centos-ruby-builder/bin/build

-34
This file was deleted.

centos-ruby-builder/bin/run

-25
This file was deleted.

centos-ruby-builder/bin/start

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash -e
2+
3+
source ${HOME}/etc/sdk
4+
source ${HOME}/etc/helpers
5+
6+
# Allow users to inspect/debug the builder image itself, by using:
7+
# $ docker run -i -t openshift/centos-ruby-builder --debug
8+
#
9+
[ "$1" == "--debug" ] && exec /bin/bash
10+
11+
# If the /tmp/src is empty, then print usage and exit.
12+
(dir_is_empty /tmp/src) && print_usage_and_exit
13+
14+
# Build the ruby application first
15+
#
16+
${HOME}/bin/prepare
17+
18+
# Replace this script with the application runner at the end of the build.
19+
#
20+
echo "---> Build successful. Commit this image with: docker commit ${HOSTNAME} ruby-app"
21+
cp -f ${HOME}/bin/run ${HOME}/bin/start && exit

centos-ruby-builder/etc/helpers

-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
# Ruby builder helper functions
22
#
33

4-
# Execute ENTRYPOINT commands using.
5-
# Note: This command exits after it finishes
6-
#
7-
function bundle_exec() {
8-
exec /opt/ruby/bin/ruby_context bundle exec "$@"
9-
}
10-
11-
# FIXME: You can't stop the bundler install for now, using SIGINT or Ctrl-C
12-
# because it is not executed using 'exec'. The 'exec' replace the parent
13-
# process with itself and so later command are not executed.
14-
#
15-
function bundle_install() {
16-
BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development:test"}
17-
/opt/ruby/bin/ruby_context bundle install $@
18-
}
19-
204
function print_usage_and_exit() {
215
echo
226
echo "This image can build your Ruby source code."
@@ -30,7 +14,3 @@ function print_usage_and_exit() {
3014
echo
3115
exit
3216
}
33-
34-
function is_puma_installed() {
35-
grep ' puma ' Gemfile.lock >/dev/null
36-
}

centos-ruby-builder/etc/puma.cfg

-8
This file was deleted.

centos-ruby/Dockerfile

+15-6
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ RUN yum update --assumeyes && \
2323

2424
# Create 'ruby' account we will use to run Ruby application
2525
#
26-
RUN mkdir -p /opt/ruby/{gems,run,src} && \
26+
RUN mkdir -p /opt/ruby/{gems,run,src,bin} && \
2727
groupadd -r ruby -f -g 433 && \
28-
useradd -u 431 -r -g ruby -d /opt/ruby -s /sbin/nologin -c "Ruby application" ruby && \
29-
chown -R ruby:ruby /opt/ruby
28+
useradd -u 431 -r -g ruby -d /opt/ruby -s /sbin/nologin -c "Ruby application" ruby
3029

31-
ADD ./bin /usr/bin/
30+
ADD ./bin /opt/ruby/bin/
3231
ADD ./etc /opt/ruby/etc/
33-
RUN chmod +x /usr/bin/{prepare,run,ruby_context,save-artifacts,usage}
32+
33+
# FIXME: The STI require all scripts in /usr/bin path, this layer is here to
34+
# maintain backward compatibility
35+
#
36+
RUN cp -f /opt/ruby/bin/prepare /usr/bin/prepare && \
37+
cp -f /opt/ruby/bin/run /usr/bin/run && \
38+
cp -f /opt/ruby/bin/save-artifacts /usr/bin/save-artifacts
39+
40+
RUN chown -R ruby:ruby /opt/ruby
3441

3542
# Set the 'root' directory where this build will search for Gemfile and
3643
# config.ru.
@@ -42,6 +49,8 @@ RUN chmod +x /usr/bin/{prepare,run,ruby_context,save-artifacts,usage}
4249
# GIT repository. The default value is the root folder.
4350
#
4451
ENV APP_ROOT .
52+
ENV HOME /opt/ruby
53+
ENV PATH $HOME/bin:$PATH
4554

4655
# FIXME: This might be a potential bug in STI where if we keep the USER
4756
# instruction here, the resulting application image will fail to run.
@@ -52,4 +61,4 @@ EXPOSE 9292
5261

5362
# Display STI usage when invoked outside STI builder
5463
#
55-
CMD ["/usr/bin/usage"]
64+
CMD ["/opt/ruby/bin/usage"]

centos-ruby/bin/prepare

100644100755
+17-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
#!/bin/sh -e
22

3-
source /opt/ruby/etc/sdk
3+
source ${HOME}/etc/sdk
44

55
restore_build_artifacts
66

7-
app_src_dir="/opt/ruby/src/"
8-
app_root_dir="${app_src_dir}${APP_ROOT:-.}"
9-
bundle_dir="/opt/ruby/bundle"
7+
app_runtime_dir="${HOME}/src"
8+
app_src_dir="/tmp/src"
109

11-
echo "Building your Ruby application"
10+
echo "---> Installing application source"
11+
cp -Rf ${app_src_dir}/* ${app_runtime_dir}/
1212

13-
mkdir -p $app_src_dir
14-
cp -Rf /tmp/src/* ${app_src_dir}/
15-
16-
cd $app_root_dir
13+
pushd "$app_runtime_dir/${APP_ROOT}" >/dev/null
14+
echo "---> Building your Ruby application from source"
1715

1816
if [ -f Gemfile ]; then
19-
echo "Installing dependencies using Bundler"
17+
echo "---> Installing dependencies using Bundler"
18+
2019
ADDTL_BUNDLE_ARGS=""
2120
if [ -f Gemfile.lock ]; then
2221
ADDTL_BUNDLE_ARGS="--deployment"
2322
fi
23+
2424
if [[ "$RAILS_ENV" == "development" || "$RACK_ENV" == "development" ]]; then
2525
BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"test"}
2626
elif [[ "$RAILS_ENV" == "test" || "$RACK_ENV" == "test" ]]; then
2727
BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development"}
2828
else
2929
BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development:test"}
3030
fi
31-
ruby_context "bundle install --path ${bundle_dir} ${ADDTL_BUNDLE_ARGS}"
31+
32+
bundle_install --path ${HOME}/bundle ${ADDTL_BUNDLE_ARGS}
3233
fi
34+
35+
# TODO: Add `rake assets:precompile` step here for Rails applications
36+
# TODO: Add `rake db:migrate` if linked with DB container
37+
38+
popd >/dev/null

centos-ruby/bin/ruby_context

100644100755
+12-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
source /opt/ruby/etc/sdk
44

5-
export GEM_HOME=/opt/ruby/gems
6-
export GEMRC=/opt/ruby/etc/gemrc
5+
export GEM_HOME=${HOME}/gems
6+
export GEMRC=${HOME}/etc/gemrc
7+
export EXECJS_RUNTIME=Node
78

8-
export RUBY_BIN_DIR="/opt/rh/ruby193/root/usr/bin"
9-
export GEM_INSTALL_OPTS="--bindir ${RUBY_BIN_DIR}"
9+
function scl_env() {
10+
/usr/bin/scl enable ruby193 nodejs010 "printenv $1"
11+
}
1012

11-
# If NodeJS is installed in system, then enable it to allow Rails
12-
# assets to compile successfully
13+
# FIXME: This is required in order to 'exec' run ruby instead of
14+
# 'scl' command. This have to be fixed in SCL in (near) future.
1315
#
16+
vars=( PATH LD_LIBRARY_PATH PYTHONPATH PKG_CONFIG_PATH )
17+
for var in "${vars[@]}"; do export ${var}=$(scl_env "${var}"); done
1418

15-
collections="ruby193 $(is_nodejs_installed && echo 'nodejs010')"
16-
17-
# You can reference environment variables in Dockerfile RUN
18-
# command and they will be substituted here
19-
#
20-
exec /usr/bin/scl enable ${collections} "$(echo -n "$@" | envsubst)"
19+
cmd="$1" && shift
20+
exec "$cmd" "$@"

centos-ruby/bin/run

100644100755
+15-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
#!/bin/bash
1+
#!/bin/bash -e
22

33
source /opt/ruby/etc/sdk
44

5-
app_src_dir="/opt/ruby/src/"
6-
app_root_dir="${app_src_dir}${APP_ROOT:-.}"
5+
# The app_root_dir needs to be exported, so the Puma or other Ruby
6+
# application server can pick it up and use it as 'application root'
7+
#
8+
export app_root_dir="${HOME}/src/${APP_ROOT:-.}"
9+
10+
export RACK_ENV=${RACK_ENV:-"production"}
11+
export RAILS_ENV=${RAILS_ENV:-"${RACK_ENV}"}
712

813
cd $app_root_dir
914

10-
# Choose the best application server to use. If users have 'puma' in the
11-
# Gemfile, then start the Ruby application using puma.
12-
#
13-
# Otherwise fallback to 'rackup'
15+
# Allow users to inspect/debug the builder image itself, by using:
16+
# $ docker run -i -t openshift/centos-ruby-builder --debug
1417
#
18+
[ "$1" == "--debug" ] && exec /bin/bash
19+
1520
if is_puma_installed; then
16-
puma_cfg="/opt/ruby/etc/puma.cfg"
17-
/usr/bin/ruby_context "bundle exec 'puma --config $puma_cfg'"
21+
bundle_exec "puma --config ${HOME}/etc/puma.cfg"
1822
else
19-
/usr/bin/ruby_context "bundle exec 'rackup'"
23+
echo "You might consider adding 'puma' into your Gemfile."
24+
bundle_exec "rackup -P ${HOME}/run/rack.pid --host 0.0.0.0"
2025
fi

centos-ruby/bin/save-artifacts

100644100755
File mode changed.

centos-ruby/bin/usage

100644100755
File mode changed.

centos-ruby/build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22

3-
(cd base && docker build -t openshift/centos-ruby .)
4-
(cd extended && docker build -t openshift/centos-ruby-extended .)
3+
(docker build -t openshift/centos-ruby .)
4+
(cd ../centos-ruby-builder && docker build -t openshift/centos-ruby-builder .)

centos-ruby/etc/puma.cfg

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
environment 'production'
2-
pidfile '/opt/ruby/run/puma.pid'
3-
state_path '/opt/ruby/run/puma.state'
1+
environment ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
2+
pidfile "#{ENV['HOME']}/run/puma.pid"
3+
state_path "#{ENV['HOME']}/run/puma.state"
44
threads 0, 16
55
workers 0
66
bind 'tcp://0.0.0.0:9292'
7-
8-
preload_app!

centos-ruby/etc/sdk

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
function is_puma_installed() {
2+
[ ! -f Gemfile.lock ] && return 1
23
grep ' puma ' Gemfile.lock >/dev/null
34
}
45

5-
function is_nodejs_installed() {
6-
(/usr/bin/scl -l | grep nodejs010) >/dev/null
7-
}
8-
96
function restore_build_artifacts() {
107
if [ -e /tmp/artifacts/gems.tgz ]; then
11-
echo -n "Restoring build artifacts"
8+
echo "Restoring build artifacts"
129
pushd / >/dev/null
1310
tar zxf /tmp/artifacts/gems.tgz
1411
popd >/dev/null
1512
fi
1613
}
1714

18-
function dir_not_empty() {
19-
[ -n "$(ls -A "$1" 2>/dev/null)" ]
15+
function dir_is_empty() {
16+
[ -z "$(ls -A "$1" 2>/dev/null)" ]
17+
}
18+
19+
# Execute ENTRYPOINT commands using.
20+
# Note: This command exits after it finishes
21+
#
22+
function bundle_exec() {
23+
exec ruby_context bundle exec "$@"
24+
}
25+
26+
# FIXME: You can't stop the bundler install for now, using SIGINT or Ctrl-C
27+
# because it is not executed using 'exec'. The 'exec' replace the parent
28+
# process with itself and so later command are not executed.
29+
#
30+
function bundle_install() {
31+
BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development:test"}
32+
ruby_context bundle install $@
2033
}

0 commit comments

Comments
 (0)