LoginSignup
7
1

More than 5 years have passed since last update.

Jenkins 入り イメージを作る Dockerfile を書いてみた

Last updated at Posted at 2018-12-14

この記事は、富士通ソーシアルサイエンスラボラトリ Advent Calendar 2018 の 25 日目の記事です。

をぉっ、トリだ!

うそ、まぢ? まじはしりきっちゃった?

まずはこのカレンダを作ってくれた @moriyatakashijp さんにお礼申し上げます。

書き手になってくれた皆っ! 僕らはしりきったぜぇい!!

はじめに

閑話休題。

@GORO_Neko です。ご存知の方ご無沙汰してます。初めての方お初にお目にかかります。

えーっと、先にお断りをば一言。

自分、別段構築作業のスペシャリストでも何でもありません(何か別の要件で必要になる環境やらミドルウェアやらを しぶしぶと 突っ込んでるだけだよなぁ(^^; )。

以下は、自分が所属する会社の意向を反映したものでもスタンスを示すものでもなく、単なる一個人の趣味の活動から産まれた記述です。

Jenkins を使って CI したいんだけど

Jenkins を構築したいわけじゃない。
ましてや、Jenkins 動かすために必要なだけの Java の実行環境をセットアップするとか、けっして喜んでやってるわけじゃない(そもそも Jenkins で CI させる予定のアプリケーションを書くために使っている言語は not Java だ!)。

ここのところ仕事どころかプライベートな都合からも、何度となくこんな思いをさせられました。

いやな思いをしたままほっておくと、気持ちがはれません。
てなわけで、改善活動(?)を行ってみました。

要は何度も好きでもない作業を 人力 でやる羽目になったのがイカンかったわけですよ。

どうせ実行しなきゃならない各作業は毎回ほとんど変わらないんだから、これ 機械 にでもやらせりゃよかったんですよね。
所謂人的ミス、入力コマンドを間違えて頭を抱えるなんてことも起きなくなるだろうし。

おっしゃ、自動化だ!

と言うわけで、Jenkins 入りコンテナイメージを作る Dockerfile を書いてみました。

以下の Dockerfile は proxy が存在する環境下で、そのproxy 越しに資源をインターネットから得つつ、Jenkins が稼働しているコンテナイメージを、プログラマブルに構築します。

【想定する proxy 】

項目
URL http://proxy.com
Port 番号 8080
ユーザ ID hoge@jp.com
パスワード fuga

proxy の存在しない場所で、コンテナイメージを作る場合は、以下の部分を削除するなりコメント化するなりすれば OK です。


# Set environment variables and config files for accessing the Internet via F net's Proxy.
  :
# for http/https/ftp
  :
# for yum
  :
# for curl
  :

今回書いた Dockerfile

Dockerfile を書く際、よくはまるポイントを 2 点メモっておきます。

  1. コンテナイメージ作成時に参照したい環境変数は ENV コマンドで指定しろ

  2. コンテナイメージ作成時に特定ディレクトリ下で作業をしたい場合 "RUN cd ..." ではなく WORKDIR コマンドで指定しろ

あとは、コンテナイメージ作成用に実行が必要なコマンドを RUN の後ろに、コンテナ稼働時に実行が必要なコマンドを CMD の後ろにそれぞれ記述すれば、何とかなります( … たぶん)。

手を抜いて、ガンガン RUN 書いてます。
出来上がったイメージ、メッチャ積層状態です。
気になる方は、"docker build" の積層された各層をがっちゃんこする機能等を、適宜適応してください。

この辺りをウマク何とかしてくれるらしい

docker-slim

とか使うと良いんじゃないかなと思っています。


#
# Specify the image (CentOS 7) to be the base of the container.
From centos:centos7

# Specify working directory (/root).
WORKDIR /root

# Set environment variables and config files for accessing the Internet via F net's Proxy.

# for http/https/ftp
RUN echo "export FTP_PROXY=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile
RUN echo "export ftp_proxy=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile
RUN echo "export HTTP_PROXY=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile
RUN echo "export http_proxy=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile
RUN echo "export HTTPS_PROXY=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile
RUN echo "export https_proxy=http://hoge%40jp.com:fuga@proxy.com:8080" >> .bash_profile

ENV FTP_PROXY http://hoge%40jp.com:fuga@proxy.com:8080
ENV ftp_proxy http://hoge%40jp.com:fuga@proxy.com:8080
ENV HTTP_PROXY http://hoge%40jp.com:fuga@proxy.com:8080
ENV http_proxy http://hoge%40jp.com:fuga@proxy.com:8080
ENV HTTPS_PROXY http://hoge%40jp.com:fuga@proxy.com:8080
ENV https_proxy http://hoge%40jp.com:fuga@proxy.com:8080

# for yum

RUN echo "proxy=http://proxy.com:8080" >> /etc/yum.conf
RUN echo "proxy_username=hoge@jp.com" >> /etc/yum.conf
RUN echo "proxy_password=fuga" >> /etc/yum.conf

# for curl

RUN echo 'proxy-user = "hoge@jp.com:fuga"' > .curlrc
RUN echo 'proxy = "http://proxy.com:8080"' >> .curlrc

# Update applications and others.
RUN yum update -y
RUN yum upgrade -y

# Set the root user's password (pw = root).
RUN echo 'root:root' | chpasswd

# Install sshd.
RUN yum install openssh-server -y
RUN mkdir /var/run/sshd

# Change the setting so that the root user can SSH login.
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

# Install network tools (ip, ifconfig).
RUN yum install net-tools -y
RUN yum install iproute -y

# Install working tools (vim, curl).
RUN yum install vim -y
RUN yum install curl -y

# Setup Jenkins.

# Install Java(Java8).
RUN yum install java-1.8.0-openjdk -y

# install "/etc/rc.d/init.d/functions"
RUN yum install initscripts -y

# Install Jenkins.
WORKDIR /etc/yum.repos.d
RUN curl -O http://pkg.jenkins-ci.org/redhat/jenkins.repo
RUN rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
RUN yum install --enablerepo=jenkins jenkins -y

# Run Jenkins.
# CMD chkconfig jenkins on
# CMD systemctl start jenkins

# Display initial password for Jenkins
# CMD cat /var/lib/jenkins/secrets/initialAdminPassword

Dockerfile から得られたコンテナイメージを使って作成したコンテナには sshd も稼働していますので、ssh でログイン( uid=root、password=root )できます。

では、また。

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