Docker: made dockerfiles use a single stage build process.

This commit is contained in:
Konstantin Pavlov
2023-02-13 17:04:24 -08:00
parent eb47174875
commit 054518bb36
2 changed files with 22 additions and 35 deletions

View File

@@ -15,58 +15,49 @@ VERSION_minimal ?=
CONTAINER_minimal ?= debian:bullseye-slim CONTAINER_minimal ?= debian:bullseye-slim
CONFIGURE_minimal ?= CONFIGURE_minimal ?=
INSTALL_minimal ?= version INSTALL_minimal ?= version
define COPY_minimal RUN_minimal ?= /bin/true
endef
VERSION_go ?= 1.19 VERSION_go ?= 1.19
CONTAINER_go ?= golang:$(VERSION_go) CONTAINER_go ?= golang:$(VERSION_go)
CONFIGURE_go ?= go --go-path=$$GOPATH CONFIGURE_go ?= go --go-path=$$GOPATH
INSTALL_go ?= go-install-src libunit-install INSTALL_go ?= go-install-src libunit-install
define COPY_go RUN_go ?= /bin/true
COPY --from=BUILDER /usr/lib/\*-linux-gnu/libunit.a /tmp/\n\$
COPY --from=BUILDER /usr/include/nxt_* /usr/include/\n\$
COPY --from=BUILDER /go/src/ /go/src/
endef
VERSION_jsc ?= 11 VERSION_jsc ?= 11
CONTAINER_jsc ?= eclipse-temurin:$(VERSION_jsc)-jdk CONTAINER_jsc ?= eclipse-temurin:$(VERSION_jsc)-jdk
CONFIGURE_jsc ?= java --jars=/usr/share/unit-jsc-common/ CONFIGURE_jsc ?= java --jars=/usr/share/unit-jsc-common/
INSTALL_jsc ?= java-shared-install java-install INSTALL_jsc ?= java-shared-install java-install
COPY_jsc = COPY --from=BUILDER /usr/share/unit-jsc-common/ /usr/share/unit-jsc-common/ RUN_jsc ?= /bin/true
VERSION_node ?= 18 VERSION_node ?= 18
CONTAINER_node ?= node:$(VERSION_node) CONTAINER_node ?= node:$(VERSION_node)
CONFIGURE_node ?= nodejs --node-gyp=/usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp CONFIGURE_node ?= nodejs --node-gyp=/usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp
INSTALL_node ?= node node-install libunit-install INSTALL_node ?= node node-install libunit-install
define COPY_node RUN_node ?= /bin/true
COPY --from=BUILDER /usr/lib/\*-linux-gnu/libunit.a /tmp/\n\$
COPY --from=BUILDER /usr/include/nxt_* /usr/include/\n\$
COPY --from=BUILDER /usr/local/lib/node_modules/unit-http/ /usr/local/lib/node_modules/unit-http/
endef
VERSION_perl ?= 5.36 VERSION_perl ?= 5.36
CONTAINER_perl ?= perl:$(VERSION_perl) CONTAINER_perl ?= perl:$(VERSION_perl)
CONFIGURE_perl ?= perl CONFIGURE_perl ?= perl
INSTALL_perl ?= perl-install INSTALL_perl ?= perl-install
COPY_perl = RUN_perl ?= /bin/true
VERSION_php ?= 8.1 VERSION_php ?= 8.1
CONTAINER_php ?= php:$(VERSION_php)-cli CONTAINER_php ?= php:$(VERSION_php)-cli
CONFIGURE_php ?= php CONFIGURE_php ?= php
INSTALL_php ?= php-install INSTALL_php ?= php-install
COPY_php = RUN ldconfig RUN_php ?= ldconfig
VERSION_python ?= 3.11 VERSION_python ?= 3.11
CONTAINER_python ?= python:$(VERSION_python) CONTAINER_python ?= python:$(VERSION_python)
CONFIGURE_python ?= python --config=/usr/local/bin/python3-config CONFIGURE_python ?= python --config=/usr/local/bin/python3-config
INSTALL_python ?= python3-install INSTALL_python ?= python3-install
COPY_python = RUN_python ?= /bin/true
VERSION_ruby ?= 3.1 VERSION_ruby ?= 3.1
CONTAINER_ruby ?= ruby:$(VERSION_ruby) CONTAINER_ruby ?= ruby:$(VERSION_ruby)
CONFIGURE_ruby ?= ruby CONFIGURE_ruby ?= ruby
INSTALL_ruby ?= ruby-install INSTALL_ruby ?= ruby-install
COPY_ruby = RUN gem install rack RUN_ruby ?= gem install rack
default: default:
@echo "valid targets: all build dockerfiles clean" @echo "valid targets: all build dockerfiles clean"
@@ -78,14 +69,14 @@ modname = $(shell echo $1 | /usr/bin/tr -d '.01234567890-')
dockerfiles: $(addprefix Dockerfile., $(MODVERSIONS)) dockerfiles: $(addprefix Dockerfile., $(MODVERSIONS))
build: $(addprefix build-,$(MODVERSIONS)) build: $(addprefix build-,$(MODVERSIONS))
Dockerfile.%: ../../version Dockerfile.%: ../../version template.Dockerfile
@echo "===> Building $@" @echo "===> Building $@"
cat template.Dockerfile | sed \ cat template.Dockerfile | sed \
-e 's,@@VERSION@@,$(VERSION),g' \ -e 's,@@VERSION@@,$(VERSION),g' \
-e 's,@@CONTAINER@@,$(CONTAINER_$(call modname, $*)),g' \ -e 's,@@CONTAINER@@,$(CONTAINER_$(call modname, $*)),g' \
-e 's,@@CONFIGURE@@,$(CONFIGURE_$(call modname, $*)),g' \ -e 's,@@CONFIGURE@@,$(CONFIGURE_$(call modname, $*)),g' \
-e 's,@@INSTALL@@,$(INSTALL_$(call modname, $*)),g' \ -e 's,@@INSTALL@@,$(INSTALL_$(call modname, $*)),g' \
-e 's,@@COPY@@,$(COPY_$(call modname, $*)),g' \ -e 's,@@RUN@@,$(RUN_$(call modname, $*)),g' \
> $@ > $@
build-%: Dockerfile.% build-%: Dockerfile.%

