当前位置: 首页 > 工具软件 > PHP Docker > 使用案例 >

docker-compose高效部署php环境

朱运诚
2023-12-01

一、前期准备

需要准备phpmysqlnginx镜像。下面是我安装的镜像。

[root@localhost ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
docker.io/phpdockerio/php72-fpm   latest              01cc79d26d94        9 days ago          164 MB
docker.io/mysql                   latest              e3fcc9e1cc04        13 days ago         544 MB
docker.io/nginx                   latest              8cf1bfb43ff5        13 days ago         132 MB

目录结构如下:

[root@localhost ~]# tree /www
/www
├── conf
│   └── php.conf
├── logs
└── web
    └── index.php

3 directories, 2 files

web 是网站的目录
conf Nginx的配置目录

其中,/www/conf/php.conf下的文件内容如下:

server {
	listen	80;
	server_name	localhost;

	location / {
		root	/usr/share/nginx/html;
		index	index.html index.htm index.php;
	}

	error_page	500 502 503 504	/50x.html;
	location = /50x.html {
		root	/usr/share/nginx/html;
	}

	location ~ .php$ {
		fastcgi_pass	php:9000;
		fastcgi_index	index.php;
		fastcgi_param	SCRIPT_FILENAME	/web/$fastcgi_script_name;
		include		fastcgi_params;
	}
}

二、编写docker-compose.yml文件

在工作的目录下创建docker-compose.yml文件。

version: "2.3"
services:
  nginx:
    image: nginx
    privileged: true
    ports: 
      - "80:80"
    volumes: 
      - /www/web:/usr/share/nginx/html
      - /www/conf:/etc/nginx/conf.d
      - /www/logs:/var/log/nginx
    networks:
      - web-net
  php:
    image: phpdockerio/php72-fpm
    privileged: true
    volumes:
      - /www/web:/web
    networks:
      - web-net
  mysql:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
    networks:
      - web-net
networks: 
  web-net:

严格注意yaml格式。

privileged 用来设置容器,需要操作挂载的权限

实例:

[root@localhost ~]# docker-compose up -d
Creating network "root_web-net" with the default driver
Creating root_nginx_1 ... done
Creating root_php_1   ... done
Creating root_mysql_1 ... done

构建成功
/www/web下,新建index.php,能正常访问。

 类似资料: