-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile
110 lines (86 loc) · 3.35 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
ARG RUBY_VERSION=2.7.8
ARG DEBIAN_RELEASE=bullseye
ARG ARCH=amd64
# SHARED BUILD STAGE ==========================================================
# - setup common env variables (that are used in all build stages)
# - set workdir
# - update system gems
FROM --platform=$ARCH ruby:$RUBY_VERSION-slim-$DEBIAN_RELEASE as base
# ARG BUNDLER_VERSION=1.16.5
ENV APP_ROOT=/var/www/hanami_todo_api \
APP_USER=www-data
ENV LANG=C.UTF-8 \
GEM_HOME=$APP_ROOT/vendor/bundle \
BUNDLE_APP_CONFIG=.bundle \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=5
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_BIN $BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH
WORKDIR $APP_ROOT
# Update rubygems and install bundler
# RUN gem update --system; gem install bundler -v $BUNDLER_VERSION
# FIRST BUILD STAGE
# - install the gems
# - install the packages
FROM base AS intermediate
ARG DEBIAN_RELEASE
# Install packages necessary for compiling native extensions
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq && apt-get upgrade -yq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
libpq-dev \
git
COPY Gemfile* .ruby-version ./
RUN bundle i
# BUILDER STAGE - base stage for final builds =================================
# - installs packages
FROM base AS builder
ARG PG_MAJOR=16
ARG DEBIAN_RELEASE
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
gnupg \
wget
# Add PostgreSQL to sources list
RUN wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
echo "deb https://apt.postgresql.org/pub/repos/apt $DEBIAN_RELEASE-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# Update system
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && apt-get upgrade -yq && \
# Install required packages
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
# for postgres
libpq-dev \
postgresql-client-$PG_MAJOR \
# debug tools
vim
FROM builder AS dev
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
apt-get update -qq && apt-get upgrade -yq && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
libvips \
file \
git
FROM builder as prod
ARG RAILS_ENV
RUN chown -R $APP_USER:$APP_USER /var/www
COPY --chown=$APP_USER:$APP_USER . .
COPY --chown=$APP_USER:$APP_USER --from=intermediate $APP_ROOT $APP_ROOT
USER $APP_USER
RUN bootsnap precompile --gemfile app/ lib/