-
首先瞭解一下Docker以及Docker的一些基本操作,還有docker-compose是什麼。
-
然後需要安裝Docker和docker-compose,我將使用docker-compose來管理我的Dockerfiles。
zPhal-dockerfiles
app/
index.php
phpinfo.php
data/
.gitignore
files/
mysql/
conf.d/
mysql-file.cnf
Dockerfile
nginx/
conf.d/
default.conf
zphal.conf
Dockerfile
nginx.conf
php/
pkg/
.gitignore
Dockerfile
php.ini
php-dev.ini
php-fpm.conf
redis/
Dockerfile
docker-compose.yml
logs/
.gitgnore
README.md
FROM php:7.2-fpm
MAINTAINER goozp "gzp@goozp.com"
# 設定時區
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/*
# 將預先下載好的拓展包從宿主機複製進去
COPY ./pkg/redis.tgz /home/redis.tgz
COPY ./pkg/cphalcon.tar.gz /home/cphalcon.tar.gz
# 安裝 PECL 拓展,這裡我們安裝的是Redis
RUN pecl install /home/redis.tgz && echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
# 安裝第三方拓展,這裡是 Phalcon 拓展
RUN cd /home \
&& tar -zxvf cphalcon.tar.gz \
&& mv cphalcon-* phalcon \
&& cd phalcon/build \
&& ./install \
&& echo "extension=phalcon.so" > /usr/local/etc/php/conf.d/phalcon.ini
# 安裝 Composer
ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH
RUN rm -f /home/redis.tgz \
rm -f /home/cphalcon.tar.gz
WORKDIR /data
# Write Permission
RUN usermod -u 1000 www-data
services:
php-fpm:
volumes:
- /etc/localtime:/etc/localtime:ro
FROM nginx:1.12
# set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
FROM mysql:5.7
# set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
FROM redis:3.2
# set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
nginx/
conf.d/
default.conf
zphal.conf
Dockerfile
nginx.conf
server {
listen 80 default;
index index.html index.htm;
server_name localhost docker;
root /data/www;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/$fastcgi_script_name;
}
}
nginx:
build: ./nginx
depends_on:
- php-fpm
links:
- php-fpm:php-fpm
volumes:
- ../app:/data/www:rw
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ../logs/nginx:/var/log/nginx
ports:
- "80:80"
- "8080:8080"
- "443:443"
restart: always
command: nginx -g 'daemon off;'
-
build定義了你的Dockerfile在哪裡,如果沒有寫Dockerfile可以不用build,可以用images引數定義官方映象,比如image:mysql:5.7;
-
depends_on表示將依賴其它映象,比如Nginx依賴php-fpm,沒有它我Nginx沒法玩;
-
links定義連線,比如要連線到php-fpm容器,就是php-fpm:php-fpm,後面是別名;
-
ports表示埠對映,80:80表示將80埠對映到宿主機的80埠;
-
restart重啟,restart: always表示將自動重啟;
-
command是自動執行的命令;
-
……
version: '3.2'
services:
php-fpm:
build: ./php/
ports:
- "9000:9000"
links:
- mysql-db:mysql-db
- redis-db:redis-db
volumes:
- ../app:/data/www:rw
- ./php/php-dev.ini:/usr/local/etc/php/php.ini:ro
- ./php/php-fpm.conf:/usr/local/etc/php-fpm.conf:ro
- ../logs/php-fpm:/var/log/php-fpm:rw
restart: always
command: php-fpm
nginx:
build: ./nginx
depends_on:
- php-fpm
links:
- php-fpm:php-fpm
volumes:
- ../app:/data/www:rw
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ../logs/nginx:/var/log/nginx
ports:
- "80:80"
- "8080:8080"
- "443:443"
restart: always
command: nginx -g 'daemon off;'
mysql-db:
build: ./mysql
ports:
- "3306:3306"
volumes:
- ../data/mysql:/var/lib/mysql:rw
- ../logs/mysql:/var/lib/mysql-logs:rw
- ./mysql/conf.d:/etc/mysql/conf.d:ro
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: zphaldb
MYSQL_USER: zphal
MYSQL_PASSWORD: zphal123
restart: always
command: "--character-set-server=utf8"
redis-db:
build: ./redis
ports:
- "6379:6379"
volumes:
- ../data/redis:/data
restart: always
cd zPhal-dockerfiles/files
wget https://pecl.php.net/get/redis-3.1.6.tgz -O php/pkg/redis.tgz
wget https://codeload.github.com/phalcon/cphalcon/tar.gz/v3.3.1 -O php/pkg/cphalcon.tar.gz
docker-compose up
docker-compose up -d
docker-compose down
docker-compose run --rm -w /data/www/zPhal php-fpm composer update
cd zPhal-dockerfiles/app
docker run -it --rm -v `pwd`:/data/www/ -w /data/www/zPhal files_php-fpm composer update
-
註意掛載路徑。
-
構建失敗時,註意容器內是否報錯。
-
加速映象。如果過程下載映象很慢,可以使用國內的加速映象服務。
-
https://github.com/ZpGuo/zPhal-dockerfiles
-
https://docs.docker.com/