docker compose php+mysql+nginx

 前面我们已经把php基础环境搭建好了,但是还缺少数据库,我们这里使用mysql

首先在docker-compose.yml添加mysql

 

mysql:
    image: mysql
    volumes:
      - ./dbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456
      MYSQL_ROOT_PASSWORD: 123456
    networks:
      - code-network

 

 

 

 

 

完整的yml如下:

 

version: "2"
services:
  web:
    image: nginx
    ports:
      - "8080:80"
    volumes:
      - ./code:/code
      - ./site.conf:/etc/nginx/conf.d/default.conf
    networks:
      - code-network
  mysql:
    image: mysql
    volumes:
      - ./dbdata:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_USER: root
      MYSQL_PASSWORD: 123456
      MYSQL_ROOT_PASSWORD: 123456
    networks:
      - code-network
  php:
    image: php:7.0.12-fpm
    volumes:
      - ./code:/code
    networks:
      - code-network

networks:
  code-network:
    driver: bridge


对于environment,在docker文档有提到

 

https://docs.docker.com/samples/library/mysql/

 

Environment Variables

 

When you start the mysql image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the docker run command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.

MYSQL_ROOT_PASSWORD

This variable is mandatory and specifies the password that will be set for the MySQL rootsuperuser account. In the above example, it was set to my-secret-pw.

MYSQL_DATABASE

This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

MYSQL_USERMYSQL_PASSWORD

These variables are optional, used in conjunction to create a new user and to set that user’s password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE variable. Both variables are required for a user to be created.

Do note that there is no need to use this mechanism to create the root superuser, that user gets created by default with the password specified by the MYSQL_ROOT_PASSWORD variable.

MYSQL_ALLOW_EMPTY_PASSWORD

This is an optional variable. Set to yes to allow the container to be started with a blank password for the root user. NOTE: Setting this variable to yes is not recommended unless you really know what you are doing, since this will leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access.

MYSQL_RANDOM_ROOT_PASSWORD

This is an optional variable. Set to yes to generate a random initial password for the root user (using pwgen). The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: .....).

MYSQL_ONETIME_PASSWORD

Sets root (not the user specified in MYSQL_USER!) user as expired once init is complete, forcing a password change on first login. NOTE: This feature is supported on MySQL 5.6+ only. Using this option on MySQL 5.5 will throw an appropriate error during initialization.

使用docker-compose up -d运行

完后我们把index.php修改下

 

<?php
//phpinfo();
$conn = new mysqli("mysql", 'root', '123456', 'dockertest');
if(!$conn){
  die($conn->connect_error);
}
$result = mysqli_query($conn, "select * from blog");
while ($arr = $result->fetch_assoc()) {
  var_dump($arr);
}
?>

 

 

这里需要说明的是host,我们使用的是docker image, 所以这里的host填写image名称

然后我们运行,然而运行结果并不是我们想要的,并且报错了,提示找不到mysqli, 翻看docker php:

https://docs.docker.com/samples/library/php/

在文档里面有这样一段

 

How to install more PHP extensions

 

We provide the helper scripts docker-php-ext-configuredocker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.

In order to keep the images smaller, PHP’s source is kept in a compressed tar file. To facilitate linking of PHP’s source with any extension, we also provide the helper script docker-php-source to easily extract the tar or delete the extracted source. Note: if you do use docker-php-source to extract the source, be sure to delete it in the same layer of the docker image.


所以需要我们手动去安装mysqli扩展

 

接下来我们新建一个Dockerfile,放在docker/php目录下:

 

FROM php:7.0-fpm

RUN docker-php-ext-install mysqli


修改docker-compose.yml

 

 

php:
    build: ./docker/php/
    volumes:
      - ./code:/code
    networks:
      - code-network


在运行docker-compose up -d

 

完后运行docker-compose up 

在浏览器输入127.0.0.1:8080,我们就会看到数据了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值