Docker--------docker-compose LNMP实战

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1. 背景

   dockerfile 与 docker-compose 相关内容在前面文章已经详述,这里就不再一一说明。


2. 环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@docker ~] # cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)
  
[root@docker ~] # uname -r
3.10.0-327.36.3.el7.x86_64
 
[root@docker ~] # docker version
Client:
  Version:         1.12.6
  API version:     1.24
  Package version: docker-1.12.6-28.git1398f24.el7.centos.x86_64
  Go version:      go1.7.4
  Git commit:      1398f24 /1 .12.6
  Built:           Fri May 26 17:28:18 2017
  OS /Arch :         linux /amd64
 
[root@docker ~] # docker-compose version
docker-compose version 1.14.0, build c7bdf9e
docker-py version: 2.3.0
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
 
[root@docker ~] # ifconfig eth0 | sed -n 2p | awk '{print $2}'
192.168.60.150


3. 制作镜像

  * 构建php dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Php-fpm
#Version 1.0.1
#Author lisea
 
#Base Image
FROM centos:7
 
#Maintainer
MAINTAINER lisea cnlisea@126.com
 
#Commands
RUN rpm -ivh http: //mirrors .aliyun.com /epel/epel-release-latest-7 .noarch.rpm
RUN yum  install  php-fpm -y
RUN  sed  -i -e  's\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g'  -e 's\listen.allo
wed_clients = 127.0.0.1\;listen.allowed_clients = 127.0.0.1\g'  /etc/php-fpm .d /www .c
onf
RUN  sed  -i  's\;daemonize = yes\daemonize = no\g'  /etc/php-fpm .conf
 
EXPOSE 9000
CMD [ "php-fpm" ]


  * 构建 php 镜像

1
[root@docker php-fpm] # docker build -t lisea/php-fpm:v1.0.1 .


   * 准备nginx配置nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
user nginx nginx;
worker_processes auto;
daemon off;
 
error_log  /var/log/error_nginx .log crit;
pid  /var/run/nginx .pid;
worker_rlimit_nofile 102400;
 
events {
   use epoll;
   worker_connections 102400;
   multi_accept on;
}
 
