forked from lipanski/ruby-dockerfile-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrails_with_assets_and_private_deps.Dockerfile
More file actions
70 lines (52 loc) · 2.4 KB
/
rails_with_assets_and_private_deps.Dockerfile
File metadata and controls
70 lines (52 loc) · 2.4 KB
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
# Start from a small, trusted base image with the version pinned down
FROM ruby:2.7.1-alpine AS base
# Install system dependencies required both at runtime and build time
# The image uses Postgres but you can swap it with mariadb-dev (for MySQL) or sqlite-dev
RUN apk add --update \
postgresql-dev \
tzdata \
nodejs \
yarn
# This stage will be responsible for installing gems and npm packages
FROM base AS dependencies
# The argument is required later, when installing private gems or npm packages
ARG GITHUB_TOKEN
# Install system dependencies required to build some Ruby gems (pg)
RUN apk add --update build-base
COPY Gemfile Gemfile.lock ./
# Don't install development or test dependencies
RUN bundle config set without "development test"
# Install gems (including private ones)
# This uses the GITHUB_TOKEN argument, which is also cleaned up in the same step
RUN git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/some-user".insteadOf git@github.com:some-user && \
git config --global --add url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/some-user".insteadOf ssh://git@github && \
bundle install --jobs=3 --retry=3 && \
rm ~/.gitconfig
COPY package.json yarn.lock ./
# Install npm packages (including private ones)
# This uses the GITHUB_TOKEN argument, which is also cleaned up in the same step
RUN git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/some-user".insteadOf git@github.com:some-user && \
git config --global --add url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/some-user".insteadOf ssh://git@github && \
yarn install --frozen-lockfile \
rm ~/.gitconfig
# We're back at the base stage
FROM base
# Create a non-root user to run the app and own app-specific files
RUN adduser -D app
# Switch to this user
USER app
# We'll install the app in this directory
WORKDIR /home/app
# Copy over gems from the dependencies stage
COPY --from=dependencies /usr/local/bundle/ /usr/local/bundle/
# Copy over npm packages from the dependencies stage
# Note that we have to use `--chown` here
COPY --chown=app --from=dependencies /node_modules/ node_modules/
# Finally, copy over the code
# This is where the .dockerignore file comes into play
# Note that we have to use `--chown` here
COPY --chown=app . ./
# Install assets
RUN RAILS_ENV=production SECRET_KEY_BASE=assets bundle exec rake assets:precompile
# Launch the server
CMD ["bundle", "exec", "rackup"]