LoginSignup
1
1

More than 1 year has passed since last update.

Dockerfileで完結 CentOS8でlinuxbrewを使う方法

Last updated at Posted at 2021-06-11

概要

linuxbrew使っていますか?
Macユーザーはお馴染みのbrewコマンドがlinuxでも使えます。

公式 Homebrew on Linux

他の記事では、せっかくdockerを使っているのに、brewのインストールは手動で行う、、、というものをよく見かけます。

そんなdockerはいやだ!!(笑)ということで、今回は、Dockerfile内の記述でbrewコマンドが使えるようにしてみましょう。

環境

  • maxOS BigSur 11.2.3
  • Docker version 20.10.6
  • docker-compose version 1.29.1

構成

.
├── docker
│   └── app
│       └── Dockerfile
└── docker-compose.yml

手順

1. docker-compose.yml

今回の構成ではいらないと言えばいらないけれども、dockerfile単体で使うことはあまりないので(私はw)、いつも通りdocker-compose.ymlにまとめる方向です。

docker-compose.yml
version: "3.9"
services:
  app:
    build:
      context: ./docker/app
    tty: true

ちなみに、tty: trueは、設定していないと起動後に終了してしまうためです。

2. Dockerfileを作成

さあ、お待ちかねのDockerfileです。

FROM centos:centos8

SHELL ["/bin/bash", "-c"]

RUN dnf -y update && \
    dnf install -y procps-ng curl file git \
      && dnf clean all
RUN dnf -y groupinstall 'Development Tools'

# linuxbrew
RUN git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew && \
    mkdir ~/.linuxbrew/bin && \
    ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin
ENV PATH $PATH:~/.linuxbrew/bin

解説

イメージ
FROM centos:centos8

イメージは、CentOS8を利用します。

シェルはBash

SHELL ["/bin/bash", "-c"]で、bashを利用するように指定しています。

インストールのためのパッケージ準備
RUN dnf -y update && \
    dnf install -y procps-ng curl file git \
      && dnf clean all
      && dnf clean all
RUN dnf -y groupinstall 'Development Tools'

Fedora, CentOS, or Red Hatにある通りlinuxbrewをインストールするために必要なprocps-ng curl file gitDevelopment Toolsをインストールします。

linuxbrewのインストール
RUN git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew && \
    mkdir ~/.linuxbrew/bin && \
    ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin
ENV PATH $PATH:~/.linuxbrew/bin

通常のインストールだと、対話式でコマンド実行をしなければならずDockerfile内の記述ではうまくいかないため、
Alternative Installationを参考にしています。
ただし、パスを通す方法が公式とは異なります。
公式の方法では、eval $(~/.linuxbrew/bin/brew shellenv)となっていますが、Dockerfile内でパスを通すにはENVを利用する必要があるため、ENV PATH $PATH:~/.linuxbrew/binとしています。

3. 確認

起動

docker-compose up -d

コンテナにインスペクション

docker-compose exec app bash

brewコマンド実行

brew -v

以下のようにバージョン情報が出力されたらOKです。

Homebrew 3.1.11-45-g33e71c9
Homebrew/homebrew-core N/A

停止

docker-compose down

最後に

Dockerを使うなら、Dockerで完結したいですよね!
とはいえ、今回linuxbrewをDockerfileで完結させるために1日かかってしまったのは辛いところです(汗)
今回だけで見れば、手動でインストールしてもよかった感は否めません。
今後、docker×linuxbrewを使う場面が多々あることを祈ります(笑)

参考

1
1
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
1