利用docker搭建yii2 详细步骤

时间:2023-03-09 19:02:05
利用docker搭建yii2 详细步骤

定位镜像

在hub.docker.com 搜索yii2,并且最后定位到 https://hub.docker.com/r/codemix/yii2-base/
codemix/yii2-base 然后在github上打开 clone 到本地

启动容器

按照下面步骤操作

composer create-project --no-install codemix/yii2-dockerized myprojectcd myproject
cp docker-compose-example.yml docker-compose.yml
cp .env-example .env
启动docker 建议加上 -d 参数
docker-compose up

数据库迁移
docker-compose run --rm web ./yii migrate

把配置修改为自己没有占用的端口

里面有 nginx 和 Apache 两个web服务器可以选择

执行 docker-compose build
docker-compose up -d

访问 ip:port

直接访问本地对应的端口

docker-compose.yml 文件修改参考

 # For *-apache base image
# web:
# build: ./
# ports:
# - "8080:80"
# expose:
# - ""
# volumes:
# - ./:/var/www/html/
# links:
# - db
# environment:
# ENABLE_ENV_FILE:
# ENABLE_LOCALCONF:
# API_TOKEN: "<YOUR GITHUB API TOKEN>" # Uncomment to autostart at boottime
#restart: always # Uncomment to send log output to syslog
#log_driver: "syslog"
#log_opt:
# tag: "docker-web" # for *-php-fpm and *-hhvm base image
app:
build: ./
expose:
- ""
volumes:
- ./:/var/www/html/
links:
- db
environment:
ENABLE_ENV_FILE:
ENABLE_LOCALCONF:
API_TOKEN: "f1528c9bc2181668bc80417e33a7fb79412bf52a" nginx:
build: ./nginx
ports:
- "8083:80"
links:
- app
volumes_from:
- app db:
image: mysql:5.6
ports:
- "3308:3306"
expose:
- ""
environment:
MYSQL_ROOT_PASSWORD: secret-root
MYSQL_DATABASE: web
MYSQL_USER: web
MYSQL_PASSWORD: web # Uncomment to autostart at boottime
#restart: always 

Dockerfile 文件修改参考

 #FROM codemix/yii2-base:2.0.-apache
FROM codemix/yii2-base:2.0.-php-fpm
#FROM codemix/yii2-base:2.0.-hhvm # Composer packages are installed first. This will only add packages
# that are not already in the yii2-base image.
COPY composer.json /var/www/html/
COPY composer.lock /var/www/html/
RUN composer self-update --no-progress && \
composer install --no-progress # Copy the working dir to the image's web root
COPY . /var/www/html # The following directories are .dockerignored to not pollute the docker images
# with local logs and published assets from development. So we need to create
# empty dirs and set right permissions inside the container.
RUN mkdir -p runtime web/assets \
&& chown www-data:www-data runtime web/assets # Expose everything under /var/www (vendor + html)
# This is only required for the nginx setup
VOLUME ["/var/www"]