Skip to content

Instantly share code, notes, and snippets.

@ndbroadbent
Last active November 16, 2019 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndbroadbent/3cc1273f8cfe7439da02548326e5afcb to your computer and use it in GitHub Desktop.
Save ndbroadbent/3cc1273f8cfe7439da02548326e5afcb to your computer and use it in GitHub Desktop.
Compile Dockerfile templates for dev + builds
./script/compile_dockerfiles
# While working on Dockerfiles, run: export DOCKER_DEV=true
# This will split up all the commands into separate RUN statements,
# so that each layer is cached separately.
# When building the final Docker image, run: unset DOCKER_DEV
# Then the built Docker image will run everything in a single command.
if [ "$DOCKER_DEV" = "true" ]; then
DOCKERFILE=docker/ci/Dockerfile.ci.dev
else
DOCKERFILE=docker/ci/Dockerfile.ci
fi
echo "=> Building $DOCKERFILE..."
docker build -f "$DOCKERFILE" "$BUILD_TMPDIR"
if [ "$DOCKER_DEV" = "true" ]; then
puts "[Docker Dev] Inspecting $IMAGE_TAG layers with dive..."
dive "$IMAGE_TAG"
exit
fi
#!/usr/bin/env ruby
# frozen_string_literal: true
# While working on a Dockerfile, we want to split everything up into
# separate RUN commands so that we can take advantage of Docker's layer caching,
# and we don't have to keep running the same commands over and over.
# Then when we build the real image, we want to run all of these commands together
# and store them in a single layer.
# Our Dockerfiles are written as ERB templates so that we can easily switch
# between these 2 behaviors.
require 'erb'
require 'pry-byebug'
def group_commands(*commands)
if @dev
return commands.map do |cmd|
indented_cmd = cmd.lines.map { |l| " #{l}" }.join.strip
"RUN #{indented_cmd}"
end.join("\n")
end
joined_commands = commands.join(" \\\n && ")
"RUN #{joined_commands}"
end
def install_packages(*packages)
indent_packages = packages.map { |p| " #{p}" }
"apt-get install -y -q --no-install-recommends \\\n" \
"#{indent_packages.join(" \\\n")}"
end
def apt_cleanup
"apt-get autoremove --assume-yes && rm -rf /var/lib/apt/lists/*"
end
puts "=> Compiling ERB templates into dev + build Dockerfiles..."
root_dir = File.expand_path('../../', __dir__)
dockerfile_templates = Dir.glob(File.join(root_dir, 'docker/**/*.erb'))
dockerfile_templates.each do |path|
absolute_path = File.expand_path(path)
# puts dockerfile_templates
erb = ERB.new(File.read(absolute_path), nil, '-')
erb.filename = absolute_path
dev_path = absolute_path.sub(/\.erb$/, '.dev')
build_path = absolute_path.sub(/\.erb$/, '')
@dev = true
dev_dockerfile = erb.result(binding).chomp
puts "====> Writing dev Dockerfile: #{dev_path.sub(root_dir, '.')}"
File.open(dev_path, 'w') { |f| f.puts dev_dockerfile }
@dev = false
build_dockerfile = erb.result(binding).chomp
puts "====> Writing build Dockerfile: #{build_path.sub(root_dir, '.')}"
File.open(build_path, 'w') { |f| f.puts build_dockerfile }
end
FROM debian:stretch-slim
# Set up a node group + user, minimal .bashrc, .gemrc, install packages
RUN groupadd --gid 1000 node \
&& useradd --uid 1000 --gid node --shell /bin/bash --create-home node \
&& echo 'export LS_OPTIONS="--color=auto"\
\nalias ls="ls $LS_OPTIONS"\
\nalias ll="ls $LS_OPTIONS -l"\
\nalias l="ls $LS_OPTIONS -lA"' >> "$HOME/.bashrc" \
&& echo "gem: --no-rdoc --no-ri" > "$HOME/.gemrc"
ARG RUBY_VERSION=2.5.7-jemalloc
# Python is required for the gsutil command (for ci_cache script).
# We were previously installing the whole google-cloud-sdk package, but it was huge.
# See: https://cloud.google.com/storage/docs/gsutil_install
<%= group_commands(
'apt-get update -q \
&& apt-get dist-upgrade --assume-yes \
&& apt-get install --assume-yes -q --no-install-recommends \
curl gnupg apt-transport-https ca-certificates',
'curl -SLf https://raw.githubusercontent.com/fullstaq-labs/fullstaq-ruby-server-edition/master/fullstaq-ruby.asc | apt-key add - \
&& echo "deb https://apt.fullstaqruby.org debian-9 main" > /etc/apt/sources.list.d/fullstaq-ruby.list \
&& apt-get update -q',
install_packages('fullstaq-ruby-${RUBY_VERSION}'),
install_packages('libjemalloc1'),
install_packages('python2.7', 'python-pip'),
'apt-get remove --assume-yes gnupg apt-transport-https',
apt_cleanup)
-%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment