Skip to content

Commit

Permalink
chore: 使用 s6-overlay 作为多进程管理服务
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Jul 21, 2020
1 parent f5c3a2f commit b00a55a
Show file tree
Hide file tree
Showing 20 changed files with 132 additions and 52 deletions.
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ RUN SECRET_TOKEN=precompile_placeholder bin/rails assets:precompile && \
cp -r public/ new_public/

# Remove folders not needed in resulting image
RUN rm -rf node_modules tmp/cache spec .browserslistrc babel.config.js \
RUN rm -rf docker node_modules tmp/cache spec .browserslistrc babel.config.js \
package.json postcss.config.js yarn.lock

##################################################################################
Expand All @@ -63,9 +63,10 @@ ARG REPLACE_CHINA_MIRROR="true"
ARG ORIGINAL_REPO_URL="http://dl-cdn.alpinelinux.org"
ARG MIRROR_REPO_URL="https://mirrors.tuna.tsinghua.edu.cn"
ARG RUBYGEMS_SOURCE="https://gems.ruby-china.com/"
ARG PACKAGES="tzdata imagemagick imagemagick-dev postgresql-dev postgresql-client openssl openssl-dev"
ARG PACKAGES="tzdata curl shadow logrotate imagemagick imagemagick-dev postgresql-dev postgresql-client openssl openssl-dev"
ARG RUBY_GEMS="bundler"
ARG APP_ROOT=/app
ARG S6_OVERLAY_VERSION="2.0.0.1"

LABEL im.ews.zealot.build-date=$BUILD_DATE \
im.ews.zealot.vcs-ref=$VCS_REF \
Expand All @@ -77,11 +78,13 @@ LABEL im.ews.zealot.build-date=$BUILD_DATE \
im.ews.zealot.maintaner="icyleaf <icyleaf.cn@gmail.com>"

ENV TZ="Asia/Shanghai" \
PS1="$(whoami)@$(hostname):$(pwd)$ " \
DOCKER_TAG="$TAG" \
BUNDLE_APP_CONFIG="$APP_ROOT/.bundle" \
ZEALOT_VCS_REF="$VCS_REF" \
ZEALOT_VERSION="$ZEALOT_VERSION" \
RAILS_ENV="production"
RAILS_ENV="production" \
ENABLE_BOOTSNAP="false"

# System dependencies
RUN set -ex && \
Expand All @@ -92,18 +95,15 @@ RUN set -ex && \
gem sources --add $RUBYGEMS_SOURCE --remove https://rubygems.org/; \
fi && \
apk --update --no-cache add $PACKAGES && \
cp /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone && \
curl -L -s https://github.com/just-containers/s6-overlay/releases/download/v${S6_OVERLAY_VERSION}/s6-overlay-amd64.tar.gz | tar xvzf - -C / && \
gem install $RUBY_GEMS && \
echo "0 */5 * * * /bin/sh -l -c 'find /tmp -type f -mmin +300 -exec rm -f {} \;' >> /var/log/clean_tmp_cron.log 2>&1" > /etc/crontabs/clean_tmp && \
chmod 0644 /etc/crontabs/clean_tmp && \
touch /var/log/clean_tmp_cron.log && \
crontab /etc/crontabs/clean_tmp
adduser -D -u 911 -g zealot -h /app -s /sbin/nologin zealot

WORKDIR $APP_ROOT

COPY docker/rootfs /
COPY --from=builder $APP_ROOT $APP_ROOT

EXPOSE 3000

ENTRYPOINT [ "./docker-endpoint.sh" ]
ENTRYPOINT ["/init"]
7 changes: 6 additions & 1 deletion config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.

# Broken in docker image, need set it false
# Ref: https://github.com/Shopify/bootsnap/issues/262
if ENV.fetch('ENABLE_BOOTSNAP', 'true') == 'true'
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
end
41 changes: 0 additions & 41 deletions docker-endpoint.sh

This file was deleted.

Empty file.
16 changes: 16 additions & 0 deletions docker/rootfs/etc/cont-init.d/00-adduser
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/with-contenv sh

PUID=${PUID:-911}
PGID=${PGID:-911}

groupmod -o -g "$PGID" zealot
usermod -o -u "$PUID" zealot

echo "
-------------------------------------
GID/UID
-------------------------------------
User uid: $(id -u zealot)
User gid: $(id -g zealot)
-------------------------------------
"
8 changes: 8 additions & 0 deletions docker/rootfs/etc/cont-init.d/01-set-timezone
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/with-contenv sh

