镜像下载
[root@localhost ~]# docker pull nginx

[root@localhost ~]# docker pull mysql

[root@localhost ~]# docker pull php:7.1.0-fpm

编写生成nginx容器
[root@localhost ~]# vim docker_nginx.sh
#!/usr/bin/env bash
docker run -p 80:80 --name nginx \
-v /docker/code:/usr/share/nginx/html \
-v /var/log/nginx:/var/log/nginx \
-v /docker/nginx/conf.d:/etc/nginx/conf.d \
--privileged=true \
-d nginx
 
[root@localhost ~]# sh docker_nginx.sh
3e9cd3d3b97c7e5ca001b30b5905c02c616fc48a81d14f716e3242d181ce2b31
编写生成php容器
[root@localhost ~]# vim docker_php.sh
 
#! /usr/bin bash
docker run -p 9000:9000 --name php \
-v /docker/code/:/var/www/html/ \
--privileged=true \
-d php:7.1.0-fpm
 
[root@localhost ~]# sh docker_php.sh
754a8c8c860fb3650a92e011ebae9d0575209c96a429411099d143f36cc1a435
编写生成mysql容器脚本
[root@localhost ~]# vim  docker_mysql.sh
# /usr/bin bash
docker run --name mysql \      #创建的容器名称
 -e MYSQL_ROOT_PASSWORD=lujunwen \     #设置mysql密码
 -v /usr/lib/mysql:/usr/lib/mysql \       
 -p 3306:3306 \              #容器的3306端口映射到宿主机器
 -d mysql                   #后台运行容器
 
[root@localhost ~]# sh docker_mysql.sh
63362a2f5744f962c7de19894e87624fa24579a3ca0177113f6703c8b2093250
创建项目的文件夹和nginx配置
[root@localhost ~]# mkdir /docker/code
mkdir: 无法创建目录"/docker/code": 文件已存在
[root@localhost ~]# mkdir /docker/nginx/conf.d
mkdir: 无法创建目录"/docker/nginx/conf.d": 文件已存在
[root@localhost ~]# vim /docker/nginx/conf.d/default.conf
 
server {
  listen  80 default_server;
  server_name localhost;
  root   /usr/share/nginx/html/;
 
  location / {
   index index.html index.htm index.php;
   autoindex off;
  }
  location ~ \.php(.*)$ {
   root  /var/www/html/;
   fastcgi_pass 172.17.0.3:9000;
   fastcgi_index index.php;
   fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param PATH_INFO $fastcgi_path_info;
   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
   include  fastcgi_params;
  }
}
重启Nginx容器
[root@localhost ~]# docker restart nginx
nginx
进行检测

创建index文件

[root@localhost ~]# echo "< ?php phpinfo(); ?>" > /docker/code/index.php

测试,成功后出现

该php镜像,不包括pdo_mysql扩展,需要自行安装,安装方法如下

docker  exec -it php bash
apt-get -y update
apt-get install -y libpng12-dev libjpeg-dev libpq-dev libxml2-dev
docker-php-ext-install  pdo_mysql pdo_pgsql
如果想要使用redis扩展
pecl install redis-3.1.1
docker-php-ext-enable redis
进入容器bash并进入mysql行
[root@localhost docker]#  docker exec -it mysql bash
root@63362a2f5744:/# mysql -uroot -p lujunwen
docker容器mysql

Myslq设置密码方法二

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -d mysql

原文来自:

本文地址://gulass.cn/docker-web-lnmp.html编辑:xiangping wu,审核员:逄增宝

Linux大全:

Linux系统大全:

红帽认证RHCE考试心得: