使用docker搭建LAMP环境
环境:Centos7.4
[root@oracle ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
成都创新互联-专业网站定制、快速模板网站建设、高性价比名山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式名山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖名山地区。费用合理售后完善,十多年实体公司更值得信赖。
Get the most up-to-date version of Docker
https://get.docker.com
(This script is meant for quick & easy install via:)
[root@oracle ~]# curl -fsSL https://get.docker.com -o get-docker.sh
[root@oracle ~]# sh get-docker.sh
[root@oracle ~]# service docker start
Redirecting to /bin/systemctl start docker.service
[root@oracle ~]# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
[root@oracle ~]# docker search -s 10 lamp
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
linode/lamp LAMP on Ubuntu 14.04.1 LTS Container 157
tutum/lamp Out-of-the-box LAMP image (PHP+MySQL) 108
greyltc/lamp a super secure, up-to-date and lightweight L猞 92 [OK]
mattrayner/lamp A simple LAMP docker image running the prere猞 55 [OK]
fauria/lamp Modern, developer friendly LAMP stack. Inclu猞 46 [OK]
janes/alpine-lamp lamp base on alpine linux 36 [OK]
nickistre/ubuntu-lamp LAMP server on Ubuntu 26 [OK]
nickistre/centos-lamp LAMP on centos setup 24 [OK]
dgraziotin/lamp 11 [OK]
lioshi/lamp Docker image for LAMP + MySql under debian 10 [OK]
到docker hub的官网去搜索linode/lamp这个镜像,可以查看到他的具体使用说明(查看具体使用说明,避免走弯路)
https://hub.docker.com/r/linode/lamp
[root@oracle ~]# docker pull linode/lamp (下载镜像)
Using default tag: latest
latest: Pulling from linode/lamp
a3ed95caeb02: Pull complete
76a4cab4eb20: Pull complete
d2ff49536f4d: Pull complete
f94adccdbb9c: Pull complete
808b5278afbb: Pull complete
ab367f87d978: Pull complete
Digest: sha256:4b7af614899c795aa30cb60b62c28b978201c2dca3fb1352a60fec206edb92de
Status: Downloaded newer image for linode/lamp:latest
[root@oracle ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
linode/lamp latest 2359fa12fded 3 years ago 372MB
建立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
启动镜像
[root@oracle ~]# docker run -it -p 80:80 -p 3306:3306 -v lamp_www:/var/www -v lamp_apache_conf:/etc/apache2 -v lamp_mysql_conf:/etc/mysql -v lamp_mysql_data:/var/lib/mysql --name lamp linode/lamp /bin/bash
(docker run:运行一个container,如果后面要绑定宿主主机的0-1024端口需要使用sudo,因为是Ubuntu)
( -p port1:port2: 将宿主机的端口port1映射到容器中的port2)
(-v volume:path: 将宿主机的docker卷映射到容器中的指定的路径)
( -t -i linode/lamp /bin/bash:使用linode/lamp生成容器,并打开shell)
现在已经进入到了容器内。如果想退出容器,请不要使用exit或ctrl+d.这样会将容器直接关闭.
退出请使用ctrl + p ,然后 ctrl + q
上面的启动命令中一共映射了四个路径,分别是apache的web根目录,apache和MySQL的两个主配置文件,及MySQL的数据目录.这些都是为了方便我们后续直接在宿主机上对配置文件和web文件的修改进行的操作,利用docker卷映射了数据库的目录也能保证镜像再次运行的时候数据不会丢失.
[root@oracle ~]# ls -la /var/lib/docker/volumes
total 24
drwx------. 8 root root 255 Dec 1 13:41 .
drwx--x--x. 15 root root 200 Dec 1 10:56 ..
drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_apache_conf
drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_mysql_conf
drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_mysql_data
drwxr-xr-x. 3 root root 19 Dec 1 13:41 lamp_www
进入lamp_mysql_data这个卷的目录,可以看到镜像中的数据库目录已经映射到宿主机里了. 这个卷只要不删除,其数据就一直存在其中.这样避免了镜像重启后数据丢失的情况.
[root@oracle ~]# ls -la /var/lib/docker/volumes/lamp_mysql_data
total 0
drwxr-xr-x. 3 root root 19 Dec 1 13:41 .
drwx------. 8 root root 255 Dec 1 13:41 ..
drwx------. 5 102 106 164 Dec 1 13:43 _data
[root@oracle ~]# ls -la /var/lib/docker/volumes/lamp_mysql_data/_data
total 28684
drwx------. 5 102 106 164 Dec 1 13:43 .
drwxr-xr-x. 3 root root 19 Dec 1 13:41 ..
-rw-r--r--. 1 root root 0 Feb 20 2015 debian-5.5.flag
drwx------. 2 102 106 20 Dec 1 13:43 exampleDB
-rw-rw----. 1 102 106 18874368 Feb 20 2015 ibdata1
-rw-rw----. 1 102 106 5242880 Feb 20 2015 ib_logfile0
-rw-rw----. 1 102 106 5242880 Feb 20 2015 ib_logfile1
drwx------. 2 102 root 4096 Dec 1 13:43 mysql
-rw-------. 1 root root 6 Feb 20 2015 mysql_upgrade_info
drwx------. 2 102 106 4096 Dec 1 13:43 performance_schema
在终端中对lamp环境作个性化配置
[root@oracle ~]# docker exec -i -t lamp bash
启动apache2和mysql
root@9a44df59980e:/#service apache2 start
root@9a44df59980e:/#service mysql start
( 可以通过netstat -naltp 命令查看80和3306端口有没有开启成功)
root@9a44df59980e:/# netstat -naltp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0: LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 100/apache2
设置mysql允许root远程登录,及设置root的密码
root@9a44df59980e:/#smysql -u root -p
#初始密码:Admin2015
#修改root可远程登录:
mysql>use mysql;
mysql>update user set host = '%' where user = 'root' and host='127.0.0.1';
#修改密码:
mysql>update user set password=password("123456") where user='root';
#刷新权限
FLUSH PRIVILEGES;
mysql> quit
Bye
安装php扩展
root@9a44df59980e:/# apt-get update
root@9a44df59980e:/# apt-get install -y php5-mysql
root@9a44df59980e:/# apt-get install -y php5-gd
#然后重启apache2:
root@9a44df59980e:/# service apache2 restart
#再次确认mysql和apache2有没有安装成功:
root@9a44df59980e:/# netstat -naltp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0: LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 2899/apache2
可以通过下面的命令查看服务器上有哪些php的扩展:
root@9a44df59980e:/# apt-cache search php5-*
测试结果:
[root@oracle ~]# ip add
ens33:
link/ether 00:0c:29:6b:22:a9 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.203/24 brd 192.168.1.255 scope global noprefixroute dynamic ens33
valid_lft 2619sec preferred_lft 2619sec
inet6 fe80::9375:cddb:5162:50f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@oracle ~]# curl http://192.168.1.203
The Docker LAMP stack is working.
The configuration information can be found here or here
This index.html file is located in the "/var/www/example.com/public_html" directory.
访问http://192.168.1.203/
测试实例
[root@oracle ~]# docker exec -i -t lamp bash
创建数据库数据
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;
数据库创建完成后在宿主机上查看,能看到上面容器中创建的数据库example
[root@oracle _data]# cd /var/lib/docker/volumes/lamp_mysql_data/_data
[root@oracle _data]# ls
debian-5.5.flag example exampleDB ibdata1 ib_logfile0 ib_logfile1 mysql mysql_upgrade_info performance_schema
创建PHP文件
root@9a44df59980e:/# cd /var/www/example.com/public_html
root@9a44df59980e:/var/www/example.com/public_html# vi links.x
root@9a44df59980e:/var/www/example.com/public_html# vi index.php
Data from tbl
$connect = mysql_connect("localhost","root","123456") or die ("Problem connecting to DataBase");
$select = mysql_select_db('example', $connect);
$sql="select * from tbl";
$result = mysql_query($sql, $connect);
if ($result) {
echo "Found these entries in the database:
";
echo "
User Name | Last Name | Domain Name | Request Date |
$idx | $user | $last | $text |
}
else
{
echo "No data.";
}
mysql_free_result($result);
include ('links.x');
?>
打开http://192.168.1.203/index.php,已经能看到数据
root@9a44df59980e:/var/www/example.com/public_html# vi add.php
Add an entry
root@9a44df59980e:/var/www/example.com/public_html# vi editdb.php
$connect = mysql_connect("localhost","root","123456") or die ("Problem connecting to DataBase");
$query = "update tbl set
idx='{$_POST['idx']}',UserName='{$_POST['UserName']}',LastName='{$_POST['LastName']}',FreeText='{$_POST['FreeText']}' where
idx='{$_POST['idx']}'";
$select = mysql_select_db('example', $connect);
$result = mysql_query($query, $connect);
$query = "SELECT * FROM tbl";
$result = mysql_query($query, $connect);
if ($result)
{
echo "Found these entries in the database:
echo "
idx | User Name | Last Name | Free Text |
$idx | $user | $last | $text |
}
else
{
echo "No data.";
}
mysql_free_result($result);
include ('links.x');
?>
root@9a44df59980e:/var/www/example.com/public_html# vi del.php
Del an entry
$connect = mysql_connect("localhost","root","123456") or die ("Problem connecting to DataBase");
$select = mysql_select_db('example', $connect);
$sql="select * from tbl";
$result = mysql_query($sql, $connect);
if ($result)
{
echo "Found these entries in the database:
";
echo "
idx | User Name | Last Name | Free Text |
idx=$idx&UserName=$user&LastName=$last&FreeText=$text\">$idx | $user | $last | $text |
}
else
{
echo "No data.";
}
mysql_free_result($result);
include ('links.x');
?>
root@9a44df59980e:/var/www/example.com/public_html# vi dele.php
$connect = mysql_connect("localhost","root","123456") or die ("Problem connecting to DataBase");
$query = "delete from tbl where idx='{$_GET['idx']}'";
$select = mysql_select_db('example', $connect);
$result = mysql_query($query, $connect);
$query = "SELECT * FROM tbl";
$result = mysql_query($query, $connect);
if ($result)
{
echo "Found these entries in the database:
";
echo "
idx | User Name | Last Name | Free Text |
$idx | $user | $last | $text |
}
else
{
echo "No data.";
}
mysql_free_result($result);
include ('links.x');
?>
root@9a44df59980e:/var/www/example.com/public_html# ls -la
total 40
drwxr-xr-x. 2 root root 174 Dec 1 10:09 .
drwxr-xr-x. 5 root root 51 Dec 1 05:43 ..
-rw-r--r--. 1 root root 666 Dec 1 10:04 add.php
-rw-r--r--. 1 root root 1046 Dec 1 10:05 add2tbl.php
-rw-r--r--. 1 root root 1081 Dec 1 10:07 del.php
-rw-r--r--. 1 root root 977 Dec 1 10:07 dele.php
-rw-r--r--. 1 root root 1102 Dec 1 10:05 edit.php
-rw-r--r--. 1 root root 1064 Dec 1 10:06 editdb.php
-rw-r--r--. 1 root root 800 Dec 1 10:06 editing.php
-rw-r--r--. 1 root root 492 Feb 19 2015 index.html
-rw-r--r--. 1 root root 989 Dec 1 09:49 index.php
-rw-r--r--. 1 root root 211 Dec 1 10:01 links.x
以上数据存在于宿主主机上
[root@oracle _data]# ls -la /var/lib/docker/volumes/lamp_www/_data/example.com/public_html
total 40
drwxr-xr-x. 2 root root 174 Dec 1 18:09 .
drwxr-xr-x. 5 root root 51 Dec 1 13:43 ..
-rw-r--r--. 1 root root 1046 Dec 1 18:05 add2tbl.php
-rw-r--r--. 1 root root 666 Dec 1 18:04 add.php
-rw-r--r--. 1 root root 977 Dec 1 18:07 dele.php
-rw-r--r--. 1 root root 1081 Dec 1 18:07 del.php
-rw-r--r--. 1 root root 1064 Dec 1 18:06 editdb.php
-rw-r--r--. 1 root root 800 Dec 1 18:06 editing.php
-rw-r--r--. 1 root root 1102 Dec 1 18:05 edit.php
-rw-r--r--. 1 root root 492 Feb 20 2015 index.html
-rw-r--r--. 1 root root 989 Dec 1 17:49 index.php
-rw-r--r--. 1 root root 211 Dec 1 18:01 links.x
因此:
宿主机上mysql数据目录
/var/lib/docker/volumes/lamp_mysql_data/_data
宿主机上mysql配置文件目录
/var/lib/docker/volumes/lamp_mysql_conf/_data
宿主机上网页数据目录
/var/lib/docker/volumes/lamp_www/_data/example.com/public_html
宿主机上apache配置文件目录
/var/lib/docker/volumes/lamp_apache_conf/_data
网页名称:使用docker搭建LAMP环境
分享网址:http://azwzsj.com/article/pijehj.html