Dockerfile如何制作LAMP

这篇文章主要介绍Dockerfile如何制作LAMP,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

因为努力和真诚,有更多的客户和我们聚集在一起,为了共同目标,创新互联公司在工作上密切配合,从创业型企业到如今不断成长,要感谢客户对我们的高要求,让我们敢于面对挑战,才有今天的进步与发展。从网站到微信平台小程序开发,软件开发,成都app软件开发公司,10年企业网站建设服务经验,为企业提供网站设计,网站运营一条龙服务.为企业提供营销型网站建设,定制网站开发,原创设计,10年品质,值得您的信赖.

Dockerfile制作LAMP(MySQL默认密码空)
环境:
httpd 2.4.37
mysql 5.6.40
php 7.0.27
[root@oracle ~]# service docker start

建立docker卷
[root@oracle ~]# docker volume create lamp_www
[root@oracle ~]# docker volume create lamp_apache_conf
[root@oracle ~]# docker volume create lamp_mysql_conf
[root@oracle ~]# docker volume create lamp_mysql_data

在宿主机创建 /var/lib/docker/volumes目录
[root@oracle ~]# mkdir /var/lib/docker/volumes     (该路径docker默认,不能修改。)

[root@oracle ~]#  mkdir /root/lamp

[root@oracle ~]#  cd /root/lamp

[root@oracle my_mysql]# pwd
/root/lamp

[root@oracle my_mysql]# vi Dockerfile
FROM centos
MAINTAINER norman "normanjin@163.com"

#APACHE
#安装wget
RUN yum install -y wget
RUN yum install -y gcc make apr-devel apr apr-util apr-util-devel pcre-devel
RUN yum install -y net-tools
#下载并解压源码包,也可以先下载httpd-2.4.37.tar.gz到/root/lamp,然后直接使用COPY httpd-2.4.37.tar.gz /usr/local/src (也就是将本地目录下的httpd-2.4.37.tar.gz直接传送到image的/usr/local/src目录中,然后再执行WORKDIR /usr/local/src和RUN tar -zxvf httpd-2.4.37.tar.gz)
WORKDIR /usr/local/src
RUN wget http://apache.fayea.com/httpd/httpd-2.4.37.tar.gz
RUN tar -zxvf httpd-2.4.37.tar.gz
WORKDIR /usr/local/src/httpd-2.4.37
#编译安装apache (--enable-so是为了以后可以使用DSO. 编译安装中遗漏的模块,可以通过DSO安装方法来安装)
RUN ./configure --enable-so
RUN make
RUN make install
#修改apache配置文件
RUN sed -i 's/#ServerName www.example.com:80/ServerName localhost:80/g' /usr/local/apache2/conf/httpd.conf
#设置 apache开机自启动,在/etc/rc.local最后添加一行/usr/local/apache2/bin/httpd(不知道为什么apache就是不能开机自动启动)
RUN echo "/usr/local/apache2/bin/httpd" >>/etc/rc.local
#开放80端口
EXPOSE 80

#MYSQL
ADD http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm /usr/local/
RUN rpm -ivh /usr/local/mysql-community-release-el7-5.noarch.rpm
RUN yum install mysql-server -y
EXPOSE 3306
CMD ["systemctl enable mysqld"]
CMD ["systemctl start mysqld"]

#PHP
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
RUN yum install -y perl
WORKDIR /usr/local/src
RUN wget http://101.96.10.63/cn2.php.net/distributions/php-7.0.27.tar.gz
RUN tar -zxvf php-7.0.27.tar.gz
WORKDIR /usr/local/src/php-7.0.27
RUN sed -i 's#/replace/with/path/to/perl/interpreter#/usr/bin/perl#g' /usr/local/apache2/bin/apxs

RUN ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-apxs2=/usr/local/apache2/bin/apxs --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock
RUN make
RUN make install
WORKDIR /usr/local/src/php-7.0.27
RUN cp php.ini-production /usr/local/php/etc/php.ini
#去除下面配置语句的注释”;”
RUN sed -i 's#;extension=php_mysqli.dll#extension=php_mysqli.dll#g' /usr/local/php/etc/php.ini
RUN sed -i 's#; extension_dir = "ext"#extension_dir = "ext"#g' /usr/local/php/etc/php.ini
RUN echo "PATH=$PATH:/usr/local/php/bin" >> /root/.bashrc
RUN export PATH
#使配置立即生效
RUN source /etc/profile

[root@oracle my_mysql]#  docker build -t lamp .          (Dockerfile 创建镜像)

