docker 安装php,nginx,redis,mysql容器并且link起来

这个人好意思说自己是原创,我也是服了,坑多,自己已经修改

我搭建的php7的

docker 安装php,nginx,redis,mysql容器并且link起来

    docker pull mysql
    docker pull nginx
    docker pull redis
    docker pull php:7.0-fpm


    需要制定版本的可以像php那样加版本号,没加的就是最新的


运行环境
MacBook Pro,OSX 10.13.6

启动 php-fpm
解释执行 php 需要 php-fpm,先让它运行起来:

docker run --name php7 -d  -v  /home/www:/www/wwwroot php:7.0-fpm

解释执行 php 需要 php-fpm,先让它运行起来:

说明:
php7 是容器的名字

~/home/lnmp/www  宿主机目录

/www/wwwroot     目录名字


 

启动mysql
 docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6

加版本,如果上面没有的话又会下载一遍

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

所以最好不要加版本

–name 容器名称

mysql 为自定义名称

-p:暴露端口,映射端口(可以映射多个端口) 外部端口:容器内部端口

-p 3306:3306 映射端口

-e:设置任意环境变量(容器内)

MYSQL_ROOT_PASSWORD=root 这里指 设置数据库密码为root

启动redis

docker run --name redis -p 6379:6379 -d redis

redis 为自定义名称 
-p 6379:6379 映射端口, 即 外部端口:容器内端口 
redis:latest 镜像名称:版本

nginx ,php-fpm ,mysql,redis容器内链接

docker run --name nginx --link mysql:db --link redis:redis --link php7:php -p 80:80 -v /home/www:/www/wwwroot -d nginx

编辑 nginx 配置文件

docker将容器中的conf.d中的default.conf的配置文件cp到本地 
docker cp nginx:/etc/nginx/conf.d/default.conf default.conf 

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /www/wwwroot;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root          /www/wwwroot;
        fastcgi_pass   172.17.0.2:9000; #php-fpm容器的ip,端口9000
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/wwwroot/$fastcgi_script_name;
        include        fastcgi_params;

    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

查看容器ip 的方法

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID> 
或 
docker inspect <container id> 
或 
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id!

修改完可以复制回去  docker cp default.conf  nginx:/etc/nginx/conf.d/default.conf
以上是我的配置文件
启动容器
docker start nginx
关闭容器
docker stop nginx
重启容器
docker restart nginx
删除容器
docker rm nginx
进入容器
docker exec -it nginx /bin/bash
容器状态
docker status nginx
查看镜像
docker images
查看运行的docker容器
docker ps
查看全部容器
docker ps -a
删除镜像
docker rmi nginx
导出 export
1.先查看 所有容器
sudo docker ps -a
2.找到要导出容器 的 CONTAINER ID,然后执行命令
sudo docker export 容器CONTAINER ID > 导出地址文件名

sudo docker export 234wer2323dfdfdsfq > /home/export.tar
导入 import
cat /home/export.tar | sudo docker import - lanmps:latest
保存 save
Save命令用于持久化镜像(不是容器)
1.先查看 所有镜像
sudo docker images

2.找到要保存的镜像名称
sudo docker save 镜像名称 > 保存地址文件名

sudo docker save lanmps > /home/save-lanmps.tar
加载
docker load < /home/save-lanmps.tar
最后访问localhost:8888就可以展示访问的目录
使用redis
要想使用redis那就必须在php容器中安装redis扩展
redis扩展下载连接 
phpize 
./configure 
make && make install 
这个时候编译安装完毕,然后会生成一个redis.so的扩展,这个时候要问他的配置文件在哪呢? 
他的配置文件直接扔到/usr/local/etc/php/conf.d下面

redis.ini文件名字
extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/redis.so
重启php-fpm容器
docker restart php72
localhost:8888/index.php
echo phpinfo();

会发现redis扩展已经装好
写段代码吧

$redis = new Redis();
$redis->connect('172.17.0.5',6379); //容器ip
// print_r($redis);die;
$strname = 'push';
$re = $redis->publish($strname,"来自小编自己写的    {$strname}推送!");
echo "-----{$strname}----推送成功<br />";
$redis->close();
mysql使用
try {
    $db = new PDO('mysql:host=172.17.0.4;dbname=test','root', 'root');
    $arr = $db->query("select * from user")->fetch();
    var_dump($arr);die;
} catch (PDOException $e) {
    print "Error: " . $e->getMessage() . "<br/>";
    die();
}
原文:https://blog.csdn.net/qq_36373262/article/details/79727223 
自己亲测,有坑的部分已经填平

发布了29 篇原创文章 · 获赞 5 · 访问量 8003

猜你喜欢

转载自blog.csdn.net/qq_25194685/article/details/89742199