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天前
|
存储 机器学习/深度学习 中间件
快速上手 Elasticsearch:Docker Compose 部署详解
本文介绍了如何使用Docker Compose快速搭建Elasticsearch学习环境。Elasticsearch是一款用于实时搜索和分析的分布式中间件,适用于多种场景,如搜索、日志分析、机器学习等。首先,创建docker网络,拉取最新版8.12.2镜像。接着,编写docker-compose.yml文件,配置单节点集群,设置端口映射、内存限制及数据卷挂载。然后,创建并配置数据卷目录,允许远程访问和跨域。最后,启动服务并验证,通过浏览器访问确认服务运行正常。本文为初学者提供了一个简便的Elasticsearch部署方法。
141 4
快速上手 Elasticsearch:Docker Compose 部署详解
|
30天前
|
关系型数据库 MySQL Java
Docker Compose详细教程(从入门到放弃)
Docker Compose详细教程(从入门到放弃)
127 0
|
3月前
|
存储 数据可视化 数据安全/隐私保护
使用 Docker Compose 部署 Docker Registry
【1月更文挑战第2天】 在内网环境中,我们期望能够在本地共享镜像。为了解决这一问题,Docker Registry成为了我们的救星。Docker Registry是一个用于存储和管理Docker镜像的开源工具。通过在本地部署Docker Registry,您可以轻松地构建、存储和分享自己的Docker镜像。
149 3
使用 Docker Compose 部署 Docker Registry
|
4月前
|
Kubernetes 关系型数据库 MySQL
Docker Compose入门:打造多容器应用的完美舞台
Docker Compose 是一个强大的工具,它允许开发者通过简单的 YAML 文件定义和管理多容器的应用。本文将深入讨论 Docker Compose 的基本概念、常用命令以及高级应用场景,并通过更为丰富和实际的示例代码,助您轻松掌握如何通过 Docker Compose 打造复杂而高效的多容器应用。
|
1月前
|
Java Docker 微服务
如何使用Docker和Docker Compose部署微服务
【2月更文挑战第12天】
198 0
|
4月前
|
应用服务中间件 Linux nginx
Docker Compose 安装与使用(常用指令)
Docker Compose 安装与使用(常用指令)
222 0
|
4月前
|
前端开发 Docker Windows
Windows 安装 Docker Compose
Windows 安装 Docker Compose
|
2月前
|
Docker 容器
Docker Compose的安装与配置
Docker Compose的安装与配置
125 2
Docker Compose的安装与配置
|
2月前
|
存储 监控 Serverless
Serverless应用引擎(SAE)不支持直接通过Docker Compose进行部署
【2月更文挑战第8天】Serverless应用引擎(SAE)不支持直接通过Docker Compose进行部署
262 1
|
2月前
|
消息中间件 运维 应用服务中间件
容器化运维:构建高可用RabbitMQ集群的Docker Compose指南
容器化运维:构建高可用RabbitMQ集群的Docker Compose指南
154 0