1 创建mysql
docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=root -d mysql01
然后再当前目录下创建 conf,data ,log 三个文件夹,把镜像里的配置复制到这些目录下,然后删除mysql01
docker cp mysql01:/etc/mysql/conf.d/. /root/mysql/conf/
docker cp mysql01:/var/lib/mysql/. /root/mysql/data/
docker cp mysql01:/logs/. /root/mysql/log/
创建一下新的mysql 镜像
docker run --name mysql02 -p 3306:3306 -d \
-v /root/mysql/conf/:/etc/mysql/conf.d/ \
-v /root/mysql/data:/var/lib/mysql \
-v /root/mysql/log:/logs \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always mysql:latest --lower_case_table_names=1
1 创建php
docker run -d --name my-php1 -p 9000:9000 php:8.1.3-fpm
老规矩复制配置文件
docker cp my-php1:/usr/local/etc/php/. /home/gui/project/phpfile/conf/
移除旧的php镜像
建立新的的php镜像
docker run --name myphp-fpm -p 9000:9000 \
-v /root/project/phpfile/www/html:/www \
-v /root/project/phpfile/conf:/usr/local/etc/php \
--link mysql02 -d php:8.1.3-fpm
docker run --name myphp-fpm -p 9000:9000 -v /root/project/phpfile:/www --link mysql02 -d php:8.1.3-fpm
1 创建nginx
docker run -d --name nginx01 -p:8880:80 nginx:latest
创建nginx 下的文件目录
mkdir -p nginx/{conf,html,logs}
复制镜像里nginx里的文件到我们创建的目录里
docker cp nginx01:/etc/nginx/nginx.conf /root/nginx/conf/nginx.conf
docker cp nginx01:/etc/nginx/conf.d /root/nginx/conf/conf.d
docker cp nginx01:/usr/share/nginx/html/index.html /root/nginx/html/index.html
停止刚刚创建的nginx
docker stop nginx01
docker rm nginx01
启动我们挂载目录的nginx ,我们给它重新命名成nginx02
docker run -d --name nginx02 -p 8880:80 \
-v /root/project/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/project/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /root/project/nginx/logs:/var/log/nginx \
-v /root/project/nginx/html:/usr/share/nginx/html \
--link myphp-fpm \
--privileged=true nginx:latest
-p 映射端口,格式为“宿主机端口:容器端口”
-v 挂载文件
-e TZ=Asia/Shanghai 设置时区
--privileged=true 让容器中的root用户拥有真正的root权限
--name 容器名字,以后可以使用这个名字启动或者停止容器
--restart=always docker启动时自动启动容器
-d 指定要启动的镜像名
--link myphp-fpm 关联我们的php
然后cd 到 nginx.conf 所在目录,vim nginx.conf
# 这里原来是 user nginx ,改成user root 外网就可以访问了
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/root/project/phpfile 目录下创建 index.php
<?php
phpinfo();
?>
修改/conf.d/ 目录下的default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
# root /home/gui/project/phpfile;
# index index.html index.htm index.php default.html default.htm default.php;
location / {
root /usr/share/nginx/html;
index 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 html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
31,0-1 Top
location ~ \.php$ {
fastcgi_pass myphp-fpm:9000; # 这里myphp-fpm 是php容器的名称
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name; # /www 是php容器映射出来的文件目录
include fastcgi_params;
}
}
然后我从主机访问VirtualBox的时候
输入地址:http://192.168.56.104:8880/index.php
访问成功:
4 进入php镜像安装扩展
docker exec -it myphp-fpm /bin/bash
执行一下 cd /usr/src/php/ext/
cd /usr/src/php/ext/ 到这个目录下
ls
安装mysql的扩展: docker-php-ext-install pdo_mysql
进入php的配置目录,conf.d目录下找到docker-php-ext-sodium.ini 写上如下配置
全部都解除封印吧
;extension=bz2
;extension=curl
;extension=ffi
;extension=ftp
;extension=fileinfo
;extension=gd
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
;extension=mbstring
;extension=exif ; Must be after mbstring as it depends on it
;extension=mysqli
;extension=oci8_12c ; Use with Oracle Database 12c Instant Client
;extension=oci8_19 ; Use with Oracle Database 19 Instant Client
;extension=odbc
;extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop
;extension=snmp
;extension=soap
;extension=sockets
;extension=sodium
;extension=sqlite3
;extension=tidy
;extension=xsl
;zend_extension=opcache
用 docker inspect mysql02 查一下mysql数据库的内部ip
然后改一下index.php的内容
<?php
namespace pdo_edu;
use PDO;
$username = 'yourname';
$password = 'yourpassword';
$dsn = 'mysql:host=172.17.0.4;dbname=mydb1;port=3306;charset=utf8';
$db=new PDO($dsn,$username,$password);
var_dump($db);
if($db){
echo '<h2>连接成功</h2>';
}else{
echo '<h2>连接失败</h2>';
}
?>
~
错误记录
执行 docker-php-ext-install curl 发生
执行 apt-get install libcurl4-openssl-dev 再重复上述命令
php的镜像里安装composer
进入docker镜像
按照如下步骤执行 https://pkg.xyz/#how-to-install-composer
然后注意用 php composer.phar 这个指令来使用composer
我们配置一下composer的阿里云
php composer.phar config -g repo.packagist composer https://mirrors.aliyun.com/composer/
启动php think worker发生错误
进入docker执行
docker-php-ext-configure pcntl --enable-pcntl && docker-php-ext-install pcntl