Skip to content

Commit 1bb9788

Browse files
authored
Add Dockerfile linter (#980)
1 parent 1a239d2 commit 1bb9788

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ jobs:
6262
repo-token: ${{ secrets.GITHUB_TOKEN }}
6363
- name: Run Code Analysis
6464
run: bundle exec rails code:analysis
65+
- name: Run linter on production Dockerfile
66+
uses: hadolint/hadolint-action@v3.1.0
67+
with:
68+
dockerfile: "Dockerfile"
69+
70+
- name: Run linter on dev Dockerfile
71+
uses: hadolint/hadolint-action@v3.1.0
72+
with:
73+
dockerfile: "Dockerfile.dev"
6574
tests:
6675
name: Tests
6776
runs-on: ubuntu-latest

.hadolint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ignored:
2+
- DL3059 # Multiple consecutive `RUN` instructions. We ignore this because we want to cache as many RUN stages as possible.
3+
- DL3008 # Pin versions in apt-get install.
4+
- DL3028 # Pin versions in gem install.

.tool-versions

Lines changed: 0 additions & 1 deletion
This file was deleted.

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ ENV NODE_ENV=production
2323
FROM base AS builder
2424

2525
RUN apt-get update -qq && \
26-
apt-get install -y build-essential libssl-dev libpq-dev git libsasl2-dev libyaml-dev curl && \
26+
apt-get install --no-install-recommends -y build-essential libssl-dev libpq-dev git libsasl2-dev libyaml-dev curl && \
2727
rm -rf /var/lib/apt/lists/*
2828

2929
# Copy node binaries from node image.
3030
COPY --from=node /usr/local /usr/local
3131
COPY --from=node /opt /opt
3232

3333
# Create app directory.
34-
RUN mkdir -p $APP_HOME
34+
RUN mkdir -p "${APP_HOME}"
3535

3636
# Setup work directory.
3737
WORKDIR $APP_HOME
@@ -42,7 +42,7 @@ COPY --link Gemfile Gemfile.lock package.json yarn.lock .yarnrc.yml .ruby-versio
4242
RUN corepack enable
4343
RUN gem install bundler && bundle install -j 4 && yarn install --immutable && \
4444
bundle exec bootsnap precompile --gemfile && \
45-
rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git
45+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
4646

4747
# Copy application code
4848
COPY --link . .
@@ -59,10 +59,10 @@ FROM base
5959
# Install packages needed for deployment
6060
RUN apt-get update -qq && \
6161
apt-get install --no-install-recommends -y curl libpq-dev libvips libjemalloc2 libyaml-dev && \
62-
apt-get clean
62+
apt-get clean && rm -rf /var/lib/apt/lists/*
6363

6464
# Create app directory.
65-
RUN mkdir -p $APP_HOME
65+
RUN mkdir -p "${APP_HOME}"
6666

6767
# Setup work directory.
6868
WORKDIR $APP_HOME

Dockerfile.dev

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ FROM node:$NODE_VERSION AS node
88
FROM ruby:${RUBY_VERSION}-slim
99

1010
RUN apt-get update -qq && \
11-
apt-get install -y build-essential libssl-dev libpq-dev vim git libsasl2-dev libyaml-dev curl && \
11+
apt-get install --no-install-recommends -y build-essential libssl-dev libpq-dev vim git libsasl2-dev libyaml-dev curl && \
1212
rm -rf /var/lib/apt/lists/*
1313

1414
# Copy node binaries from node image.
@@ -32,7 +32,7 @@ RUN gem install foreman bundler
3232
COPY --link package.json yarn.lock .yarnrc.yml ./
3333

3434
RUN corepack enable
35-
RUN yarn install --immutable
35+
RUN yarn install --immutable && yarn cache clean
3636

3737
COPY --link Gemfile Gemfile.lock .ruby-version ./
3838
RUN bundle install -j 4

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ With `bundle exec rails code:analysis` you can run the code analysis tool, you c
134134
- [Rails Best Practices](https://github.com/flyerhzm/rails_best_practices#custom-configuration) Edit `config/rails_best_practices.yml`
135135
- [Brakeman](https://github.com/presidentbeef/brakeman) Run `brakeman -I` to generate `config/brakeman.ignore`
136136

137+
## More linters
138+
- [Hadolint](https://github.com/hadolint/hadolint) Install with `brew install hadolint` and run `hadolint Dockerfile*`. Edit `.hadolint.yml` to omit additional rules.
139+
137140
## Impersonation
138141

139142
The `rails_api_base` incorporates a user impersonation feature, allowing `AdminUser`s to assume the identity of other `User`s. This feature is disabled by default.

0 commit comments

Comments
 (0)