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修改下

 

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,我们就会看到数据了

你可能感兴趣的:(docker)