`

基于docker打ruby镜像包

阅读更多
一:机遇https://hub.docker.com/_/ruby打原生ruby镜像
build.sh
#!/bin/bash
docker build --no-cache -t registry.xxx.com/xs/ruby:2.6.6-ubuntu  /data/images/ruby2.6.6-ubuntu/ && \ 
docker push registry.xxx.com/xs/ruby:2.6.6-ubuntu


Dockerfile
FROM ubuntu:18.04

RUN set -eux; \
	apt-get update; \
	apt-get install -y --no-install-recommends \
		bzip2 \
		ca-certificates \
		libffi-dev \
		libgmp-dev \
		libssl-dev \
		libyaml-dev \
		procps \
		apt-utils \
		debconf-utils \
		zlib1g-dev \
	; \
        echo "tzdata tzdata/Areas select Asia" > /tmp/preseed.txt && \
        echo "tzdata tzdata/Zones/Asia select Shanghai" >> /tmp/preseed.txt && \
        rm -f /etc/timezone && \
        rm -f /etc/localtime && \
        debconf-set-selections /tmp/preseed.txt && \
	apt-get -y install locales tzdata && \
	rm -rf /var/lib/apt/lists/*

# skip installing gem documentation
RUN set -eux; \
	mkdir -p /usr/local/etc; \
	{ \
		echo 'install: --no-document'; \
		echo 'update: --no-document'; \
	} >> /usr/local/etc/gemrc

ENV RUBY_MAJOR 2.6
ENV RUBY_VERSION 2.6.6
ENV RUBY_DOWNLOAD_SHA256 5db187882b7ac34016cd48d7032e197f07e4968f406b0690e20193b9b424841f

# some of ruby's build scripts are written in ruby
#   we purge system ruby later to make sure our final image uses what we just built
RUN set -eux; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends \
		autoconf \
		bison \
		dpkg-dev \
		gcc \
		libbz2-dev \
		libgdbm-compat-dev \
		libgdbm-dev \
		libglib2.0-dev \
		libncurses-dev \
		libreadline-dev \
		libxml2-dev \
		libxslt-dev \
		make \
		ruby \
		wget \
		xz-utils \
	; \
	rm -rf /var/lib/apt/lists/*; \
	\
	wget -O ruby.tar.xz "https://cache.ruby-lang.org/pub/ruby/${RUBY_MAJOR%-rc}/ruby-$RUBY_VERSION.tar.xz"; \
	echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.xz" | sha256sum --check --strict; \
	\
	mkdir -p /usr/src/ruby; \
	tar -xJf ruby.tar.xz -C /usr/src/ruby --strip-components=1; \
	rm ruby.tar.xz; \
	\
	cd /usr/src/ruby; \
	\
# hack in "ENABLE_PATH_CHECK" disabling to suppress:
#   warning: Insecure world writable dir
	{ \
		echo '#define ENABLE_PATH_CHECK 0'; \
		echo; \
		cat file.c; \
	} > file.c.new; \
	mv file.c.new file.c; \
	\
	autoconf; \
	gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
	./configure \
		--build="$gnuArch" \
		--disable-install-doc \
		--enable-shared \
	; \
	make -j "$(nproc)"; \
	make install; \
	\
	apt-mark auto '.*' > /dev/null; \
	apt-mark manual $savedAptMark > /dev/null; \
	find /usr/local -type f -executable -not \( -name '*tkinter*' \) -exec ldd '{}' ';' \
		| awk '/=>/ { print $(NF-1) }' \
		| sort -u \
		| xargs -r dpkg-query --search \
		| cut -d: -f1 \
		| sort -u \
		| xargs -r apt-mark manual \
	; \
	apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
	\
	cd /; \
	rm -r /usr/src/ruby; \
# verify we have no "ruby" packages installed
	! dpkg -l | grep -i ruby; \
	[ "$(command -v ruby)" = '/usr/local/bin/ruby' ]; \
# rough smoke test
	ruby --version; \
	gem --version; \
	bundle --version

# don't create ".bundle" in all our apps
ENV GEM_HOME /usr/local/bundle
ENV BUNDLE_SILENCE_ROOT_WARNING=1 \
	BUNDLE_APP_CONFIG="$GEM_HOME"
ENV PATH $GEM_HOME/bin:$PATH
# adjust permissions of a few directories for running "gem install" as an arbitrary user
RUN mkdir -p "$GEM_HOME" && chmod 777 "$GEM_HOME"

CMD [ "irb" ]


二:在原生镜像基础上,打包执行gemfile的中间镜像,这样不需要每次up都重新拉gemfile
可以修改使用FROM ubuntu:18.04

build.sh
#!/bin/bash
image_name=ruby-middle
tag=v2
docker build --no-cache -t registry.xxx.com/xs/"$image_name":"$tag"  . && \ 
docker push registry.xxx.com/xs/"$image_name":"$tag"


Dockerfile
FROM registry.xxx.com/xs/ruby:2.6.6-ubuntu18
LABEL layer=middle


#修改时区及中文环境
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true


COPY prepare/ /var/tmp/ruby_om_export
COPY exclude.list /var/tmp/
RUN apt-get update && \
    apt-get install -y locales language-pack-zh-hans net-tools git rsync gcc g++ make libmariadb-dev unzip zip vim && \
    cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone
ENV LANG zh_CN.utf8
ENV LANGUAGE zh_CN:zh

WORKDIR /var/tmp/ruby_om_export
RUN sed -i "s/^ruby.*/ruby \'2.6.6\'/" /var/tmp/ruby_om_export/Gemfile && \
gem i bundler -v 1.17.3 && \
bundle install

#
COPY entrypoint.sh /usr/bin/

#EXPOSE 映射端口
EXPOSE 3002

#CMD 容器运行命令 
ENTRYPOINT ["entrypoint.sh"]



三:机遇中间镜像做git更新发布镜像,可以配合脚本,过滤不必要覆盖的文件。

entrypoint.sh
#!/bin/sh
instfile=/opt/ruby_om_export/init_inst
intdir=/var/tmp/ruby_om_export

#清空日志
if [ -f /opt/ruby_om_export/log/production.log ];then
    :>/opt/ruby_om_export/log/production.log
fi

#拷贝代码文件,排除配置文件
if [ -d "$intdir" ];then
    rsync -a --delete /var/tmp/ruby_om_export /opt/ --exclude-from=/var/tmp/exclude.list && rm -fr /var/tmp/ruby_om_export
    chmod +x /opt/ruby_om_export/bin/*
fi

if [ -f "$instfile" ];then
    cd /opt/ruby_om_export && \
    RAILS_ENV=production bundle exec rake db:migrate && \
    RAILS_ENV=production bundle exec rake assets:precompile && \
    RAILS_ENV=production bundle exec sidekiq -d -C config/sidekiq.yml && \
    /usr/local/bundle/bin/puma -e production && \
    RAILS_ENV=production bundle exec rake monitor_key_event file_path='/opt/ruby_om_export/log/redis_key_event.log' pid_file_path='/opt/ruby_om_export/tmp/pids/monitor_key_event.pid' && \
    tail -f /opt/ruby_om_export/log/production.log
else
    cd /opt/ruby_om_export && \
    RAILS_ENV=production bundle exec rake db:create
    RAILS_ENV=production bundle exec rake db:migrate && \
    RAILS_ENV=production bundle exec rake assets:precompile && \
    RAILS_ENV=production bundle exec sidekiq -d -C config/sidekiq.yml && \
    /usr/local/bundle/bin/puma -e production && \
    RAILS_ENV=production bundle exec rake monitor_key_event file_path='/opt/ruby_om_export/log/redis_key_event.log' pid_file_path='/opt/ruby_om_export/tmp/pids/monitor_key_event.pid' && \
    echo "`date`" > "$instfile" && \
    tail -f /opt/ruby_om_export/log/production.log
fi


exclude.list
config/database.yml
config/secrets.yml
config/mqtt.yml
config/mongoid.yml
log/redis_key_event.log
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics