基於dockerfile創建nginx+PHP+mongo數據庫_完美搭建
第一步:
從git上:git clone
http://git.oursdata.com/wangyue/dockerfiles.git

進入下圖的文件夾中

然后執行以下的說明執行步驟
第二步:
開發環境dockerfile
BUILD
docker build -t silentmoon/centos7:devel -f centos7_ssh_devel .
//咖啡時間,請保持網絡連接,如果網速慢的話,就別繼續往下看了。
RUN
docker run -d -P --name devel_centos7_ssh -h develop silentmoon/centos7:devel
//沒有報錯就說明鏡像啟動正常 docker ps 看一下是否存在
ACCESS
- User/Password: work/qwe!@#bnm root/4man49a52e27
- Method of ssh
ssh work@127.0.0.1 -p32768 //一般情況是32768,docker ps查看一下
- Method of docker
docker exec -it 9e34e8ff45ea bash //容器 id 需要自己查看
- Method of Shell
ln -s `pwd`/devel ~/bin/devel chmod 755 ~/bin/devel devel //可不做這步
Work Directory Structure
cd ~
mkdir app
mkdir env
mkdir src
mkdir tmp
mkdir tar
mkdir bin
Nginx Install
GET
cd ~/src wget http://www.oursdata.com/misc/nginx-1.10.2.tar.gz tar -zxvf nginx-1.10.2
Configure & Make
cd ~/src/nginx-1.10.2
./configure --prefix=/home/work/env/nginx --sbin-path=/home/work/env/nginx/sbin/nginx --conf-path=/home/work/env/nginx/conf/nginx.conf --error-log-path=/home/work/env/nginx/error-log --pid-path=/home/work/env/nginx/run/nginx.pid --lock-path=/home/work/env/nginx/lock/nginx.lock --user=work --group=work make && make install
補充:默認下nginx是沒有啟動的,需要進入sbin文件下,./nginx 啟動 ./nginx -s reload 重新加載nginx
PHP Install
GET
cd ~/src wget http://www.oursdata.com/misc/php-5.6.29.tar.gz tar -zxvf php-5.6.29.tar.gz
Configure & Make
cd ~/src/php-5.6.29
./configure --prefix=/home/work/env/php --with-config-file-path=/home/work/env/php/etc --with-mysqli --with-pdo-mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype make && make install
補充:etc文件下是沒有php.ini文件,需要到80服務器上復制一個配置文件過來且可以同時把80服務器上的ext下擴展文件也拷貝過來,然后去sbin文件執行
./php-fpm -c ../etc/php.ini
設置nginx配置文件:
再nginx的結尾處nginx.conf
加上 include hosts/*.conf;

然后再conf文件夾下新建一個hosts文件夾,在hosts文件新建
配置文件如:
tianjian.conf
server {
listen 80;
index index.php;
root /home/work/app
/tj-bdaas/src/tj-bdaas/src/web/web; //此處是你自己定制的web訪問的項目地址就是你-v映射到容器里路徑
server_name
tianjian.loc.cn; //此處為自己配置的虛擬域名地址
location / {
index index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last; }
}
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
配置文件完成后,進入sbin文件下 執行
./nginx -s reload
最后去 vim /etc/hosts 文件下配置自己的虛擬域名,如下圖

tianjian.loc.cn 已可以訪問PHP頁面ok!
第三步
此時容器里面的nginx和php 已經安裝完成,現在可以把這個容易做成一個鏡像
docker commit -m "change somth" -a "somebody info" container_id(docker ps -a獲取id)
新鏡像名字 //
docker使用現有容器生成新的鏡像
這樣就會生成一個新的鏡像
通過
docker images

就會看到自己制作的鏡像
第四步
把自己鏡像再放到容器里面
docker制作鏡像
docker run -p 127.0.0.1:8080:80 -p 127.0.0.1:32768:22 -d -v /usr/local/var/www/philisense/tj-bdaas/src:/home/work/app tang/centos_devel:latest
這樣就大功告成了!
如果數據庫為mongodb庫,需要安裝下mongo 數據庫
docker pull mongo //由於網絡原因,可能會報錯,多pull幾次就可以
docker pull mongo -express //由於網絡原因,可能會報錯,多pull幾次就可以
創建mongoldb容器
docker run —name mongo -p 27017:27017 -d mongo
創建mongo-expres容器
docker run —link mongo:mongo —name mongo-express -p 8081:8081 -d mongo-express
sudo docker inspect mongo 查看mongo的ip地址