首页 娱乐资讯 医疗资讯 灯饰资讯 音乐资讯 it资讯 综艺频道 电脑资讯 站长资讯 小说 读书心得 旅游资讯 女性话题
详解mac下通过docker搭建LEMP环境
您当前的位置 :首页 > 站长资讯 正文 来源: 豫农生活网 发布时间:2020-08-10 06:08:06

在mac下通过搭建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 upWaiting for VM and Docker daemon to start.......................oooStarted.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.14boot2docker: 1.2.0 master : e75396e - Fri Aug 22 06:03:48 UTC 2014docker@boot2docker:~$

这样我们就成功登录virtualbox中的host环境,便可以进行docker操作了

安装nginx,php,mysql,基于ubuntu14:04,以下我是通过Dockerfile安装的

这里是DockFile

6.生成mysql镜像

Dockerfile

# LEMP stack as a docker containerFROM ubuntu:14.04MAINTAINER Daniel Watrous <email>#ENV http_proxy http://proxy.example.com:8080#ENV https_proxy https://proxy.example.com:8080 RUN apt-get updateRUN apt-get -y upgrade# seed database passCOPY mysqlpwdseed /root/mysqlpwdseedRUN 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 secretmysql-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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc2332dcad7ca 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 containerFROM ubuntu:14.04MAINTAINER Daniel Watrous <email>ENV http_proxy http://proxy.example.com:8080ENV https_proxy https://proxy.example.com:8080 # install nginxRUN apt-get updateRUN apt-get -y upgradeRUN apt-get -y install nginxRUN echo "daemon off;" >> /etc/nginx/nginx.confRUN mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bakCOPY default /etc/nginx/sites-available/default # install PHPRUN apt-get -y install php5-fpm php5-mysqlRUN sed -i s//;cgi/.fix_pathinfo/s*/=/s*1/cgi.fix_pathinfo/=0/ /etc/php5/fpm/php.ini # prepare php test scriptsRUN echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.phpADD wall.php /usr/share/nginx/html/wall.php # add volumes for debug and file manipulationVOLUME ["/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 postsif (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 HTMLecho "<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 resultsetmysql_free_result($result); // Closing connectionmysql_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 imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZElocal/nginx v1 9489872e2ebb 25 hours ago 250 MBlocal/mysql v1 b70d2e0f7f9c 26 hours ago 351.1 MBubuntu 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 psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES52e430b58579 local/nginx:v1 "/bin/sh -c 'service 21 hours ago Up 3 seconds 0.0.0.0:80->80/tcp nginxc2332dcad7ca 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 mysqldocker rm nginx

启动容器时挂载我们的目录到nginx的目录中

docker run -d -p 3306:3306 name mysql mysqldbdocker 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了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。

精华推荐
大家爱看
高清图集