xuol001 发表于 2015-4-17 10:56:19

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
#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
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 "" > /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的测试文件
页: [1]
查看完整版本: mac下通过docker搭建LEMP环境