您好,登錄后才能下訂單哦!
這篇文章主要介紹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 '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:
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”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。