LoginSignup
1
0

More than 1 year has passed since last update.

alpine ベースの Dockerfile で git と git-secrets をインストールする

Last updated at Posted at 2022-09-24

1. Dockerfile

次の記載で、alpine ベースの Dockerfile で git と git-secrets をインストールすることができました。
もっと、適切な方法があるかもしれませんが、同じようなことで躓いた方の参考となればと思い、残しておきます。

Dockerfile
FROM node:16-alpine3.16
RUN apk update && \
    apk add bash git make && \
    apk add --upgrade grep
RUN git clone https://github.com/awslabs/git-secrets /home/alpine/git-secrets
WORKDIR /home/alpine/git-secrets
RUN make && make install
# 以下略

参考
moduscreate/alpine-git-secrets

実際の Dockerfile の全体や docker-compose.yml の記載については、こちらの記事で書いています。
以下、参考までに、作業中に発生したエラーの一覧も残しておきます。

2. Error example

作業中に出たエラーの一覧です。
エラーメッセージと対応方法を、書いておきます。

2-1. env: can't execute 'bash': No such file or directory

単に git のみインストールした場合です。

Dockerfile 該当部分
RUN apk update && apk add git

これで git commit をすると「bash が実行できない」とのエラーが出ます。

terminal
# git commit -m "test commit"
env: can't execute 'bash': No such file or directory

これは RUNapk add bash を追加すれば解決します。

参考
Docker Build Error: env: can't execute 'bash': No such file or directory
UbuntuからAlpineにイメージを移行して容量を減らす例

2-2. git: 'secrets' is not a git command. See 'git --help'.

次に git と bash のみインストールした場合です。

Dockerfile 該当部分
RUN apk update && apk add git bash

これで git commit をすると「'secrets' は git コマンドではありません」とのエラーが出ます。

terminal
# git commit -m "test commit"
git: 'secrets' is not a git command. See 'git --help'.

これは、git-secrets がインストールされていないために生じるエラーのため、git-secrets をインストールすれば解決します。

なお、次のコマンドで、commit 時に git-secrets を呼び出さないようにすることも可能です(良い解決とは言えませんが)。

# rm -r .git/hooks

参考
git-secrets削除後のエラーの解決策

2-3. grep: unrecognized option: d

次に git, bash, git-secrets をインストールした場合です。

Dockerfile 該当部分
RUN apk update && \
    apk add bash git make
RUN git clone https://github.com/awslabs/git-secrets /home/alpine/git-secrets
WORKDIR /home/alpine/git-secrets
RUN make && make install

これで git commit をすると「grep で d オプションが認識されない」とのエラーが出ます。

terminal
# git commit -m "test commit"
grep: unrecognized option: d
BusyBox v1.35.0 (2022-08-01 15:14:44 UTC) multi-call binary.

Usage: grep [-HhnlLoqvsrRiwFE] [-m N] [-A|B|C N] { PATTERN | -e PATTERN... | -f FILE... } [FILE]...

Search for PATTERN in FILEs (or stdin)

        -H      Add 'filename:' prefix
        -h      Do not add 'filename:' prefix
        -n      Add 'line_no:' prefix
        -l      Show only names of files that match
        -L      Show only names of files that don't match
        -c      Show only count of matching lines
        -o      Show only the matching part of line
        -q      Quiet. Return 0 if PATTERN is found, 1 otherwise
        -v      Select non-matching lines
        -s      Suppress open and read errors
        -r      Recurse
        -R      Recurse and dereference symlinks
        -i      Ignore case
        -w      Match whole words only
        -x      Match whole lines only
        -F      PATTERN is a literal (not regexp)
        -E      PATTERN is an extended regexp
        -m N    Match up to N times per file
        -A N    Print N lines of trailing context
        -B N    Print N lines of leading context
        -C N    Same as '-A N -B N'
        -e PTRN Pattern to match
        -f FILE Read pattern from file

これは RUNapk add --upgrade grep を追加して grep をアップグレードすれば解決します。

参考
[RFC] Make it work with Alpine Linux / BusyBox grep
[alpine] sh: make: not found

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0