Docke--Dockerfile 構建LNMP環境

Dockerfile 構建nginx並結合php

一、構建基礎鏡像

先構建一個基礎鏡像,添加repo的環境和編譯的環境,而centos鏡像就是初始的官方鏡像,後面構建php、nginx、mysql都使用該鏡像爲base image:php

[root@server myCentos]# cat Dockerfile   #查看Dockerfile文件
# base image
FROM centos

# MAINTAINER
MAINTAINER 381347268@qq.com

# add epel and 163 yum
RUN yum install wget epel-release -y \
    && mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back \
    && wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo \
    && wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo \
    && yum clean all && yum makecache

# Necessary packages
RUN yum install -y  wget gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdata crontabs

# change timzone to Asia/Shanghai
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@server myCentos]# docker build -t centos:v1 .  #構建鏡像取名爲centos:v1
[root@server myCentos]# docker images  #構建完成後查看該鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos              v1                  cca50f986518        About a minute ago   665MB

查看nginx和php的目錄結構:html

[root@server ~]# tree nginx
nginx
├── Dockerfile
├── fastcgi_params
├── nginx-1.8.1.tar.gz
├── nginx.conf
└── www.conf
[root@server ~]# tree php
php
├── Dockerfile
├── libmcrypt-2.5.8.tar.gz
├── php-5.6.35.tar.gz
├── php-fpm.conf.default
└── php.ini-production

這裏詳細描述下nginx和php的構建過程,已經構建過程當中用到的全部工具包和配置文件node

二、構建nginx鏡像

首先構建nginx鏡像,查看nginx的Dockerfile:mysql

[root@server nginx]# cat Dockerfile 
# base image
FROM centos:v1

MAINTAINER 381347268@qq.com

RUN useradd -M -s /sbin/nologin www
ADD nginx-1.8.1.tar.gz /usr/local/src

# install Dependency package
RUN yum install libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel libxml2 libxml2-dev libxslt-devel

WORKDIR /usr/local/src/nginx-1.8.1

# make && make install
RUN ./configure --user=www --group=www --prefix=/usr/local/nginx --with-file-aio --with-ipv6 --with-http_ssl_module  --with-http_spdy_module --with-http_realip_module    --with-http_addition_module    --with-http_xslt_module   --with-http_image_filter_module    --with-http_geoip_module  --with-http_sub_module  --with-http_dav_module --with-http_flv_module    --with-http_mp4_module --with-http_gunzip_module  --with-http_gzip_static_module  --with-http_auth_request_module  --with-http_random_index_module   --with-http_secure_link_module   --with-http_degradation_module   --with-http_stub_status_module && make && make install

# configure
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params
RUN mkdir /usr/local/nginx/conf/conf.d
COPY www.conf /usr/local/nginx/conf/conf.d/www.conf

EXPOSE 80

CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

這裏nginx採用了編譯安裝,建立了用戶www和安裝了nginx的一些依賴包,copy了一些配置文件到鏡像中。詳細配置文件參考:nginx

經過Dockerfile構建nginx鏡像:c++

[root@server nginx]# docker build -t nginx:v1 .  #構建nginx鏡像,tag爲v1
[root@server nginx]# docker images   #查看構建完成的鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  c1c90aaefa1e        33 seconds ago      752MB
centos              v1                  cca50f986518        2 hours ago         665MB

特別說明下配置文件:git