http {
   include mime.types;
   default_type application /octet-stream ;
   server_names_hash_bucket_size 128;
   client_header_buffer_size 4k;
   large_client_header_buffers 4 32k;
   client_max_body_size 1024m;
   client_body_buffer_size 10m;
   sendfile on;
   tcp_nopush on;
   keepalive_timeout 60;
   server_tokens off;
   tcp_nodelay on;
 
   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
   fastcgi_buffer_size 64k;
   fastcgi_buffers 4 64k;
   fastcgi_busy_buffers_size 128k;
   fastcgi_temp_file_write_size 128k;
   fastcgi_intercept_errors on;
 
   #Gzip Compression
   gzip  on;
   gzip_buffers 16 8k;
   gzip_comp_level 6;
   gzip_http_version 1.1;
   gzip_min_length 256;
   gzip_proxied any;
   gzip_vary on;
   gzip_types
     text /xml  application /xml  application /atom +xml application /rss +xml application /xhtml +xml image /svg +xml
     text /javascript  application /javascript  application /x-javascript
     text /x-json  application /json  application /x-web-app-manifest +json
     text /css  text /plain  text /x-component
     font /opentype  application /x-font-ttf  application /vnd .ms-fontobject
     image /x-icon ;
   gzip_disable  "MSIE [1-6]\.(?!.*SV1)" ;
 
   #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
   open_file_cache max=102400 inactive=20s;
   open_file_cache_valid 30s;
   open_file_cache_min_uses 1;
   open_file_cache_errors on;
 
########################## vhost #############################
   include conf.d/*.conf;
}


   * 准备nginx配置php服务相关 nginx_localhost_80.conf 文件

     php:9000是通过后面的--link 容器之间互联指定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
   listen 80;
   server_name localhost;
   root  /data/www ;
   index index.html index.htm index.php;
 
   location ~ [^/]\.php(/|$) {
     fastcgi_pass php:9000;
     #fastcgi_pass unix:/usr/local/php-fastcgi/php-fpm.sock;
     fastcgi_index index.php;
     include fastcgi.conf;
   }
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
     expires 30d;
     access_log off;
   }
   location ~ .*\.(js|css)?$ {
     expires 7d;
     access_log off;
   }
   location ~ /\.ht {
     deny all;
   }
}


   * 构建 nginx dockerfile 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#Nginx
#Version 1.0.1
#Author lisea
 
#Base Image
FROM centos:7
 
#Maintainer
MAINTAINER lisea cnlisea@126.com
 
#Commands
RUN rpm -ivh http: //mirrors .aliyun.com /epel/epel-release-latest-7 .noarch.rpm
RUN yum  install  nginx -y
ADD nginx.conf  /etc/nginx/nginx .conf
ADD nginx_localhost_80.conf  /etc/nginx/conf .d /localhost_80 .conf
 
EXPOSE 80
CMD [ "nginx" ]


  * 构建 nginx 镜像

1
[root@docker nginx] # docker build -t lisea/nginx:v1.0.1 .


   * 准备mariadb 启动脚本 startup.sh

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
if  [ ! -f  /var/lib/mysql/ibdata1  ];  then
         mysql_install_db
         chown  mysql.mysql -R  /var/lib/mysql
         /usr/bin/mysqld_safe  &
         sleep  10s
         mysql -e  "grant all privileges on *.* to 'root'@'%' identified by '123456'; FLUSH PRIVILEGES;"
         kill  -s TERM ` ps  aux |  grep  mysqld |  grep  - v  'grep'  awk  '{print $2}' `
         sleep  10s
fi
/usr/bin/mysqld_safe

 

  * 构建mariadb(mysql) dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#Mariadb
#Version 1.0.1
#Author lisea
 
#Base Image
FROM centos:7
 
#Maintainer
MAINTAINER lisea cnlisea@126.com
 
#Commands
RUN rpm -ivh http: //mirrors .aliyun.com /epel/epel-release-latest-7 .noarch.rpm
RUN yum  install  mariadb-server mariadb -y
ADD startup.sh  /opt/startup .sh
RUN  chmod  +x  /opt/startup .sh
 
EXPOSE 3306
CMD [ "/bin/bash" , "/opt/startup.sh" ]


   * 构建 mysql 镜像

1
[root@docker mysql] # docker build -t lisea/mariadb:v1.0.1 .


   * 查看所有镜像

1
2
3
4
5
6
[root@docker lnmp] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lisea /mariadb        v1.0.1              5925448a6fb6        3 minutes ago       493.6 MB
lisea /nginx          v1.0.1              f1aba93ce33d        52 minutes ago      391.4 MB
lisea /php-fpm        v1.0.1              d205ea9fcdba        About an hour ago   350.1 MB
docker.io /centos     7                   3bee3060bfc8        2 weeks ago         192.5 MB


4. 编写docker-compose

   * docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version:  "2"
services:
   lnmp_nginx:
     image: lisea /nginx :v1.0.1
     ports:
       "8080:80"
     links:
       "lnmp_php:php"
     volumes:
       /data/www : /data/www
   lnmp_php:
     image: lisea /php-fpm :v1.0.1
     volumes:
       /data/www : /data/www
   lnmp_mariadb:
     image: lisea /mariadb :v1.0.1
     ports:
       "3306:3306"
     volumes:
       /data/mariadb : /var/lib/mysql


   * 开始构建并运行 -d 指定后台运行

1
2
3
4
5
6
7
8
[root@docker lnmp] # docker-compose up -d
Creating network  "lnmp_default"  with the default driver
Creating lnmp_lnmp_php_1 ... 
Creating lnmp_lnmp_mysql_1 ... 
Creating lnmp_lnmp_php_1
Creating lnmp_lnmp_php_1 ...  done
Creating lnmp_lnmp_mariadb_1 ...  done
Creating lnmp_lnmp_nginx_1 ...  done


   * 查看端口监听状态

1
2
3
4
5
6
7
[root@docker lnmp] # netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID /Program  name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      975 /sshd               
tcp6       0      0 :::3306                 :::*                    LISTEN      15138 /docker-proxy- 
tcp6       0      0 :::8080                 :::*                    LISTEN      15320 /docker-proxy- 
tcp6       0      0 :::22                   :::*                    LISTEN      975 /sshd


5. 测试lnmp环境

  * 路径切换到nginx web目录 [/data/www]

1
[root@docker lnmp] # cd /data/www/


   * index.php

1
2
3
<?php
   phpinfo();
?>


   * 浏览器访问 ip:8080/index.php

wKioL1lJ55OT8mrPAADxZWlSBbw448.jpg


6. 总结



以需求驱动技术,技术本身没有优略之分,只有业务之分。







      本文转自asd1123509133 51CTO博客,原文链接:http://blog.51cto.com/lisea/1940480,如需转载请自行联系原作者



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
25天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
28 0
|
3月前
|
Docker 容器
电子书阅读分享《4天实战 轻松玩转docker》
电子书阅读分享《4天实战 轻松玩转docker》
123 3
|
24天前
|
运维 负载均衡 应用服务中间件
LNMP详解(九)——Nginx虚拟IP实战
LNMP详解(九)——Nginx虚拟IP实战
35 2
|
3月前
|
分布式计算 Java Linux
【深入浅出Docker原理及实战】「原理实战体系」零基础+全方位带你学习探索Docker容器开发实战指南(Dockerfile使用手册)
Docker 是一套构建在 Linux 内核之上的高级工具,旨在帮助开发人员和运维人员更轻松地交付应用程序和依赖关系,实现跨系统和跨主机的部署。使用安全且轻量级的容器环境来实现这一目标。容器可以手动创建,也可以通过编写 Dockerfile 自动创建。开发人员和运维人员可以将应用程序及其依赖打包到容器中,实现应用程序的可移植性和环境一致性。
120 5
【深入浅出Docker原理及实战】「原理实战体系」零基础+全方位带你学习探索Docker容器开发实战指南(Dockerfile使用手册)
|
16天前
|
JavaScript 前端开发 Docker
全栈开发实战:结合Python、Vue和Docker进行部署
【4月更文挑战第10天】本文介绍了如何使用Python、Vue.js和Docker进行全栈开发和部署。Python搭配Flask创建后端API,Vue.js构建前端界面,Docker负责应用的容器化部署。通过编写Dockerfile,将Python应用构建成Docker镜像并运行,前端部分使用Vue CLI创建项目并与后端交互。最后,通过Nginx和另一个Dockerfile部署前端应用。这种组合提升了开发效率,保证了应用的可维护性和扩展性,适合不同规模的企业使用。
|
1月前
|
Unix Docker 容器
【超强图解Docker常见命令与实战】
【超强图解Docker常见命令与实战】
51 0
|
2月前
|
Java Linux Docker
Docker入门到实战
Docker入门到实战、Centos7安装Docker
|
2月前
|
Cloud Native Go 数据安全/隐私保护
自定义Docker镜像推送到Docker Hub实战
自定义Docker镜像推送到Docker Hub实战
87 2
自定义Docker镜像推送到Docker Hub实战
|
2月前
|
KVM 虚拟化 Android开发
DP读书:鲲鹏处理器 架构与编程(十二)鲲鹏软件实战案例Docker+KVM的部署
DP读书:鲲鹏处理器 架构与编程(十二)鲲鹏软件实战案例Docker+KVM的部署
56 1
|
3月前
|
Linux Docker Python
Python实战技术 - Python虚拟隔离环境 和 Docker技术
Python实战技术 - Python虚拟隔离环境 和 Docker技术
204 0