Dockerfile 目录结构

[root@docker01 file]# tree -L 2 --charset ASCII
.
|-- mysql
|   |-- Dockerfile
|   |-- epel-6.repo
|   |-- my.cnf
|   `-- startup.sh
|-- nginx
|   |-- Dockerfile
|   |-- nginx-1.11.10
|   |-- nginx-1.11.10.tar.gz
|   |-- nginx.conf
|   `-- nginx_default.conf
|
`-- php-fpm
    |-- Centos-6.repo
    |-- Dockerfile
    |-- epel-6.repo
    |-- php-5.6.31
    `-- php-5.6.31.tar.gz

mysql,nginx,php的yum源

Centos-6.repo

[root@docker01 php-fpm]# cat Centos-6.repo 
# CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6[root@docker01 php-fpm]# 
[root@docker01 php-fpm]# cat Centos-6.repo 
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-6

epel-6.repo

[root@docker01 php-fpm]# cat epel-6.repo 
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://mirrors.aliyun.com/epel/6/$basearch
        http://mirrors.aliyuncs.com/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 6 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/6/$basearch/debug
        http://mirrors.aliyuncs.com/epel/6/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0

[epel-source]
name=Extra Packages for Enterprise Linux 6 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/6/SRPMS
        http://mirrors.aliyuncs.com/epel/6/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-6&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
gpgcheck=0

构建nginx镜像

Dockerfile

[root@docker01 nginx]# cat Dockerfile 
#lnmp centos 6.0
from centos:centos6
MAINTAINER dennis52o1314@163.com
ENV APP_DIR /web
add nginx-1.11.10 /nginx-1.11.10
RUN yum -y groupinstall "Development Tools" "Server Platform Deveopment"
RUN yum -y install openssl-devel pcre-devel
RUN useradd nginx -s /sbin/nologin
RUN cd /nginx-1.11.10 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre && make && make install
RUN mkdir /usr/local/nginx/conf/vhosts
RUN mkdir /var/log/nginx
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD nginx_default.conf /usr/local/nginx/conf/vhosts/default.conf
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]

nginx.conf

[root@docker01 nginx]# cat nginx.conf 
user  nginx;
worker_processes  1;
daemon  off;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

    gzip  on;
    gzip_disable  "msie6";
    gzip_vary  on;
    gzip_proxied  any;
    gzip_comp_level  6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /usr/local/nginx/conf/vhosts/*.conf;
}

nginx相关php配置nginx_default.conf

[root@docker01 nginx]# cat nginx_default.conf 
server {
    listen       80 default_server;
    server_name  localhost;

    #charset koi8-r;

    location / {
        root   /web;
        index  index.php index.html index.htm;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   APP_DIR;
    }

    # Disable nginx log write favicon.ico
    location = /favicon.ico {
    log_not_found off;
    access_log off;
    }

    # pass the PHP scripts to FastCGI server listening on port 9000
    #
    location ~ \.php$ {
        root           /web;
        fastcgi_pass   php:9000;
        #fastcgi_pass  unix:/tmp/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

构建nginx镜像

[root@docker01 nginx]#  docker build -t workpress/nginx:1.0 .

查看workpress/nginx镜像

[root@docker01 nginx]# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
workpress/nginx                  1.0                 2fbed074486c        7 weeks ago         652MB
composemultipleworkpress_nginx   latest              85bccc12873c        7 weeks ago         652MB
<none>                           <none>              47bbd62f8b03        7 weeks ago         652MB
workpress/mysql                  1.0                 6549c4af7357        7 weeks ago         359MB
workpress/php                    1.0                 44e9179f7098        7 weeks ago         1.2GB
<none>                           <none>              8e3e252129d5        7 weeks ago         626MB
centos                           centos6             7ea307891843        8 weeks ago         194MB
centos                           latest              328edcd84f1b        8 weeks ago         193MB
daocloud.io/library/nginx        latest              b8efb18f159b        2 months ago        107MB

php

Dockerfile

[root@docker01 php-fpm]# cat Dockerfile 
from centos:centos6
ADD Centos-6.repo /etc/yum.repos.d/CentOS-Base.repo
ADD epel-6.repo /etc/yum.repos.d/epel.repo
add php-5.6.31 /php-5.6.31
run yum -y groupinstall  "Desktop Platform Development" 
run yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt
run cd /php-5.6.31 && ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt  --with-bz2 --enable-fpm --with-gd && make && make install
run cp /php-5.6.31/php.ini-production  /usr/local/php/etc/php.ini
run mv /usr/local/php/etc/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"]

构建php镜像

docker build -t workpress/php:1.0 .

mysql

Dockerfile

[root@docker01 mysql]# cat Dockerfile 
FROM centos:centos6  
MAINTAINER dennis52o1314@163.com 
RUN yum install -y mysql-server mysql  
ADD ./startup.sh /opt/startup.sh
RUN chmod +x /opt/startup.sh
EXPOSE 3306
CMD ["/bin/bash","/opt/startup.sh"]  

my.cnf

[root@docker01 mysql]# cat my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/lib/mysql/mysqld.pid

startup.sh

[root@docker01 mysql]# cat startup.sh 
#!/bin/bash
if [ ! -f /var/lib/mysql/ibdata1 ]; then

    mysql_install_db

    /usr/bin/mysqld_safe &
    sleep 10s
    mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456'; FLUSH PRIVILEGES;"
    killall mysqld
    sleep 10s
fi

/usr/bin/mysqld_safe

构建mysql镜像

docker build -t workpress/mysql:1.0 .

启动博客系统

docker run -dit --name php -v /web:/web workpress/php:1.0
docker run -dit --name web -p 80:80 -v /web:/web --link php:php workpress/nginx:1.0
docker run -dit --name mysql -p 3306:3306 -v /opt/data:/var/lib/mysql workpress/mysql:1.0