在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. boot2docker: 1.2.
master : e75396e - Fri Aug :: UTC
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 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*/cgi.fix_pathinfo\=/ /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 CMD service php5-fpm start && nginx
default 一个默认的nginx配置文件
server {
listen default_server;
listen [::]: default_server ipv6only=on; root /usr/share/nginx/html;
index index.php index.html index.htm; server_name localhost; location / {
try_files $uri $uri/ =;
} error_page /.html; error_page /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() unsigned NOT NULL auto_increment,
`title` varchar() 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 hours ago MB
local/mysql v1 b70d2e0f7f9c hours ago 351.1 MB
ubuntu 14.04 9bd07e480c5b days ago 192.7 MB
运行
docker run -d -p : --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 : --name mysql mysqldb
docker run -d -p : -v /Users/lyc/web:/usr/share/nginx/html --link mysql:mysql --name nginx local/nginx:v1
这样就可以修改/Users/lyc/web中的内容来更新/usr/share/nginx/html了
写得比较匆忙,更多的命令和具体查看
<docker-从入门到实践>
http://viget.com/extend/how-to-use-docker-on-os-x-the-missing-guide
http://software.danielwatrous.com/use-docker-to-build-a-lemp-stack-buildfile/
https://docs.docker.com/installation/mac/