Skip to content

Commit

Permalink
chore(docker): add tools for generating docker images
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Jun 29, 2021
1 parent a735219 commit 8145dba
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 22 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ node_modules
Dockerfile
vendor/bundle
.cache
docker/*
PotamusFile
14 changes: 14 additions & 0 deletions .schmersion.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports:
- name: CHANGELOG.md
formatter: markdown
options:
title: CHANGELOG
description: This file contains all the latest changes and updates to Postal.
sections:
- title: Features
types: [feat]
- title: Bug Fixes
types: [fix]
#
# There are additional options you can define, check out the README for details.
# https://github.com/krystal/schmersion
43 changes: 25 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
FROM ruby:2.6

RUN apt-get update
RUN apt-get install software-properties-common -y

# Setup additional repositories
RUN apt-key adv --recv-keys --keyserver hkp:https://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
RUN add-apt-repository 'deb [arch=amd64,i386,ppc64el] http:https://mirrors.coreix.net/mariadb/repo/10.1/ubuntu xenial main'
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN apt-get update
FROM ruby:2.6 AS base

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
software-properties-common \
&& apt-key adv --recv-keys --keyserver hkp:https://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 \
&& add-apt-repository 'deb [arch=amd64,i386,ppc64el] http:https://mirrors.coreix.net/mariadb/repo/10.1/ubuntu xenial main' \
&& (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -) \
&& (echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list) \
&& (curl -sL https://deb.nodesource.com/setup_12.x | bash -) \
&& rm -rf /var/lib/apt/lists/*

# Install main dependencies
RUN apt-get install -y \
RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
curl \
libmariadbclient-dev \
Expand All @@ -24,25 +27,29 @@ RUN mkdir -p /opt/postal/app /opt/postal/config
WORKDIR /opt/postal/app

# Install bundler
RUN gem install bundler --no-doc
RUN bundle config frozen 1
RUN bundle config build.sassc --disable-march-tune-native
RUN gem install bundler -v 2.1.4 --no-doc

# Install the latest and active gem dependencies and re-run
# the appropriate commands to handle installs.
COPY Gemfile Gemfile.lock ./
RUN bundle install -j 4

# Copy the application (and set permissions)
COPY ./docker/wait-for.sh /docker-entrypoint.sh
COPY --chown=postal . .

# Export the version
ARG VERSION=unspecified
RUN echo $VERSION > VERSION

# Set the CMD
ENTRYPOINT [ "/docker-entrypoint.sh" ]
CMD ["bundle", "exec"]

FROM base AS prod
# Copy temporary configuration file which can be used for
# running the asset precompilation.
COPY --chown=postal config/postal.defaults.yml /opt/postal/config/postal.yml

# Precompile assets
RUN POSTAL_SKIP_CONFIG_CHECK=1 RAILS_GROUPS=assets bundle exec rake assets:precompile
RUN touch /opt/postal/app/public/assets/.prebuilt

# Set the CMD
CMD ["bundle", "exec"]
98 changes: 98 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# ==============================================================================
# Welcome to the Makefile
# ==============================================================================
#
# This Makefile contains a series of tasks which can help with building, testing
# and working with the app. The following tasks are available to you:
#
# Image building & releasing actions:
#
# make docker-build - Builds a production Docker image.
# make docker-ci-build - Builds an image without any asset compilation,
# ideal for running test suites and similar tasks.
# make docker-image - Builds a production Docker image and tags it as
# appropriate based on the current branch & tag.
# make docker-release - Builds a production Docker image and uploads to the
# registry.
#

# ==============================================================================
# Configuration
# ==============================================================================

# Docker image name to release the production image as.
DOCKER_IMAGE := postalhq/postal

# Path to bundle config
BUNDLE_CONFIG ?= $(HOME)/.bundle/config

# Detect if a tty is available
TTY := $(shell [ -t 0 ] && echo 1)

# Tag names
DOCKER_TAG_NAME ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
DOCKER_TAG_VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null)

ifeq ($(DOCKER_TAG_NAME),master)
DOCKER_TAG_NAME = latest
endif

ifeq ($(DOCKER_TAG_NAME),main)
DOCKER_TAG_NAME = latest
endif

# Version string to use
VERSION ?= $(DOCKER_TAG_VERSION)
ifeq ($(VERSION),)
VERSION = $(shell git describe --tags 2>/dev/null)
endif

ifeq ($(VERSION),)
VERSION = 0.0.0-dev
endif

# ==============================================================================
# Image Building
# ==============================================================================

DOCKER_BUILD_CMD = DOCKER_BUILDKIT=1 docker \
build $(if $(TTY),,--progress plain) \
--build-arg VERSION=$(VERSION) \
--secret "id=bundle_config,src=$(BUNDLE_CONFIG)" \
.

DOCKER_CI_BUILD_CMD = $(DOCKER_BUILD_CMD) --target=ci

.PHONY: docker-build
docker-build:
$(DOCKER_BUILD_CMD)

.PHONY: docker-ci-build
docker-ci-build:
$(DOCKER_CI_BUILD_CMD)

# ==============================================================================
# Image Tagging
# ==============================================================================

.PHONY: docker-image
docker-image: docker-build
$(eval IMAGE := $(shell $(DOCKER_BUILD_CMD) -q))
ifeq ($(DOCKER_TAG_NAME),latest)
docker tag "$(IMAGE)" "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)"
endif

# ==============================================================================
# Image Releasing
# ==============================================================================

.PHONY: docker-release
docker-release: docker-image
ifeq ($(DOCKER_TAG_NAME),latest)
docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)"
endif
ifneq ($(DOCKER_TAG_VERSION),)
docker tag "$(DOCKER_IMAGE):$(DOCKER_TAG_NAME)" \
"$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)" && \
docker push "$(DOCKER_IMAGE):$(DOCKER_TAG_VERSION)"
endif
2 changes: 0 additions & 2 deletions PotamusFile

This file was deleted.

60 changes: 60 additions & 0 deletions docker/wait-for.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh
[ -n "$DEBUG" ] && set -x

check_http() {
wget -T 1 -S -q -O - "$1" 2>&1 | head -1 |
head -1 | grep -E 'HTTP.+\s2\d{2}' >/dev/null 2>&1
return $?
}

check_tcp() {
host="$(echo "$1" | cut -d: -f1)"
port="$(echo "$1" | cut -d: -f2)"
if [ -z "${host}" ] || [ -z "${port}" ]; then
echo "TCP target ${1} is not in \"<host>:<port>\" format" >&2
exit 2
fi

nc -z -w1 "$host" "$port" >/dev/null 2>&1
return $?
}

wait_for() {
type="$1"
uri="$2"
timeout="${3:-30}"

seconds=0
while [ "$seconds" -lt "$timeout" ] && ! "check_${type}" "$uri"; do
if [ "$seconds" -lt "1" ]; then
printf "Waiting for %s ." "$uri"
else
printf .
fi
seconds=$((seconds + 1))
sleep 1
done

if [ "$seconds" -lt "$timeout" ]; then
if [ "$seconds" -gt "0" ]; then
echo " up!"
fi
else
echo " FAIL"
echo "ERROR: unable to connect to: $uri" >&2
exit 1
fi
}

if [ -n "$WAIT_FOR_TARGETS" ]; then
uris="$(echo "$WAIT_FOR_TARGETS" | sed -e 's/\s+/\n/g' | uniq)"
for uri in $uris; do
if echo "$uri" | grep -E '^https?:https://.*' >/dev/null 2>&1; then
wait_for "http" "$uri" "$WAIT_FOR_TIMEOUT"
else
wait_for "tcp" "$uri" "$WAIT_FOR_TIMEOUT"
fi
done
fi

exec "$@"

0 comments on commit 8145dba

Please sign in to comment.