zoomzoomzoom

解決方法の共有に主眼を置くブログ・他雑談

dockerのinstall方法for Ubuntu16.04LTS

CTFの練習問題を解く前提として、dockerをインストールしてください、って軽く書いてあったんですが、どうしてもdocker buildの操作がうまくいかない。その苦しみの軌跡が誰かの役に立つかなと思い筆を取りました。

 

懸念点1:実行結果のstep 1/15のところ、ubuntu:15.10ってなってるけど、私が利用しているubuntu16.04とバージョン違ってるのは大丈夫なのでだろうか。

懸念点2:wily-updatesとかwily-securityとか wily-*ってなってるけれども、最新版のdockerではxenialを取ってくるはずなのになんでwilyをとってこようとしているんだろうか。これが原因何じゃなかろうか。 

root@bhjp1002-CF-SX1WEUHR:/etc/apt/sources.list.d# docker --version
Docker version 17.05.0-ce, build 89658be

root@bhjp1002-CF-SX1WEUHR:~# cd /usr/local/docker \
>
root@bhjp1002-CF-SX1WEUHR:/usr/local/docker# docker build -t ctf_loginme ./
Sending build context to Docker daemon 22.53kB
Step 1/15 : FROM ubuntu:15.10
---> 9b9cb95443b5
Step 2/15 : RUN apt-get -y update && apt-get -y upgrade
---> Running in 27fa6982074d
Ign http://archive.ubuntu.com wily InRelease
Ign http://archive.ubuntu.com wily-updates InRelease
Ign http://archive.ubuntu.com wily-security InRelease
Ign http://archive.ubuntu.com wily Release.gpg
Ign http://archive.ubuntu.com wily-updates Release.gpg
Ign http://archive.ubuntu.com wily-security Release.gpg
Ign http://archive.ubuntu.com wily Release
Ign http://archive.ubuntu.com wily-updates Release
Ign http://archive.ubuntu.com wily-security Release
Err http://archive.ubuntu.com wily/main Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily/restricted Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily/universe Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily/main amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily/restricted amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily/universe amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/main Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/restricted Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/universe Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/main amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/restricted amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-updates/universe amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/main Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/restricted Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/universe Sources
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/main amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/restricted amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
Err http://archive.ubuntu.com wily-security/universe amd64 Packages
404 Not Found [IP: 91.189.88.162 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/main/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/restricted/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/universe/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/main/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/restricted/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily/universe/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/main/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/restricted/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/universe/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/main/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/restricted/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-updates/universe/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/main/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/restricted/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/universe/source/Sources 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/main/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/restricted/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/wily-security/universe/binary-amd64/Packages 404 Not Found [IP: 91.189.88.162 80]

E: Some index files failed to download. They have been ignored, or old ones used instead.
The command '/bin/sh -c apt-get -y

 

こんなふうなコマンド結果になる。

ちなみに/etc/apt/souces.listの記述内容はこんな感じ。

root@bhjp1002-CF-SX1WEUHR:/usr/local/docker# cat /etc/apt/sources.list
###### Ubuntu Main Repos
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse

###### Ubuntu Update Repos
deb http://archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse

deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable

 

ついでに、/etc/apt/sources.list.d/docker.list  の記載内容はこれ。

deb https://apt.dockerproject.org/repo ubuntu-xenial main

 

進展があったら追記いたします。

これが原因じゃない?っていうのあったらコメントで教えてくださいこのとーりm(__)m

 

追記(20170924): 

上記懸念点1と2はだいたいあたっていた。docker buildコマンドを実行した結果が思わしくなかったため、docker buildが参照するファイルってどれだろうと調べると、Dockerfileというファイルを見に行っていることが判明。

そのファイルのFROMから始まる行には15.10と記載が。。。あれ?どっかでみたことあるぞ。。。

試しに16.04にすると、、、エラー。

試しにバージョンの数字は消してubuntuだけにしてみると。。。成功!!と思いきや、今度は、

'ascii' codec can't decode というメッセージが出てきて、localeを変更しないといけない様子。私は当初ja_JPになっていたので、en_USにしないといけないと思って、localeの変更に勤しみました。設定変更を反映させるためにrebootもしましたよ。はい、意味ありませんでした。同じエラーが私をあざ笑うかのように出ますよ。もう少し調べていくと、c.UTF-8ならいけるとの記事があったので、Dockerfileを再編集し、実行。

すると、

Step 19/25 : RUN bash -c "mysql_install_db --datadir=/tmp --basedir=/tmp --user=mysql"
---> Running in eb6e9af49886
2017-09-23 17:23:23 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2017-09-23 17:23:23 [ERROR] Can't locate the server executable (mysqld).
The command '/bin/sh -c bash -c "mysql_install_db --datadir=/tmp --basedir=/tmp --user=mysql"' returned a non-zero code: 1

→もう疲れた。。。でもmysql_install_db is deprecated. Please consider switching to mysqld --initializeってことだから、ココの記述をかえれば。。。で変えてみたけど、今度は、

2017-09-23T18:02:12.403947Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-09-23T18:02:12.404022Z 0 [ERROR] Aborting

とのこと。

 

(更に追記)

directoryにすでにファイルが入っていると言われてしまうので、強制的にクリアできるように下記のコマンドを実行するようにファイルに記述。

RUN rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld

エラーは出ずにdocker imageは作成できるようになった。

 

しかし、docker runしたところ、下記のメッセージ。

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

 

→socket接続ではなくtcp接続で試すことに。結果、3歩進んで2歩下がる状態。ログは下記。

 

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to MySQL server on 'localhost' (99)'

Check that mysqld is running on localhost and that the port is 3306.
You can check this by doing 'telnet localhost 3306'
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

 

他に試してみようと思っていること:

1.編集前のDockerfileの状態に戻す(エラーがでないように)

2.mysqlとアプリサーバを別々のimageにして作成してみる。(教科書はそのような指示を明確にしていないので、別々にimage作る必要は無いと思うが)

 

 

利用するdockerコマンド

docker build -t ctf_loginme_rv3 ./

docker run -it -p 8080:80 ctf_loginme_rv3

 

現在のDockerfileの内容

root@bhjp1002-CF-SX1WEUHR:/usr/local/docker# cat Dockerfile
FROM ubuntu
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install software-properties-common
RUN LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php

RUN apt-get update

RUN apt-get -y install build-essential
RUN apt-get -y install wget
RUN echo "mysql-server mysql-server/root_password password SeC-0n4b" | debconf-set-selections && \
echo "mysql-server mysql-server/root_password_again password SeC-0n4b" | debconf-set-selections && \
apt-get -y install mysql-server
RUN apt-get -y install apache2
RUN apt-get -y --allow-unauthenticated install php5.6
RUN apt-get -y --allow-unauthenticated install php5.6-mysql
RUN apt-get -y --allow-unauthenticated install php5.6-mcrypt
RUN apt-get -y --allow-unauthenticated install php5.6-dev

RUN apt-get -y install curl
RUN apt-get -y install vim

RUN a2enmod rewrite


RUN bash -c "echo \"[mysqld]\nsecure-file-priv = '/usr/local/sqli/pub/'> /etc/my.cnf\";mkdir -p /usr/local/sqli/pub"
RUN bash -c "chown www-data:www-data /usr/local/sqli -R"
RUN rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld
RUN bash -c "mysqld --initialize --datadir=/var/lib/mysql --ignore-db-dir=lost+found --explicit_defaults_for_timestamp --user=mysql"

ADD ./apache2.conf /etc/apache2/apache2.conf
ADD ./000-default.conf /etc/apache2/sites-enabled/000-default.conf
ADD ./sql.sql /tmp/sql.sql
ADD ./index.php /usr/local/sqli/pub/index.php

 

CMD ["bash", "-c", "export APACHE_LOG_DIR=/tmp/; /usr/bin/mysqld_safe & sleep 3 | /usr/bin/mysqladmin -h localhost --protocol=tcp -u root password 'SeC-0n4b' & /usr/sbin/apache2 -D FOREGROUND"]
EXPOSE 80