Skip to content

Commit

Permalink
Refactor Dockerfile to use multi-stage builds (#2004)
Browse files Browse the repository at this point in the history
Refactored Dockerfile to use multi-stage builds for reduction image size.

The installation of the packages required for the build and the build process are carried out at the build stage. The built binaries are then placed in the final image.

This changes has reduces the image size about 1/3.
```
$ docker image ls apache/age
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
apache/age   tmp       5ffd0b539a88   22 minutes ago   458MB <-- New image
apache/age   latest    fb44b5789198   2 months ago     1.5GB <-- Original image
```
  • Loading branch information
shinyaaa committed Aug 17, 2024
1 parent 57b67c0 commit f80f55f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 22 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
# limitations under the License.
#

FROM postgres:15
# Build stage: Install necessary development tools for compilation and installation
FROM postgres:15 AS build

RUN apt-get update \
&& apt-get install -y --no-install-recommends --no-install-suggests \
bison \
build-essential \
flex \
postgresql-server-dev-15 \
locales
postgresql-server-dev-15

ENV LANG=en_US.UTF-8
ENV LC_COLLATE=en_US.UTF-8
Expand All @@ -40,6 +40,25 @@ WORKDIR /age

RUN make && make install


# Final stage: Create a final image by copying the files created in the build stage
FROM postgres:15

RUN apt-get update \
&& apt-get install -y --no-install-recommends --no-install-suggests \
locales

RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen \
&& update-locale LANG=en_US.UTF-8

ENV LANG=en_US.UTF-8
ENV LC_COLLATE=en_US.UTF-8
ENV LC_CTYPE=en_US.UTF-8

COPY --from=build /usr/lib/postgresql/15/lib/age.so /usr/lib/postgresql/15/lib/
COPY --from=build /usr/share/postgresql/15/extension/age--1.5.0.sql /usr/share/postgresql/15/extension/
COPY --from=build /usr/share/postgresql/15/extension/age.control /usr/share/postgresql/15/extension/
COPY docker/docker-entrypoint-initdb.d/00-create-extension-age.sql /docker-entrypoint-initdb.d/00-create-extension-age.sql

CMD ["postgres", "-c", "shared_preload_libraries=age"]
4 changes: 2 additions & 2 deletions docker/hooks/build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

docker buildx create --name multiarch --use --platform linux/amd64,linux/arm64/v8
docker buildx build ../ -t $IMAGE_NAME -f Dockerfile --platform linux/amd64,linux/arm64/v8 --push
docker buildx create --name multiarch --use --platform linux/amd64
docker buildx build ../ -t shinya11kato/age:pg15 -f Dockerfile --platform linux/amd64 --push --no-cache

0 comments on commit f80f55f

Please sign in to comment.