LoginSignup
28

More than 5 years have passed since last update.

docker imageを作る際に"cd"できなかった

Posted at

下記ドキュメント通りにNagios core&pluginをインストールする手順を
そのままdocker imageに落とし込んでみようとして少しハマった話。
http://assets.nagios.com/downloads/nagioscore/docs/Installing_Nagios_Core_From_Source.pdf

内容はここに書いてある通りのことです
http://stackoverflow.com/questions/17891981/docker-run-cd-does-not-work-as-expected

環境

使用環境 バージョン
Mac OS X 10.9.2
docker 0.8.1

cdが効かない

image例


RUN cd /tmp
RUN wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.4.tar.gz
RUN tar zxvf nagios-4.0.4.tar.gz
RUN cd /tmp/nagios-4.0.4
RUN ./configure --with-command-group=nagcmd

よくあるtarballをダウンロード➡️解凍➡️configure➡️makeする流れ。

今回は/tmpにダウンロードして解凍、フォルダへ移動しconfigureすると考えていた。

しかし、docker buildでimageを作成する際、一命令毎にコンテナが作成される。
次の命令は前の命令のコンテナを基にした新しいコンテナで実行されるため、
カレントディレクトリは常に"/"となる。

この状態でbuildすると下記のようになる。

コマンド コンテナの挙動
RUN cd /tmp "cd /tmp"を実行
RUN wget http://~/nagios-4.0.4.tar.gz nagios-4.0.4.tar.gzを"/"へダウンロード
RUN tar zxvf nagios-4.0.4.tar.gz "/nagios-4.0.4.tar.gz"を解凍
RUN cd /tmp/nagios-4.0.4 "cd /tmp/nagios-4.0.4"を実行しようとしたが、指定場所にフォルダがないためエラー ⬅️ここでビルド失敗
RUN ./configure --with-command-group=nagcmd

解決策

&&でつないで一命令で処理する

RUN cd /tmp     && \
    wget http://~/nagios-4.0.4.tar.gz       && \
    tar zxvf nagios-4.0.4.tar.gz            && \
    cd /tmp/nagios-4.0.4                    && \
    ./configure --with-command-group=nagcmd

まとめ

dockerでRUNできる回数は127回まで(version0.7.2以降)なので、
細かく分けるより、一回のRUNでまとめて処理した方が良さそう。
docker imageをビルドする際はコンテナが作成されることを意識するというお話でした。

完成したイメージ

FROM centos

# setup remi repository
RUN wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
RUN wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN curl -O http://rpms.famillecollet.com/RPM-GPG-KEY-remi; rpm --import RPM-GPG-KEY-remi
RUN rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
RUN yum -y update && yum -y upgrade

# setup tools
RUN yum -y groupinstall --enablerepo=epel,remi "Development Tools"
RUN yum install -y wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp

# Preparing to Nagios Install
RUN cd /tmp     && \
    wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.4.tar.gz && \
    wget http://nagios-plugins.org/download/nagios-plugins-2.0.tar.gz   && \
    tar zxvf nagios-4.0.4.tar.gz        && \
    tar zxvf nagios-plugins-2.0.tar.gz
RUN useradd nagios
RUN groupadd nagcmd
RUN usermod -a -G nagcmd nagios

# Nagios Install
RUN cd /tmp/nagios-4.0.4 && \
    ./configure --with-command-group=nagcmd && \
    make all                    && \
    make install                && \
    make install-init           && \
    make install-config         && \
    make install-commandmode    && \
    make install-webconf
RUN cp -R /tmp/nagios-4.0.4/contrib/eventhandlers/ /usr/local/nagios/libexec/
RUN chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

# set nagios web password
RUN htpasswd -b -c /usr/local/nagios/etc/htpasswd.users nagiosadmin nagios

# set plugin
RUN cd /tmp/nagios-plugins-2.0/     && \
    ./configure --with-nagios-user=nagios --with-nagios-group=nagios    && \
    make          && \
    make install

# service start
CMD ["service","httpd","start"]

# expose http port
EXPOSE 80

docker pullでダウンロード可

docker pull  gacharion/nagios4.0.4

※コンテナに入ってhttpd,nagiosをstartさせる必要があるので、supervisor化しさせないと・・・

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
28