注解: RUN sed -i 's#/replace/with/path/to/perl/interpreter#/usr/bin/perl#g' /usr/local/apache2/bin/apxs
(-i :直接修改读取的文件内容,而不是输出到终端)( # #之间的内容可以直接被修改)
(将/usr/local/apache2/bin/apxs 文件第一行内容#!/replace/with/path/to/perl/interpreter -w修改为#!/usr/bin/perl -w)
如果不做以上修改会出以下错误:
Sorry, I cannot run apxs. Possible reasons follow:

  1. Perl is not installed

  2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs

  3. Apache was not built using --enable-so (the apxs usage page is displayed)

创建container
[root@oracle my_mysql]#  docker run -itd --privileged=true -p 80:80 -p 3306:3306 -v lamp_www:/usr/local/apache2/htdocs -v lamp_apache_conf:/usr/local/apache2/conf -v lamp_mysql_conf:/etc/mysql -v lamp_mysql_data:/var/lib/mysql --name lamp lamp   /usr/sbin/init

[root@oracle my_mysql]#  docker exec -i -t lamp bash

更改apache的配置文件,达到解析php文件的目的  
[root@62b35d09adba htdocs]# vi /usr/local/apache2/conf/httpd.conf
在LoadModule最后面添加以下内容
LoadModule php7_module        modules/libphp7.so
(编译用了--with-apxs2=/usr/local/apache2/bin/apxs,httpd.conf才会有这个模块)
##通常正确编译安装PHP后会自动增加,如果没有,需要手工添加。  
接下来要检查apache目录下生成的php动态连接库文件,在目录/usr/local/apache2/modules,找到是否存在libphp7.so文件。若不存在呢?则说明php安装不正确。

Dockerfile如何制作LAMP
然后在中添加以下
AddType application/x-httpd-php .php

由于image中没能成功设置开机自动启动apache,所以这里手工启动apache
/usr/local/apache2/bin/httpd

[root@62b35d09adba htdocs]# vi /usr/local/apache2/htdocs/info.php
phpinfo();
?>

测试打开 http://192.168.1.203/info.php  看到以下说明成功配置PHP

Dockerfile如何制作LAMP
创建数据库数据
root@9a44df59980e:/# mysql -uroot -p
mysql> show databases;
mysql> create database example default charset=utf8;
mysql> show create database example \G
mysql> use example;
mysql> create table tbl (idx integer(3), UserName varchar(30), LastName varchar(50), FreeText varchar(100)) ENGINE=InnoDB AUTO_INCREMENT=1 default charset=utf8;
mysql> show create table tbl \G
mysql> show columns from tbl;
mysql> select from tbl;
mysql> insert into tbl values (1,'Rafi','Ton','Just a test');
mysql> update tbl set UserName='Berber' where UserName='Rafi';
mysql> select
from tbl;
mysql> delete from tbl where idx=1 limit 1;
mysql> insert into tbl values (1,'Rafi','Ton','Just a test');                                                                                                                                                                
mysql> select * from tbl;  
mysql> quit
Bye

数据库创建完成后在宿主机上查看,能看到上面容器中创建的数据库example
[root@oracle _data]#  ls -la /var/lib/docker/volumes/lamp_mysql_data/_data
total 110604
drwxr-xr-x. 5 mysql mysql      147 Dec  4 16:08 .
drwxr-xr-x. 3 root  root        19 Dec  4 16:03 ..
-rw-rw----. 1 mysql mysql       56 Dec  4 16:03 auto.cnf
drwx------. 2 mysql mysql       50 Dec  4 16:08 example
-rw-rw----. 1 mysql mysql 12582912 Dec  4 16:08 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Dec  4 16:08 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Dec  4 16:03 ib_logfile1
drwx------. 2 mysql mysql     4096 Dec  4 16:03 mysql
srwxrwxrwx. 1 mysql mysql        0 Dec  4 16:03 mysql.sock
drwx------. 2 mysql mysql     4096 Dec  4 16:03 performance_schema

创建PHP文件(相对与PHP5,PHP7的最大变化之一是移除了mysql扩展,推荐使用mysqli或者pdo_mysql,实际上在PHP5.5开始,PHP就着手开始准备弃用mysql扩展,如果你使用mysql扩展,可能看到过这样的提示”Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in”.所以在以后的程序中,为了保持兼容性,要尽量减少使用mysql扩展用于数据库连接.)
(以下实例使用mysqli_query取代mysql_connect)
[root@2fec8154c8d0 php-7.0.27]# vi /usr/local/apache2/htdocs/index.php

Web Database Sample Index


Data from tbl


$connect = mysqli_connect ('127.0.0.1', 'root', '', 'example');      
$sql="select * from tbl";
$result = mysqli_query($connect,$sql);

if ($result) {
echo "Found these entries in the database:

";
echo "




";
/ Fetch the results of the query 返回查询的结果 /
while ($r = mysqli_fetch_assoc($result))
{
$idx = $r["idx"];
$user = $r["UserName"];
$last = $r["LastName"];
$text = $r["FreeText"];
echo "




";
}
echo "
User NameLast NameDomain NameRequest Date
$idx$user$last$text
";
}
else
{
echo "No data.";
}
/ Destroy the result set and free the memory used for it 结束查询释放内存 /
mysqli_free_result($result);
/ Close the connection 关闭连接/
mysqli_close($connect);
?>

测试LAMP打开http://192.168.1.203/index.php,已经能看到数据

Dockerfile如何制作LAMP

推送(push)Docker镜像到Docker Hub
#登录docker
[root@oracle ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: normanjin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@oracle lamp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lamp                latest              eefbed0e6b7b        17 minutes ago      1.78GB
centos              latest              75835a67d134        8 weeks ago         200MB

#通过push命令推送镜像
[root@oracle ~]# docker push lamp
The push refers to repository [docker.io/library/lamp]
1b1c29220282: Preparing
db96da7dd05d: Preparing
9afcea8cdf38: Preparing
c065e12e3ddc: Preparing
1edb6ba7364f: Preparing
9e9ddeffb3e1: Waiting
fb4939b9199d: Waiting
69d3c2a86f3b: Waiting
2ae58cef8e6a: Waiting
eae38119b1e1: Waiting
fc07c703bbb0: Waiting
538504760e19: Waiting
f8fc7dea67ee: Waiting
17de3d8f6769: Waiting
919050766d2e: Waiting
eed51e86558f: Waiting
cda478af73dc: Waiting
0a5472ea842c: Waiting
222ef4196d28: Waiting
d907cbf78296: Waiting
c8d2020c16c8: Waiting
7ef65f464278: Waiting
852384e0fb85: Waiting
da7d2b992ce3: Waiting
8f142509c0d9: Waiting
f972d139738d: Waiting
denied: requested access to the resource is denied

[root@oracle ~]# docker tag lamp normanjin/lamp             (为lamp镜像加tag)

[root@oracle lamp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lamp                latest              eefbed0e6b7b        28 minutes ago      1.78GB
normanjin/lamp      latest              eefbed0e6b7b        28 minutes ago      1.78GB
centos              latest              75835a67d134        8 weeks ago         200MB

[root@oracle ~]# docker push normanjin/lamp
The push refers to repository [docker.io/normanjin/lamp]
1b1c29220282: Pushed
db96da7dd05d: Pushed
9afcea8cdf38: Pushed
c065e12e3ddc: Pushed
1edb6ba7364f: Pushed
9e9ddeffb3e1: Pushed
fb4939b9199d: Pushed
69d3c2a86f3b: Pushed
2ae58cef8e6a: Pushed
eae38119b1e1: Pushed
fc07c703bbb0: Pushed
538504760e19: Pushed
f8fc7dea67ee: Pushed
17de3d8f6769: Pushed
919050766d2e: Pushed
eed51e86558f: Pushed
cda478af73dc: Pushed
0a5472ea842c: Pushed
222ef4196d28: Pushed
d907cbf78296: Pushed
c8d2020c16c8: Pushed
7ef65f464278: Pushed
852384e0fb85: Pushed
da7d2b992ce3: Pushed
8f142509c0d9: Pushed
f972d139738d: Pushed
latest: digest: sha256:9567362dba14631b8fc7d1defd0e9eb5adca785e97d7410a9fc5d9d1483a3d17 size: 5790
#注:推送Docker Hub速度很慢,耐心等待,很有可能失败,失败后尝试多次重传。

查看Docker Hub上我已经发布的镜像
https://hub.docker.com/r/normanjin/lamp/,如下图所示
Dockerfile如何制作LAMP

在Docker Hub上进行搜索,也是可以搜出来的

至此我已将自己的镜像发布到Docker Hub仓库。

以上是“Dockerfile如何制作LAMP”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


文章标题:Dockerfile如何制作LAMP
本文来源:http://scyanting.com/article/ihidcp.html