共计 9098 个字符,预计需要花费 23 分钟才能阅读完成。
这篇文章主要介绍 Dockerfile 如何制作 LAMP,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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 extension=php_mysqli.dll#extension=php_mysqli.dll#g /usr/local/php/etc/php.ini
RUN sed -i 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:
Perl is not installed
apxs was not found. Try to pass the path using –with-apxs2=/path/to/apxs
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 安装不正确。
然后在 IfModule mime_module 中添加以下
AddType application/x-httpd-php .php
由于 image 中没能成功设置开机自动启动 apache, 所以这里手工启动 apache
/usr/local/apache2/bin/httpd
[root@62b35d09adba htdocs]# vi /usr/local/apache2/htdocs/info.php
?php
phpinfo();
?
测试打开 http://192.168.1.203/info.php 看到以下说明成功配置 PHP
创建数据库数据
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
html
head title Web Database Sample Index /title
/head
body bgcolor=#ffffff
h3 Data from tbl /h3
?php
$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: br p /p
echo table width=90% align=center border=1 tr
td align=center bgcolor=#00FFFF User Name /td
td align=center bgcolor=#00FFFF Last Name /td
td align=center bgcolor=#00FFFF Domain Name /td
td align=center bgcolor=#00FFFF Request Date /td
/tr
/ 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 tr
td $idx /td
td $user /td
td $last /td
td $text /td
/tr
}
echo /table
}
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);
?
/body
/html
测试 LAMP 打开 http://192.168.1.203/index.php,已经能看到数据
推送(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/,如下图所示
在 Docker Hub 上进行搜索,也是可以搜出来的
至此我已将自己的镜像发布到 Docker Hub 仓库。
以上是“Dockerfile 如何制作 LAMP”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注丸趣 TV 行业资讯频道!