安装PHP

1.搜索镜像,找到star最多的那个
peanut-b360hd3 :: ~ » docker search php
NAME                            DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
php                             While designed for web development, the PHP …   5435                [OK]                
phpmyadmin/phpmyadmin           A web interface for MySQL and MariaDB.          1065                                    [OK]
adminer                         Database management in a single PHP file.       436                 [OK]                
php-zendserver                  Zend Server - the integrated PHP application…   186                 [OK]                
webdevops/php-nginx             Nginx with PHP-FPM                              170                                     [OK]
...
2.下载php镜像
docker pull php:7.2-fpm

docker run --name my-php-fpm -d \
-p 9000:9000 --link my-mysql:mysql \
-v /home/lk/data/apps/works/php-apps/wwwroot:/usr/share/nginx/html \
php:7.2-fpm

PHP-FPM 容器的网站目录必须与 nginx 内的一致,否则会有不必要的麻烦

3.安装mysql

拉取镜像

docker pull mysql:5.7

docker run 运行容器同时建立目录映射

-v:主机和容器的目录映射关系,":"前为主机目录,之后为容器目录,挂载目录后就可以不用进入容器中修改配置,直接在对应挂载目录物理机下改配置文件修改
-e:配置信息,此处配置mysql的root用户的登陆密码
--link 容器与另外一个容器建立联系,这样就可以在当前的容器去使用另一个容器里的服务

sudo docker run -p 3306:3306 --name my-mysql \
-v /usr/local/docker/mysql/conf:/etc/mysql \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7

4.安装Nginx

docker run --name nginx-test -p 8080:80 -d nginx

docker cp nginx-test:/etc/nginx/nginx.conf /usr/local/docker/nginx/nginx.conf
docker cp nginx-test:/etc/nginx/conf.d/default.conf /usr/local/docker/nginx/conf.d/default.conf

删除nginx-test容器
docker stop fb138a759bd0 (nginx-test容器id)
docker rm fb138a759bd0
这么搞是因为 -v 默认只能创建目录

docker run --name my-nginx \
-v /usr/local/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /usr/local/docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v ~/data/apps/works/php-apps/wwwroot:/usr/share/nginx/html \
-v /usr/local/docker/nginx/log:/var/log/nginx \
-p 443:443 \
-p 80:80 \
--link my-php-fpm \
-d nginx:latest

需要注意的地方:

fastcgi_pass应该指向php-fpm对应的容器ip
server_name  docker.biz.com;
location ~ \.php$ {
    root           /usr/share/nginx/html/biz/webApp/web/;
    fastcgi_pass   172.17.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
    include        fastcgi_params;
}

docker中为php安装扩展

进入php-fpm容器执行下面命令

docker-php-ext-install pdo pdo_mysql

如果没意外就可以看到生成了docker-php-ext-pdo_mysql.ini文件
/usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini

绑定host

编辑物理机/etc/hosts
172.17.0.1 docker.biz.com


使用docker-compose 安装

version: '3'
services:
  php-fpm:
    container_name: php-fpm-compose
 #   build: ./docker/files/php-fpm #  –build 在启动容器前构建服务镜像,使用该参数就不用指定image参数
 image: php:7.2-fpm
    tty: true
    restart: always
    expose:
      - "9000"
 volumes:
      - ~/data/apps/works/php-apps/wwwroot:/data/www
      - ./docker/php-fpm/log:/var/log/php-fpm
    depends_on:
      - mysql
      - redis
    links:
      - mysql:mysql
      - redis:redis
  nginx:
    container_name: nginx-compose
    image: nginx:latest
  #  build: ./docker/files/nginx
 tty: true
    restart: always
    ports:
      - "80:80"
 - "443:443"
 volumes:
      - ~/data/apps/works/php-apps/wwwroot:/data/www
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
      - ./docker/nginx/log:/var/log/nginx
    depends_on:
      - php-fpm
    links:
      - php-fpm
  mysql:
    container_name: mysql-compose
    image: mysql:5.7
    restart: always
    expose:
      - "3306"
 ports:
      - "3306:3306"
 volumes:
      - ./docker/mysql/data:/var/lib/mysql
    environment: # 设置环境变量,这个变量名是官方镜像定义的。
 - MYSQL_ROOT_PASSWORD=123456
  redis:
    container_name: redis-compose
    image: daocloud.io/library/redis
    restart: always
    expose:
      - "6379"
 ports:
      - "6379:6379"
 volumes:
      - ./docker/redis/data:/data
    command: redis-server --appendonly yes
# 参考地址:
# https://www.cnblogs.com/minseo/p/11548177.html

参考资料

  • 使用Docker搭建MySQL服务

https://www.cnblogs.com/sabli...

  • docker-compose一键安装

https://www.awaimai.com/2120....


Peanut
6 声望0 粉丝