这几天学了 docker 又用 docker 搭建了自己的环境。这中间遇到了很多坑,为此特意写此博文,供你们搭建环境的时候借鉴。
本篇本章默认你有 docker 的基本知识
本篇用到的环境
- 主机 window 10
- docker 18.03.1
- php-fpm 7.2.8
- nginx 1.15
- mysql 5.7
实践
搭建 Dockerfile
首先我们编写我们的 Dockerfile
mysql
FROM mysql:5.7
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
nginx
FROM nginx
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
两个镜像都不需要做什么更改,在这里我们只是更改我们的时区,调整系统时间。
php
FROM php:7.2-fpm
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 安装 php 拓展
RUN apt-get update && apt-get install -y \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install zip \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install opcache \
&& docker-php-ext-install mysqli \
&& rm -r /var/lib/apt/lists/*\
&& pecl install yaf-3.0.7 \ #yaf框架拓展
&& pecl install xdebug-2.6.0 \
&& pecl install redis-4.1.0 \
&& docker-php-ext-enable yaf xdebug redis
WORKDIR /app #设置工作路径
# 创建用户
RUN usermod -u 1000 www-data
大家可以根据自己需要的拓展,自行安装,我因为要搭建yaf的环境,所以我用到了该拓展。
编写 docker-compose.yml 文件
其实这个文件你也可以不编,因为考虑我的项目以后可能要做集群,所以我采用这种方式安装
version: '3.2'
services:
mysql:
build: ./mysql/
ports:
- "3306:3306"
volumes:
- type: bind
source: ./work/conf/mysql
target: /etc/mysql
- db-data:/var/lib/mysql
- type: bind
source: ./work/logs/mysql
target: /var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: DawnAPI
MYSQL_USER: xsp
MYSQL_PASSWORD: 04221021
restart: always
command: "--character-set-server=utf8"
networks:
- backend
nginx:
build: ./nginx/
depends_on:
- php-yaf
ports:
- "80:80"
volumes:
- ./work/conf/nginx:/etc/nginx
- type: bind
source: d:/app
target: /app
- type: bind
source: ./work/logs/nginx
target: /var/log/nginx
restart: always
command: nginx -g 'daemon off;'
networks:
- frontend
php-yaf:
build: ./php-yaf/
ports:
- "9000:9000"
volumes:
- ./work/php/etc:/usr/local/etc
- ./work/logs/php-fpm:/usr/local/etc/log/php-fpm
- d:/app:/app
networks:
- frontend
restart: always
command: php-fpm
networks:
frontend:
backend:
volumes:
db-data:
我们对这个文件拆分进行解析
mysql:
build: ./mysql/
ports:
- "3306:3306"
volumes:
- type: bind
source: ./work/conf/mysql
target: /etc/mysql
- db-data:/var/lib/mysql
- type: bind
source: ./work/logs/mysql
target: /var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: DawnAPI
MYSQL_USER: xsp
MYSQL_PASSWORD: 04221021
restart: always
command: "--character-set-server=utf8"
networks:
- backend
- 首先我根据我的 Dockerfile 安装我的镜像。
- 把我的 3306 端口映射到主机目录中
- 把本地
/work/conf/mysql
目录绑定到 mysql 容器中的/var/lib/mysql
中work/conf/mysql
目录中,有mysql 所有的配置信息,方便我们对 mysql 的更改 - 把数据库数据做持久化处理
- 绑定消息日志。
- 设置一些必要的环境变量,你们设置的时候可以参照mysql官方文档
- 设置docker启动的时候,容器自动启动
- 设置mysql的字符集
- 设置其网络为 backend ,我们后面会让 php 也处于 backend 网络中,以便通信。
nginx:
build: ./nginx/
depends_on:
- php-yaf
ports:
- "80:80"
volumes:
- ./work/conf/nginx:/etc/nginx
- type: bind
source: d:/app
target: /app
- type: bind
source: ./work/logs/nginx
target: /var/log/nginx
restart: always
command: nginx -g 'daemon off;'
networks:
- frontend
- build 和上面同理。
depends_on
设置 nginx 的启动时,php-yaf 必须先启动,所以它会先启动 php-yaf- 开放 80端口
- 同样就是把一些目录映射到本地,其中值得注意的是
app
目录是用来存放我们的应用的,在 php-yaf 容器中,也要绑定这一目录
php-yaf:
build: ./php-yaf/
ports:
- "9000:9000"
volumes:
- ./work/php/etc:/usr/local/etc
- ./work/logs/php-fpm:/usr/local/etc/log/php-fpm
- d:/app:/app
networks:
- frontend
restart: always
command: php-fpm
值得注意的就是,必须把我们的 app
目录 和我们的 nginx
共享目录。
写好之后执行 docker-compose up
就安装成功了,这时候我们再访问 127.0.0.1,会发现我们访问失败了。因为我们的配置文件还没有修改。
修改配置文件
修改 nginx 配置文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
root /app;
location / {
root /app/;
index index.html index.htm index.php;
}
#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$ {
fastcgi_pass php-yaf:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
#}
}
参照修改。 修改了一般就访问成功了。
如果有问题就可以在下方留言。