首先,官方的Dockerfile我们稍微改动一下
# Default Dockerfile
#
# @link
# @document
# @contact group@
# @license /hyperf-cloud/hyperf/blob/master/LICENSE
FROM hyperf/hyperf:7.4-alpine-v3.11-swoole
LABEL maintainer="Hyperf Developers <group@>" version="1.0" license="MIT"
##
# ---------- env settings ----------
##
# --build-arg timezone=Asia/Shanghai
ARG timezone
ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
COMPOSER_VERSION=1.8.6 \
APP_ENV=prod
# update
RUN set -ex \
&& apk update \
#install ,由于是apline版本,所以需要先安装以下包
&& apk add autoconf gcc g++ make libffi-dev openssl-dev php-pear php7-dev pcre2-dev \
&& pecl channel-update \
&& pecl install mongodb \
&& touch /etc/php7// \
&& echo "extension=" > /etc/php7// \
# install composer
&& wget -nv -O /usr/local/bin/composer /composer/composer/releases/download/${COMPOSER_VERSION}/ \
&& chmod u+x /usr/local/bin/composer \
&& composer config -g composer /composer \
# show php version and extensions
&& php -v \
&& php -m \
# ---------- some config ----------
&& cd /etc/php7 \
# - config PHP
&& { \
echo "upload_max_filesize=128M"; \
echo "post_max_size=128M"; \
echo "memory_limit=1024M"; \
echo "=${TIMEZONE}"; \
} | tee / \
# - config timezone
&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
&& echo "${TIMEZONE}" > /etc/timezone \
# ---------- clear works ----------
&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
&& echo -e "\033[42;37m Build Completed :).\033[0m\n"
COPY . /data/project
WORKDIR /data/project
#初次安装注释掉,否则报错
#RUN composer install --no-dev -o
EXPOSE 9501
#运行目录未有hyperf代码的时候需要注释掉,等安装完hyperf再打开
#ENTRYPOINT ["php", "/data/project/hyperf-skeleton/bin/", "start"]
#使用watch热更新运行,需要composer安装watch扩展,是替代上面start的
#ENTRYPOINT ["php", "/data/project/hyperf-skeleton/bin/", "server:watch"]
编写
version: '3'
services:
hyperf:
build:
context: ./
dockerfile: Dockerfile
ports:
- "9501:9501"
container_name: hyperf
networks:
- web-network
volumes:
- /root/web/www:/data/project
privileged: true
user: root
stdin_open: true
tty: true
restart: always
nginx:
image: nginx
ports:
- "80:80"
container_name: nginx
restart: always
networks:
- web-network
volumes:
- /root/web/nginx/:/etc/nginx/
- /root/web/nginx/logs:/etc/nginx/logs
- /root/web/nginx/www:/etc/nginx/www
- /root/web/nginx/conf:/etc/nginx/
#depends_on:
redis:
image: redis
ports:
- "6379:6379"
container_name: redis
networks:
- web-network
volumes:
- redisdata:/data
command: "redis-server --appendonly yes --requirepass 123456"
restart: always
mongo:
image: mongo
ports:
- "27017:27017"
container_name: mongo
networks:
- web-network
restart: always
privileged: true
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
TZ: Asia/Shanghai
volumes:
- mongodata:/data/db
networks:
web-network:
external: true
volumes:
redisdata:
mongodata:
构建Dockerfile镜像
docker-compose build
启动docker-compose编排的服务容器
docker-compose up -d
最后就是我们进入hyperf镜像中使用composer创建项目了
#针对首次创建项目
composer create-project hyperf/hyperf-skeleton
#后续的操作应该就是composer install ……了,这里就不再叙述关于项目代码的说明了
hyperf部署官方文档:/2.2/#/zh-cn/quick-start/install