LoginSignup
53
35

More than 3 years have passed since last update.

Docker pull commit push Dockerfile WordPress環境をつくる 新コマンド編

Last updated at Posted at 2018-08-28

Dockerのインストール、pull、commit、push、Dockerfileからのbuildなど、一通りの事をWordPress環境を作ることを目的にやってみましょう。新コマンドに対応させました。

環境:VirtualBoxの仮想マシン上にCentOS7を初期インストールまたはパブリッククラウドでCentOS7のインスタンスを準備

事前にDokerHubのアカウントを作成しておきましょう。

Dockerのインストール

1.Dockerのインストール

必要なパッケージのインストール

コマンド
# yum install -y yum-utils device-mapper-persistent-data lvm2

リポジトリの追加

コマンド
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

最新版のDockerをインストール

コマンド
# yum install -y docker-ce

※今回は、最新版のDockerで行いますが、参考までにバージョン指定のインストールも記します。

<参考>
バージョン指定する場合

バージョンの確認

コマンド
# yum list docker-ce --showduplicates | sort -r
 * updates: ftpmirror.your.org
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
 * extras: mirror.san.fastserv.com
 * epel: mirror.pnl.gov
docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.06.1.ce-3.el7                   @docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable 
docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable 
docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable 
 * base: mirrors.syringanetworks.net
Available Packages

バージョン指定インストール(例としてdocker-ce-17.06.0.ce-1.el7.centos)

コマンド
# yum install docker-ce-17.06.0.ce-1.el7.centos

2.Dockerの自動起動設定及びサービスの起動

コマンド
# systemctl enable docker.service
# systemctl start docker.service

3.Dockerのバージョンを確認

コマンド
# docker version

4.システム情報の確認

コマンド
# docker info

Docker Hubからコンテナーイメージのpull

1.searchサブコマンドでレジストリーを検索

コマンド
# docker search linaction
INDEX       NAME                         DESCRIPTION               STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/linaction/centos   Sample CentOS6.7(Final)   0

2.コンテナーイメージをダウンロード

コマンド
# docker image pull -a linaction/centos
centos6: Pulling from docker.io/linaction/centos

47d44cb6f252: Already exists
6a7b54515901: Already exists
e788880c8cfa: Already exists
1debf8fb53e6: Already exists
72703a0520b7: Already exists
68833123ff9e: Already exists
ce76491a3be1: Already exists
Digest: sha256:759ea478951130a32e9191daaffc9b4e3c5ad089331eccc32112e370bf7c9fb8
Status: Image is up to date for docker.io/linaction/centos

3.ダウンロードしたイメージを確認

コマンド
# docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/linaction/centos   centos6             ce76491a3be1        About an hour ago   190.6 MB

コンテナーの起動

1.コンテナーを起動

コマンド
# docker container run -it --name centos01 linaction/centos:centos6 /bin/bash
[root@f2f7ecdecaf2 /]# 

起動したコンテナーの確認

1.コンテナー環境のOSのバージョンを確認

コマンド
[root@f2f7ecdecaf2 /]# cat /etc/redhat-release
CentOS release 6.7 (Final)

2.コンテナー内のプロセスを確認