TZ=${TZ:-"Asia/Shanghai"}

if [[ -f "/usr/share/zoneinfo/$TZ" ]]; then
ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime
echo "$TZ" > /etc/timezone
fi
4 changes: 4 additions & 0 deletions docker/rootfs/etc/cont-init.d/11-set-crontab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/with-contenv sh

touch /var/log/clean_tmp_cron.log
crontab /etc/crontabs/clean_tmp
14 changes: 14 additions & 0 deletions docker/rootfs/etc/cont-init.d/12-logrotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/with-contenv bash

if [[ ! -f /mnt/config/etc/logrotate.conf ]]; then
if [[ ! -d /mnt/config/etc ]]; then
s6-setuidgid guardian mkdir -p /mnt/config/etc
fi

s6-setuidgid guardian cp /etc/logrotate.conf /mnt/config/etc/logrotate.conf
fi

if [[ ! -d /mnt/config/etc/logrotate.d ]]; then
s6-setuidgid guardian mkdir -p /mnt/config/etc/logrotate.d
s6-setuidgid guardian cp -n /etc/logrotate.d/* /mnt/config/etc/logrotate.d/
fi
25 changes: 25 additions & 0 deletions docker/rootfs/etc/cont-init.d/20-init-zealot
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/with-contenv sh

cd /app

# Clear pid if unexpected exception exit
s6-setuidgid zealot rm -f tmp/pids/.pid
s6-setuidgid zealot mkdir -p tmp/pids tmp/cache tmp/uploads tmp/sockets log

# Updating assets
if [ -d "new_public" ]; then
echo "Zealot updating public ..."
for x in public/*; do
if [ -z $(echo "$x" | grep uploads) ]; then
s6-setuidgid zealot rm -rf "$x"
fi
done

for x in new_public/*; do
if [ -z $(echo "$x" | grep uploads) ]; then
s6-setuidgid zealot mv "$x" "public"
fi
done

s6-setuidgid zealot rm -rf new_public
fi
4 changes: 4 additions & 0 deletions docker/rootfs/etc/cont-init.d/25-upgrade-zealot
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/with-contenv sh

cd /app && ./bin/rails zealot:upgrade
exit 0
1 change: 1 addition & 0 deletions docker/rootfs/etc/crontabs/clean_tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 */5 * * * /bin/sh -l -c 'find /tmp -type f -mmin +300 -exec rm -f {} \;' >> /var/log/clean_tmp_cron.log 2>&1
1 change: 1 addition & 0 deletions docker/rootfs/etc/fix-attrs.d/11-crontab-dir
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/etc/crontabs/clean_tmp false root:root 0644 0755
3 changes: 3 additions & 0 deletions docker/rootfs/etc/fix-attrs.d/20-zealot-app-dir
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/app true zealot,911:911 0644 0755
/app/bin true zealot,911:911 0755 0755
/app/vendor true zealot,911:911 0755 0755
14 changes: 14 additions & 0 deletions docker/rootfs/etc/logrotate.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
weekly
rotate 4

create
dateext
compress

notifempty
nomail
noolddir

tabooext + .apk-new

include /etc/logrotate.d
4 changes: 4 additions & 0 deletions docker/rootfs/etc/logrotate.d/crond-clean-tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/var/log/clean_tmp_cron.log {
missingok
notifempty
}
5 changes: 5 additions & 0 deletions docker/rootfs/etc/logrotate.d/sidekiq
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/app/log/sidekiq.log {
missingok
notifempty
monthly
}
4 changes: 4 additions & 0 deletions docker/rootfs/etc/logrotate.d/zealot
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/app/log/zealot.log {
missingok
notifempty
}
3 changes: 3 additions & 0 deletions docker/rootfs/etc/services.d/crond/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/execlineb -P

/usr/sbin/crond -fS
5 changes: 5 additions & 0 deletions docker/rootfs/etc/services.d/sidekiq/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/with-contenv sh

echo "Starting Zealot Worker"
cd /app
bundle exec sidekiq -C config/sidekiq.yml | tee /app/log/sidekiq.log
5 changes: 5 additions & 0 deletions docker/rootfs/etc/services.d/zealot/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/with-contenv sh

echo "Starting Zealot"
cd /app
bundle exec puma -C config/puma.rb | tee /app/log/zealot.log

0 comments on commit b00a55a

Please sign in to comment.