Skip to content

Commit

Permalink
simplify docker build and container init (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Oct 1, 2018
1 parent c67ccad commit 54853b3
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 137 deletions.
40 changes: 3 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
PKG_NAME := synse
PKG_VERSION := $(shell python -c "import synse ; print(synse.__version__)")
IMAGE_NAME := vaporio/synse-server

GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2> /dev/null || true)
GIT_TAG ?= $(shell git describe --tags 2> /dev/null || true)
BUILD_DATE := $(shell date -u +%Y-%m-%dT%T 2> /dev/null)

IMAGE_DOCKERFILE ?= release.dockerfile
IMAGE_TAGS ?= latest local
IMAGE_TAG_SUFFIX ?=
IMAGE_TAGS ?= latest local

HAS_TRANSLATIONS := $(shell find synse -name '*.mo')
HAS_PY36 := $(shell which python3.6 || python -V 2>&1 | grep 3.6 || python3 -V 2>&1 | grep 3.6)
Expand All @@ -31,34 +24,6 @@ NC := \033[0m
# only be called as dependencies of other targets.
#

.PHONY: build-docker
# build the docker images with the given tags and tag suffix for the specified
# dockerfile.
build-docker:
echo "image tags: ${GREEN}$(IMAGE_TAGS)${NC}"
tags="" ; \
for tag in $(IMAGE_TAGS); do if [ "$(IMAGE_TAG_SUFFIX)" ]; then tag="$$tag-$(IMAGE_TAG_SUFFIX)"; fi; tags="$${tags} -t $(IMAGE_NAME):$${tag}" ; done ; \
echo "${GREEN}tags: $$tags${NC}" ; \
docker build -f dockerfile/$(IMAGE_DOCKERFILE) \
--build-arg BUILD_DATE=$(BUILD_DATE) \
--build-arg BUILD_VERSION=$(PKG_VERSION) \
--build-arg VCS_REF=$(GIT_COMMIT) \
$${tags} .

.PHONY: docker-default
# build the docker images for release.dockerfile -- this includes the emulator
docker-default:
@$(MAKE) build-docker

.PHONY: docker-slim
# build the docker images for slim.dockerfile -- this does not include the emulator
docker-slim:
# Build the 'vaporio/synse-server:base' image. This will be used as the base
# for the 'release' image.
@$(MAKE) build-docker IMAGE_TAGS=base IMAGE_DOCKERFILE=slim.dockerfile
# Tag the slim image with the appropriate '-slim' tags.
@$(MAKE) build-docker IMAGE_TAG_SUFFIX=slim IMAGE_DOCKERFILE=slim.dockerfile

.PHONY: pycache-clean
# pycache-clean is used to clean out .pyc and .pyo files and the __pycache__
# directories from the ./tests directory. This isn't always necessary, but
Expand Down Expand Up @@ -100,7 +65,8 @@ endif
pip-compile --output-file requirements.txt setup.py

.PHONY: docker
docker: req-translations docker-slim docker-default ## Build the docker image locally
docker: req-translations ## Build the docker image locally
@ IMAGE_TAGS="$(IMAGE_TAGS)" IMAGE_NAME=$(IMAGE_NAME) ./bin/build.sh

.PHONY: docs
docs: clean-docs ## Generate the User Guide and API documentation locally
Expand Down
59 changes: 59 additions & 0 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

# build.sh
#
# Build the Synse Server docker images (slim and full) with the
# appropriate tags.
#
# This script is intended to be called via the project Makefile
# or CI workflow where the environment variables are managed.
#

red="\033[0;31m"
green="\033[0;32m"
nc="\033[0m"

if [ ! "${IMAGE_NAME}" ]; then
echo -e "${red}error: no image name specified for build${nc}"
exit 1
fi

if [ ! "${IMAGE_TAGS}" ]; then
echo -e "${red}error: no image tags specified for build${nc}"
exit 1
fi

image="${IMAGE_NAME}"
tags="${IMAGE_TAGS}"
version=$(python -c "import synse ; print(synse.__version__)")
build_date=$(date -u +%Y-%m-%dT%T 2> /dev/null)
git_commit=$(git rev-parse --short HEAD 2> /dev/null || true)

echo "+ Building Synse Server Images +"
echo "--------------------------------"
echo " image: ${image}"
echo " version: ${version}"
echo " tags: ${tags}"
echo " date: ${build_date}"
echo " commit: ${git_commit}"
echo "--------------------------------"


for tag in ${tags}; do
echo -e "${green}tag: ${image}:${tag}[-slim]${nc}"

# build the SLIM version of the tag
docker build -f dockerfile/synse.dockerfile \
--build-arg BUILD_DATE=${build_date} \
--build-arg BUILD_VERSION=${version} \
--build-arg VCS_REF=${git_commit} \
--target=slim \
-t "${image}:${tag}-slim" .

# build the FULL verison of the tag
docker build -f dockerfile/synse.dockerfile \
--build-arg BUILD_DATE=${build_date} \
--build-arg BUILD_VERSION=${version} \
--build-arg VCS_REF=${git_commit} \
-t "${image}:${tag}" .
done
2 changes: 1 addition & 1 deletion compose/synse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
image: vaporio/synse-server:local
build:
context: ..
dockerfile: dockerfile/release.dockerfile
dockerfile: dockerfile/synse.dockerfile
environment:
- SYNSE_LOGGING=debug
ports:
Expand Down
51 changes: 0 additions & 51 deletions dockerfile/release.dockerfile

This file was deleted.

46 changes: 0 additions & 46 deletions dockerfile/slim.dockerfile

This file was deleted.

95 changes: 95 additions & 0 deletions dockerfile/synse.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# release.dockerfile
#
# The Dockerfile for the release build of Synse Server. This
# Dockerfile has multiple stages:
# * base: defines the base image and adds image metadata
# * builder: build synse server package dependencies
# * slim: a synse server build without emulator
# * full: a synse server build with emulator
#

#
# BASE
#
FROM python:3.6-alpine as base
LABEL maintainer="Vapor IO"


#
# BUILDER
#
FROM python:3.6-alpine as builder

RUN mkdir /build
WORKDIR /build

COPY requirements.txt /requirements.txt

RUN set -e -x \
&& apk --update --no-cache --virtual .build-dep add \
build-base \
&& pip install --upgrade pip \
&& pip install --prefix=/build -r /requirements.txt --no-warn-script-location \
&& rm -rf /root/.cache \
&& apk del .build-dep


#
# SLIM
#
FROM base as slim
COPY --from=builder /build /usr/local

RUN set -e -x \
&& apk --update --no-cache add \
bash libstdc++ ca-certificates tini

# Set image metadata (see: http://label-schema.org/rc1/)
ARG BUILD_VERSION
ARG BUILD_DATE
ARG VCS_REF

LABEL org.label-schema.schema-version="1.0" \
org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="vaporio/synse-server" \
org.label-schema.vcs-url="https://github.com/vapor-ware/synse-server" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vendor="Vapor IO" \
org.label-schema.version=$BUILD_VERSION

COPY . /synse
WORKDIR /synse

# Create directories for plugin sockets and configuration, then
# install Synse Server as a python package
RUN mkdir -p /tmp/synse/procs \
&& mkdir -p /synse/config \
&& pip install . \
&& rm -rf /root/.cache

ENTRYPOINT ["/sbin/tini", "--", "bin/synse.sh"]


#
# FULL
#
FROM slim as full

# Emulator installation script
COPY bin/install_emulator.sh tmp/install_emulator.sh

# Environment variables for built-in emulator configuration.
ENV PLUGIN_DEVICE_CONFIG="/synse/emulator/config/device" \
PLUGIN_CONFIG="/synse/emulator"

# The linux_amd64 emulator binary is built with libc, not muslc, it
# will not work here. The musl and glibc so files are compatible, so
# we can make a symlink to fix the missing dependency:
# https://stackoverflow.com/a/35613430
RUN set -e -x \
&& mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 \
&& apk --update --no-cache --virtual .build-dep add \
curl jq \
&& EMULATOR_OUT=/usr/local/bin/emulator ./tmp/install_emulator.sh \
&& apk del .build-dep
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
'': ['LICENSE'],
'synse': ['locale/*/LC_MESSAGES/*.mo']
},
python_requires='==3.6',
python_requires='>=3.6',
install_requires=[
'aiocache',
'bison>=0.0.6',
Expand Down
2 changes: 1 addition & 1 deletion synse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

__title__ = 'synse'
__version__ = '2.1.0'
__version__ = '2.1.1'
__description__ = 'Synse Server'
__author__ = 'Vapor IO'
__author_email__ = 'vapor@vapor.io'
Expand Down

0 comments on commit 54853b3

Please sign in to comment.