コマンド
[root@f2f7ecdecaf2 /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:55 ?        00:00:00 /bin/bash
root        13     1  0 04:59 ?        00:00:00 ps -ef

3.コンテナー環境のネットワークを確認

コマンド
[root@f2f7ecdecaf2 /]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04
          inet addr:172.17.0.4  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:648 (648.0 b)  TX bytes:738 (738.0 b)

コンテナーとホストLinuxの切替

1.コンテナーの/bin/bashから抜けてホストLinuxに戻る

Ctrlキーを押したまま、PとQを順番に押す。

2.ホストLinuxから再度コンテナー内のbashに接続

コマンド
# docker container attach centos01
[root@f2f7ecdecaf2 /]#

コンテナーの起動と停止

1.Linuxホストに戻る

Ctrlキーを押したまま、PとQを順番に押す。

2.コンテナーを停止

コマンド
# docker container stop centos01

3.停止したコンテナーを確認

コマンド
# docker container ls -a
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                        PORTS               NAMES
f2f7ecdecaf2        linaction/centos:centos6   "/bin/bash"         23 minutes ago      Exited (137) 13 seconds ago                       centos01

4.コンテナーを起動

コマンド
# docker container start centos01

5.起動したコンテナーを確認

コマンド
# docker container ls
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
f2f7ecdecaf2        linaction/centos:centos6   "/bin/bash"         30 minutes ago      Up 4 seconds                  centos01

6.docker container execコマンド

コマンド
# docker container exec -it centos01 /bin/bash
[root@f2f7ecdecaf2 /]# exit
# docker container ls
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS               NAMES
f2f7ecdecaf2        linaction/centos:centos6   "/bin/bash"         30 minutes ago      Up 4 seconds                  centos01

DockerfileからWordPress環境作成

1.コンテナーを停止

コマンド
# docker container stop centos01

2.build用のディレクトリを作成

コマンド
# mkdir ~/build_wordpress

3.Dockerfileを作成

コマンド
# cd build_wordpress
# vim Dockerfile
Dockerfileのソースは事項参照
:wq
Dockerfile
#centos6のイメージを取得
FROM centos:centos6

#タイムゾーンの設定
RUN /bin/cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

#yumリポジトリの追加
RUN yum install -y epel-release
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

#php7のインストール
RUN yum install -y --enablerepo=remi,remi-php70 php php-devel php-mbstring php-pdo php-mysql php-gd php-mcrypt libmcrypt

#yumによるHTTPD,MySQL,tar,wgetのインストール
RUN yum -y install httpd mysql-server tar wget

#tmpディレクトリに移動
WORKDIR /tmp/

#wordpress一式のダウンロード
RUN wget https://ja.wordpress.org/latest-ja.tar.gz

#wordpressの展開
RUN tar xvfz ./latest-ja.tar.gz -C /var/www/html

#ダウンロードしたwordpressの削除
RUN rm -f ./latest-ja.tar.gz

#wordpressのconfigファイルをリネームして利用可能にする
RUN cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php

#wordpressのconfigファイルに必要な情報をsedコマンドで書き換える
RUN sed -i -e 's/database_name_here/wordpress/g' -e 's/username_here/wordpress/g' -e 's/password_here/wppass/g' /var/www/html/wordpress/wp-config.php

#DocumentRootディレクトリの所有者をapacheに変更
RUN chown -R apache.apache /var/www/html/

#mysqldの起動、DB作成、ユーザ作成および権限設定、mysqldの停止
RUN service mysqld start && mysql -u root -e "CREATE DATABASE wordpress; GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'wppass'; FLUSH PRIVILEGES;" && service mysqld stop

#mysqld,httpdの起動スクリプトの作成
RUN echo -e "service mysqld start\nservice httpd start\n/bin/bash" > /startService.sh

#mysqld,httpdの起動スクリプトの権限設定
RUN chmod o+x /startService.sh

#公開ポート
EXPOSE 80

#mysqld,httpdの起動スクリプトの実行
CMD /startService.sh

4.Dockerfileを実行

コマンド
# docker image build -t linaction/wordpress:ver1.0 ~/build_wordpress
・
・
Successfully built 0c4e45b86bcd

5.コンテナーを起動

コマンド
# docker container run -itd -p 8000:80 --name wordpress01 linaction/wordpress:ver1.0
3ce407416874308af370951ecf56c77b53e2aa405ba20aae51593fd3a0e2e108

コンテナーのcommit

1.ブラウザ確認

http://VirtualBoxのIPアドレス:8000/wordpress/」または「http://パブリッククラウドグローバルIPアドレス:8000/wordpress/」にアクセスし、WordPressの管理画面が表示されることを確認
※パブリッククラウドの場合は、IPアドレスは外部IP、ポートでアクセスできない場合は、クラウド側のファイアウォールを解除してください。

2.コンテナーを停止

コマンド
# docker container stop wordpress01

3.コンテナーを保存

コマンド
# docker container commit wordpress01 DockerHubアカウント名/wordpress:ver1.0
cf75652d0f8a1509a5ad132ecf1214416c2b8dbf9fe9f93830ab8b51e738db73

4.コンテナーイメージを確認

コマンド
# docker image ls
REPOSITORY                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
linaction/wordpress          ver1.0              cf75652d0f8a        56 seconds ago      474.9 MB
docker.io/centos             centos6             26cb1244b171        6 days ago          195 MB
docker.io/DockerHubアカウント名/centos   centos6             ce76491a3be1        2 hours ago         190.6 MB

コンテナーイメージをDocker Hubにpush

1.Docker Hubにログイン

コマンド
# docker login
Username:
Password:
Email:
Login Succeeded

2.Docker Hubにコンテナーイメージをpush

コマンド
# docker image push DockerHubアカウント名/wordpress
Do you really want to push to public registry? [y/n]: y
The push refers to a repository [docker.io/linaction/wordpress] (len: 0)
・
・
Digest: sha256:97e3bd8b7ed451163af7284513d398c426c796451d343400a9859156c1a8c021

3.リポジトリーを確認

コマンド
# docker search DockerHubアカウント名
INDEX       NAME                            DESCRIPTION               STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/DockerHubアカウント名/wordpress

コンテナーイメージを再ダウンロード

最後に全てのイメージと停止コンテナを削除して、pushしたwordpressのコンテナーイメージをダウンロードして起動してみましょう。

1.停止コンテナの一括削除

コマンド
# docker container rm `docker container ls -a -q`
# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.イメージの一括削除

コマンド
# docker image rm `docker image ls -a -q`
# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

3.wordpressのコンテナーイメージをダウンロード

コマンド
# docker image pull -a DockerHubアカウント名/wordpress

4.コンテナーを起動

コマンド
# docker container run -itd -p 8000:80 --name wordpress01 DockerHubアカウント名/wordpress:ver1.0

5.ブラウザ確認

http://VirtualBoxのIPアドレス:8000/wordpress/」または「http://パブリッククラウドグローバルIPアドレス:8000/wordpress/」にアクセスし、WordPressの管理画面が表示されることを確認
※パブリッククラウドの場合は、IPアドレスは外部IP、ポートでアクセスできない場合は、クラウド側のファイアウォールを解除してください。

53
35
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
53
35