利用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:
#         - "80"
#     volumes:
#         - ./:/var/www/html/
#     links:
#         - db
#     environment:
#         ENABLE_ENV_FILE: 1
#         ENABLE_LOCALCONF: 1
#         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:
       - "9000"
   volumes:
       - ./:/var/www/html/
   links:
       - db
   environment:
       ENABLE_ENV_FILE: 1
       ENABLE_LOCALCONF: 1
       API_TOKEN: "f1528c9bc2181668bc80417e33a7fb79412bf52a"

nginx:
   build: ./nginx
   ports:
       - "8083:80"
   links:
       - app
   volumes_from:
       - app

db:
    image: mysql:5.6
    ports:
        - "3308:3306"
    expose:
        - "3308"
    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.7-apache
FROM codemix/yii2-base:2.0.7-php-fpm
#FROM codemix/yii2-base:2.0.7-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"]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值