详解mac下通过docker搭建LEMP环境
在mac下通过docker搭建lemp环境境
1.安装virtualbox。由于docker是在lxc环境的容器
2.安装boot2docker,用于与docker客户端通讯
> brew update > brew install docker > brew install boot2docker
3.初始化boot2docker,也就是在virtualbox上安装一个docker的host环境
boot2docker init
此时会下载一个镜像
4.启动虚拟机host
:~$ boot2docker up waiting for vm and docker daemon to start... ....................ooo started. to connect the docker client to the docker daemon, please set: export docker_host=tcp://192.168.59.103:2375 unset docker_cert_path
这样host环境就启动起来了,根据提示设置环境变量
export docker_host=tcp://192.168.59.103:2375
后boot2docker就能与host环境的docker客户端连接了
5.连接host环境的docker客户端
macbook-pro:~$ boot2docker ssh ## . ## ## ## == ## ## ## ## === /""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/ _ _ ____ _ _ | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __ | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_| boot2docker with virtualbox guest additions version 4.3.14 boot2docker: 1.2.0 master : e75396e - fri aug 22 06:03:48 utc 2014 docker@boot2docker:~$
这样我们就成功登录virtualbox中的host环境,便可以进行docker操作了
安装nginx,php,mysql,基于ubuntu14:04,以下我是通过dockerfile安装的
这里是dockfile
6.生成mysql镜像
dockerfile
# lemp stack as a docker container from ubuntu:14.04 maintainer daniel watrous <email> #env http_proxy http://proxy.example.com:8080 #env https_proxy https://proxy.example.com:8080 run apt-get update run apt-get -y upgrade # seed database password copy mysqlpwdseed /root/mysqlpwdseed run debconf-set-selections /root/mysqlpwdseed run apt-get -y install mysql-server run sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf run /usr/sbin/mysqld & \ sleep 10s &&\ echo "grant all on *.* to admin@'%' identified by 'secret' with grant option; flush privileges" | mysql -u root --password=secret &&\ echo "create database test" | mysql -u root --password=secret # persistence: http://txt.fliglio.com/2013/11/creating-a-mysql-docker-container/ expose 3306 cmd ["/usr/bin/mysqld_safe"]
mysqlpwdseed
mysql-server mysql-server/root_password password secret mysql-server mysql-server/root_password_again password secret
进入mysql的dockefile然后生成一个mysql镜像
docker build -t "local/mysql:v1" .
成功后用
docker images
查看已生成的镜像
运行mysql镜像,生成一个容器
docker run -d --name mysql local/mysql:v1
运行起来后,可以用
boot2docker:~$ docker ps container id image command created status ports names c2332dcad7ca local/mysql:v1 "/usr/bin/mysqld_saf 26 hours ago up 2 seconds 3306/tcp mysql,nginx/mysql
来查看正在运行的容器
7.生成nginx,php ,这两个用一个dockerfile生成一个镜像
dockerfile
# lemp stack as a docker container from ubuntu:14.04 maintainer daniel watrous <email> env http_proxy http://proxy.example.com:8080 env https_proxy https://proxy.example.com:8080 # install nginx run apt-get update run apt-get -y upgrade run apt-get -y install nginx run echo "daemon off;" >> /etc/nginx/nginx.conf run mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak copy default /etc/nginx/sites-available/default # install php run apt-get -y install php5-fpm php5-mysql run sed -i s/\;cgi\.fix_pathinfo\s*\=\s*1/cgi.fix_pathinfo\=0/ /etc/php5/fpm/php.ini # prepare php test scripts run echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php add wall.php /usr/share/nginx/html/wall.php # add volumes for debug and file manipulation volume ["/var/log/", "/usr/share/nginx/html/"] expose 80 cmd service php5-fpm start && nginx
default 一个默认的nginx配置文件
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; server_name localhost; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
wall.php 一个用来测试连接mysql的测试文件
<?php // database credentials (defined in group_vars/all) $dbname = "test"; $dbuser = "admin"; $dbpass = "secret"; $dbhost = "mysql"; // query templates $create_table = "create table if not exists `wall` ( `id` int(11) unsigned not null auto_increment, `title` varchar(255) not null default '', `content` text not null default '', primary key (`id`) ) engine=myisam default charset=utf8"; $select_wall = 'select * from wall'; // connect to and select database $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('could not connect: ' . mysql_error()); echo "connected successfully\n<br />\n"; mysql_select_db($dbname) or die('could not select database'); // create table $result = mysql_query($create_table) or die('create table failed: ' . mysql_error()); // handle new wall posts if (isset($_post["title"])) { $result = mysql_query("insert into wall (title, content) values ('".$_post["title"]."', '".$_post["content"]."')") or die('create table failed: ' . mysql_error()); } // performing sql query $result = mysql_query($select_wall) or die('query failed: ' . mysql_error()); // printing results in html echo "<table>\n"; while ($line = mysql_fetch_array($result, mysql_assoc)) { echo "\t<tr>\n"; foreach ($line as $col_value) { echo "\t\t<td>$col_value</td>\n"; } echo "\t</tr>\n"; } echo "</table>\n"; // free resultset mysql_free_result($result); // closing connection mysql_close($link); ?> <form method="post"> title: <input type="text" name="title"><br /> message: <textarea name="content"></textarea><br /> <input type="submit" value="post to wall"> </form>
把这三个文件放在同一目录
进入目录
利用dockerfile生成镜像
docker build -t "local/nginx:v1" .
生成成功后查看句是你
boot2docker:~$ docker images repository tag image id created virtual size local/nginx v1 9489872e2ebb 25 hours ago 250 mb local/mysql v1 b70d2e0f7f9c 26 hours ago 351.1 mb ubuntu 14.04 9bd07e480c5b 3 days ago 192.7 mb
运行
docker run -d -p 80:80 --link mysql:mysql --name nginx local/nginx:v1
这时通过查看正在运行的容器
boot2docker:~$ docker ps container id image command created status ports names 52e430b58579 local/nginx:v1 "/bin/sh -c 'service 21 hours ago up 3 seconds 0.0.0.0:80->80/tcp nginx c2332dcad7ca local/mysql:v1 "/usr/bin/mysqld_saf 26 hours ago up 11 minutes 3306/tcp mysql,nginx/mysql
就可以看到nginx和mysql两个运行中的容器,其中nginx通过link连接到了mysql容器,
通过
--link mysql:mysql
命令,把mysql容器的地址加入到了nginx父容器的host文件中,此后,通过访问别名mysql,就可以访问到数据库了。
浏览器访问:192.168.59.103/wall.php,就可访问刚搭好的web服务器
8.持载目录到容器中
我们希望持载一个本地目录到容器中,以后存放可能需要修改的web内容,这里我们通过
-v /users/lyc/web:/usr/share/nginx/html
把的本地目录到容器中,存放web目录内容,这样就可以把wall.php之类的web文件放在/users/lyc/web目录,供容器访问
但是因为mac环境下host主机为virtualbox,因此我们先要绑定mac下的目录到虚拟机中
首先停止boot2docker
> boot2docker down
然后
> curl http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso > ~/.boot2docker/boot2docker.iso > vboxmanage sharedfolder add boot2docker-vm -name home -hostpath /users > boot2docker up
重新连docker后
先删除原先停止的容器
docker rm mysql docker rm nginx
启动容器时挂载我们的目录到nginx的目录中
docker run -d -p 3306:3306 --name mysql mysqldb docker run -d -p 80:80 -v /users/lyc/web:/usr/share/nginx/html --link mysql:mysql --name nginx local/nginx:v1
这样就可以修改/users/lyc/web中的内容来更新/usr/share/nginx/html了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。