View File

@@ -3,6 +3,7 @@ FROM @@CONTAINER@@ as BUILDER
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>" LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
RUN set -ex \ RUN set -ex \
&& savedAptMark="$(apt-mark showmanual)" \
&& apt-get update \ && apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y ca-certificates mercurial build-essential libssl-dev libpcre2-dev \ && apt-get install --no-install-recommends --no-install-suggests -y ca-certificates mercurial build-essential libssl-dev libpcre2-dev \
&& mkdir -p /usr/lib/unit/modules /usr/lib/unit/debug-modules \ && mkdir -p /usr/lib/unit/modules /usr/lib/unit/debug-modules \
@@ -38,20 +39,12 @@ RUN set -ex \
&& ./configure $CONFIGURE_ARGS --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/modules \ && ./configure $CONFIGURE_ARGS --cc-opt="$CC_OPT" --modulesdir=/usr/lib/unit/modules \
&& ./configure @@CONFIGURE@@ \ && ./configure @@CONFIGURE@@ \
&& make -j $NCPU @@INSTALL@@ \ && make -j $NCPU @@INSTALL@@ \
&& ldd /usr/sbin/unitd | awk '/=>/{print $(NF-1)}' | while read n; do dpkg-query -S $n; done | sed 's/^\([^:]\+\):.*$/\1/' | sort | uniq > /requirements.apt && for f in /usr/sbin/unitd /usr/lib/unit/modules/*.unit.so; do \
ldd $f | awk '/=>/{print $(NF-1)}' | while read n; do dpkg-query -S $n; done | sed 's/^\([^:]\+\):.*$/\1/' | sort | uniq >> /requirements.apt; \
FROM @@CONTAINER@@ done \
COPY docker-entrypoint.sh /usr/local/bin/ && apt-mark showmanual | xargs apt-mark auto > /dev/null \
COPY --from=BUILDER /usr/sbin/unitd /usr/sbin/unitd && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; } \
COPY --from=BUILDER /usr/sbin/unitd-debug /usr/sbin/unitd-debug && @@RUN@@ \
COPY --from=BUILDER /usr/lib/unit/ /usr/lib/unit/
COPY --from=BUILDER /requirements.apt /requirements.apt
@@COPY@@
RUN set -x \
&& if [ -f "/tmp/libunit.a" ]; then \
mv /tmp/libunit.a /usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/libunit.a; \
rm -f /tmp/libunit.a; \
fi \
&& mkdir -p /var/lib/unit/ \ && mkdir -p /var/lib/unit/ \
&& mkdir /docker-entrypoint.d/ \ && mkdir /docker-entrypoint.d/ \
&& addgroup --system unit \ && addgroup --system unit \
@@ -64,12 +57,15 @@ RUN set -x \
--gecos "unit user" \ --gecos "unit user" \
--shell /bin/false \ --shell /bin/false \
unit \ unit \
&& apt update \ && apt-get update \
&& apt --no-install-recommends --no-install-suggests -y install curl $(cat /requirements.apt) \ && apt-get --no-install-recommends --no-install-suggests -y install curl $(cat /requirements.apt) \
&& apt-get purge -y --auto-remove \
&& apt-get clean && rm -rf /var/lib/apt/lists/* \ && apt-get clean && rm -rf /var/lib/apt/lists/* \
&& rm -f /requirements.apt \ && rm -f /requirements.apt \
&& ln -sf /dev/stdout /var/log/unit.log && ln -sf /dev/stdout /var/log/unit.log
COPY docker-entrypoint.sh /usr/local/bin/
STOPSIGNAL SIGTERM STOPSIGNAL SIGTERM
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]