Docker 环境搭建
2020-06-28 本文已影响0人
归隐小赵
服务器:阿里云ESC
系统:CENTOS7.7
安全组:开放-1/-1
//拉取docker镜像
docker pull nginx:latest
//PS:需使用docker cp 进行copy到宿主机
先启动一个镜像,然后copy文件下来
docker run -p 80:80 --name my_nginx nginx
运行成功后 cp目录
需要cp目录:
docker cp my_nginx:/usr/share/nginx/html /nginx 需要将html从文件夹内拿出来
mv /nginx/html /nginx/www 重命名为www
docker cp my_nginx:/etc/nginx/nginx.conf /nginx/nginx.conf
docker cp my_nginx:/etc/nginx/conf.d /nginx 需要将配置从文件夹内拿出来
//创建nginx容器,并进行守护进行运行
docker run -p 80:80 --name my_nginx -v /nginx/www:/usr/share/nginx/html -v /nginx/nginx.conf:/etc/nginx/nginx.conf -v /nginx/logs:/var/log/nginx -v /nginx/conf.d:/etc/nginx/conf.d -d nginx
停止镜像
docker stop my_nginx
删除多余镜像
docker rm my_nginx
//拉取php7.1镜像
docker pull php:7.1.0-fpm
启动php容器,并挂载至/nginx/www
docker run -p 9000:9000 --name myphp -v /nginx/www/:/usr/share/nginx/html/ --privileged=true -d php:7.1.0-fpm
查看php镜像的ip地址(配置NGINX时需要使用)
docker inspect --format='{{.NetworkSettings.IPAddress}}' myphp
172.17.0.3
docker exec -it myphp /bin/bash #进入php容器
docker-php-source extract //拉取所有的docker已打包的扩展到/usr/src
进入ext目录
cd /usr/src/php/ext/
ls -l
安装pecl 拓展
apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev
安装mysqli
docker-php-ext-install mysqli
安装reids
pecl install redis 一路回车,不用Y不用N
docker-php-ext-enable 启用redis拓展
ctld+d 退出容器
//重启容器
docker restart myphp
//修改默认nginx配置:(conf/default.conf)
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
#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$ {
fastcgi_pass 172.17.0.3:9000; //这里输入docker的地址
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$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;
#}
}
推送配置到nginx内
docker cp /nginx/conf.d/default.conf my_nginx:/etc/nginx/conf.d/default.conf
进入容器,重启nginx
docker exec -it my_nginx /bin/bash
service nginx reload
ctrl+d 退出
OK,访问成功
参考文档:https://blog.csdn.net/u014265398/article/details/105007165
待补充:
PHP可挂载php.ini配置文件
MYSQL未安装