[root@server nginx]# cat www.conf 
server {
    listen   80;
    root /usr/local/nginx/html;
    index index.htm index.html index.php;
    location ~ \.php$ {
        root /usr/local/nginx/html;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

能夠發現fastcgi_pass php:9000;github

這是由於php也是一個容器,和nginx是隔離的,後面啓動容器的時候nginx將會經過--link的方式與鏡像進行互聯訪問sql

三、構建php鏡像

查看php的Dockerfile:docker

[root@server php]# cat Dockerfile 
# base image
FROM centos:v1

MAINTAINER 381347268@qq.com

ADD libmcrypt-2.5.8.tar.gz /usr/local/src

WORKDIR /usr/local/src/libmcrypt-2.5.8
RUN ./configure && make && make install

ADD php-5.6.35.tar.gz /usr/local/src

RUN yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel

WORKDIR /usr/local/src/php-5.6.35
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd && make && make install


COPY php.ini-production /usr/local/php/etc/php.ini
COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

RUN useradd -M -s /sbin/nologin php
RUN sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@php@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf
RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf


EXPOSE 9000

CMD ["/usr/local/php/sbin/php-fpm"]

構建的服務必須運行在前臺,而對於nginx來講:daemon off 表示將後臺運行關閉了,因而運行在前臺

而對於php:sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf

這裏也是將daemon模式關閉了,因而/usr/local/php/sbin/php-fpm 運行在前臺

經過Dockerfile構建php鏡像:

[root@server php]# docker build -t php:v1 .  #構建php鏡像,tag爲v1
[root@server php]# docker images  #查看構建完成的鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 v1                  2019612cc0a4        35 seconds ago      1.19GB
nginx               v1                  c1c90aaefa1e        About an hour ago   752MB
centos              v1                  cca50f986518        2 hours ago         665M

四、啓動nginx容器和php容器

利用構建的鏡像啓動php、nginx服務:

[root@server php]# docker run -d --name php -v /www:/usr/local/nginx/html php:v1  #啓動一個php容器並經過-v進行映射
cf77c85e7c2f63a7ac2db2edaa37d22418503e8cfa35d87b9cfad541d03e004a
[root@server php]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1  #啓動一個nginx容器並經過-v進行映射及--link進行關聯php容器
3bbc12db8e9cc118a7824c5a314d13c4e37f3a6397f32bbfe939f9e424b306e0

上面啓動php的容器時,使用了-v進行映射,若是這裏不進行映射,那麼php的程序會啓動,可是遇到php結尾的文件將不會解析,出現file not found的錯誤

查看容器狀態:

[root@server php]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
3bbc12db8e9c        nginx:v1            "/usr/local/nginx/sb…"   26 seconds ago      Up 25 seconds               0.0.0.0:80->80/tcp   nginx
cf77c85e7c2f        php:v1              "/usr/local/php/sbin…"   2 minutes ago       Up 2 minutes                9000/tcp             php

在宿主機上到網站目錄建立測試文件:

[root@server www]# tree .  #查看網站目錄的結構
.
├── index.php
└── test.html
[root@server www]# cat index.php   #查看index.php文件 <?php phpinfo() ?>
[root@server www]# cat test.html   #查看test.html文件 <h1>Hello Docker nginx-php</h1>

訪問測試:

進入到php容器查看hosts文件:

[root@server ~]# docker exec -it php /bin/bash  #使用exec進入php容器
[root@cf77c85e7c2f php-5.6.35]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    cf77c85e7c2f

進入到nginx容器查看hosts文件:

[root@server ~]# docker exec -it nginx /bin/bash  #使用exec進入nginx容器
[root@3bbc12db8e9c nginx-1.8.1]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    php cf77c85e7c2f
172.17.0.3    3bbc12db8e9c

能夠看到nginx的hosts文件中有一條php的解析,這就是爲何nginx可以和php進行通訊的原因(經過--link進行指定)

 

基於上面的步驟,nginx和php的鏈接就ok了,下面添加一個mysql 而後測試部署wordpress

五、構建mysql鏡像

查看mysql的目錄結構:

[root@server mysql]# tree .
.
├── business.sql
├── Dockerfile
├── MariaDB-10.0.33-centos7-x86_64-client.rpm
├── MariaDB-10.0.33-centos7-x86_64-common.rpm
├── MariaDB-10.0.33-centos7-x86_64-compat.rpm
├── MariaDB-10.0.33-centos7-x86_64-server.rpm
├── server.cnf
└── setup.sh

查看mysql的Dockerfile:

[root@server mysql]# cat Dockerfile 
FROM centos

MAINTAINER 381347268@qq.com

COPY MariaDB-10.0.33-centos7-x86_64-client.rpm /root/MariaDB-10.0.33-centos7-x86_64-client.rpm
COPY MariaDB-10.0.33-centos7-x86_64-common.rpm /root/MariaDB-10.0.33-centos7-x86_64-common.rpm 
COPY MariaDB-10.0.33-centos7-x86_64-compat.rpm  /root/MariaDB-10.0.33-centos7-x86_64-compat.rpm
COPY MariaDB-10.0.33-centos7-x86_64-server.rpm  /root/MariaDB-10.0.33-centos7-x86_64-server.rpm
WORKDIR /root
RUN yum remove mysql-libs -y
RUN yum -y install *.rpm
ADD business.sql /root/business.sql
ADD server.cnf /etc/my.cnf.d/server.cnf
ADD setup.sh /root/setup.sh

RUN yum clean all
RUN chmod +x /root/setup.sh
EXPOSE 3306

CMD ["/root/setup.sh"]

(1)這裏所使用的基礎鏡像是centos(官方鏡像,latest);(2)配置文件經過後面啓動時掛載進去,(若是後面有更改,只須要在某個目錄建立好配置文件,而後進行掛載便可);(3)business.sql是關於執行的sql(賦予root密碼,建立數據庫等等);(4)setup.sh是執行了從新初始化mysql(mariadb),最後重啓了服務(使用了 --user=mysql,避免出現了使用root用戶啓動mariadb)

setup.sh腳本原理:Dockerfile用來安裝數據庫服務,安裝完成後,經過setup.sh腳本從新將mysql進行初始化  初始化後,開啓MySQL服務執行setup.sh中的sql,關閉mysql數據庫 從新之前臺的方式啓動MySQL數據庫服務

[root@server mysql]# cat setup.sh 
#!/bin/sh
chown -R mysql:mysql /var/lib/mysql

mysql_install_db --user=mysql > /dev/null

mysqld_safe --user=mysql &

sleep 5

mysql < /root/business.sql

sleep 5

ps -wef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9

mysqld_safe --user=mysql
setup.sh
[root@server mysql]# cat business.sql 
create database wordpress DEFAULT CHARACTER SET utf8;

USE mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

UPDATE user SET password=PASSWORD("root") WHERE user='root';
FLUSH PRIVILEGES;
business.sql
[root@server mysql]# cat server.cnf 
[mysqld]
bind-address=0.0.0.0
console=1
general_log=1
general_log_file=/dev/stdout
collation-server=utf8_unicode_ci
character-set-server=utf8
server.cnf

經過Dockerfile構建mysql鏡像:

[root@server mysql]# docker build -t mysql:v1 .  #構建mysql鏡像

查看構建完成的鏡像:

[root@server mysql]# docker images  #查看三個鏡像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               v1                  4a3e428817c1        2 hours ago         813MB
php                 v1                  2019612cc0a4        3 hours ago         1.19GB
nginx               v1                  c1c90aaefa1e        4 hours ago         752MB

六、部署lnmp環境及wordpress

基於上面的三個鏡像 nginx、mysql、php構建lnmp環境,咱們先將上面啓動的容器給刪除掉,由於沒有什麼用了

先啓動三個容器:

[root@server mysql]# docker run -d --name mysql -v /root/mysql/server.cnf:/etc/my.cnf.d/server.cnf -v /data/mysql:/var/lib/mysql -p 3306:3306 mysql:v1  #啓動一個mysql容器
df28bcb97289676654ea7b573b0e3e52f910a599ff03b2dbf2dfe6345726127d
[root@server mysql]# docker run -d --name php -v /www:/usr/local/nginx/html --link=mysql:mysql php:v1  #啓動一個php容器
2f7fd098f0069deeaef4eb5b067e981c6be01a16d0769c3bc4203eab03a45c40
[root@server mysql]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1  #啓動一個nginx容器
b68688cd6f2d324e58c469cfb49fa7a14257ef2bb0c301a40a36ef244201c159
[root@server mysql]# docker ps  #查看啓動的三個容器的狀態
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
b68688cd6f2d        nginx:v1            "/usr/local/nginx/sb…"   11 seconds ago       Up 10 seconds       0.0.0.0:80->80/tcp       nginx
2f7fd098f006        php:v1              "/usr/local/php/sbin…"   About a minute ago   Up About a minute   9000/tcp                 php
df28bcb97289        mysql:v1            "/root/setup.sh"         About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   mysql

這裏闡述下這裏啓動的這三個容器:(1)先啓動一個mysql容器, 而且將上面的server.cnf配置文件和數據目錄掛載到了容器內部,這樣就防止了數據的丟失;(2)接着啓動一個php容器,使用了-v進行映射,若是這裏不進行映射,那麼php的程序會啓動,可是遇到php結尾的文件將不會解析,出現file not found的錯誤,而且使用了 --link 與mysql 關聯,這樣php容器就能夠鏈接mysql數據庫了;(3)最後啓動一個nginx容器,一樣使用了-v 進行映射,將網站目錄映射到宿主機,經過 --link與php 關聯

開始安裝wordpress:

[root@server ~]# cd /www/
[root@server www]# wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
[root@server www]# tar xf wordpress-4.7.4-zh_CN.tar.gz
[root@server www]# ll
總用量 8324
-rw-r--r-- 1 root   root           19 1月  11 12:27 index.php
-rw-r--r-- 1 root   root           32 1月  11 12:04 test.html
drwxr-xr-x 5 nobody nfsnobody    4096 4月  23 2017 wordpress
-rw-r--r-- 1 root   root      8507412 4月  23 2017 wordpress-4.7.4-zh_CN.tar.gz

經過瀏覽器訪問宿主機進行安裝:http://IP/wordpress

...省略...

 

進入到mysql容器查看hosts:

[root@server ~]# docker exec -it mysql /bin/bash  #使用exec進入mysql容器
[root@df28bcb97289 ~]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    df28bcb97289

進入到php容器查看hosts:

[root@server ~]# docker exec -it php /bin/bash  #使用exec進入到php容器
[root@2f7fd098f006 php-5.6.35]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.2    mysql df28bcb97289
172.17.0.3    2f7fd098f006

進入到nginx容器查看hosts:

[root@server ~]# docker exec -it nginx /bin/bash  #使用exec進入到nginx容器
[root@b68688cd6f2d nginx-1.8.1]# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.3    php 2f7fd098f006
172.17.0.4    b68688cd6f2d

經過觀察能夠看見在php容器的hosts中有一條mysql的記錄,這也就是爲何安裝wordpress時後,咱們直接填寫mysql容器的名字變能夠鏈接了。同理,nginx中也有一條php的記錄。

相關軟件包及配置文件見GitHub :https://github.com/buji595/docker_project/tree/master/Dockerfile_Project/lnmp

相關文章
相關標籤/搜索