利用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 文件修改参考
1 # For *-apache base image 2 # web: 3 # build: ./ 4 # ports: 5 # - "8080:80" 6 # expose: 7 # - "80" 8 # volumes: 9 # - ./:/var/www/html/ 10 # links: 11 # - db 12 # environment: 13 # ENABLE_ENV_FILE: 1 14 # ENABLE_LOCALCONF: 1 15 # API_TOKEN: "<YOUR GITHUB API TOKEN>" 16 17 # Uncomment to autostart at boottime 18 #restart: always 19 20 # Uncomment to send log output to syslog 21 #log_driver: "syslog" 22 #log_opt: 23 # tag: "docker-web" 24 25 # for *-php-fpm and *-hhvm base image 26 app: 27 build: ./ 28 expose: 29 - "9000" 30 volumes: 31 - ./:/var/www/html/ 32 links: 33 - db 34 environment: 35 ENABLE_ENV_FILE: 1 36 ENABLE_LOCALCONF: 1 37 API_TOKEN: "f1528c9bc2181668bc80417e33a7fb79412bf52a" 38 39 nginx: 40 build: ./nginx 41 ports: 42 - "8083:80" 43 links: 44 - app 45 volumes_from: 46 - app 47 48 db: 49 image: mysql:5.6 50 ports: 51 - "3308:3306" 52 expose: 53 - "3308" 54 environment: 55 MYSQL_ROOT_PASSWORD: secret-root 56 MYSQL_DATABASE: web 57 MYSQL_USER: web 58 MYSQL_PASSWORD: web 59 60 # Uncomment to autostart at boottime 61 #restart: always
Dockerfile 文件修改参考
1 #FROM codemix/yii2-base:2.0.7-apache 2 FROM codemix/yii2-base:2.0.7-php-fpm 3 #FROM codemix/yii2-base:2.0.7-hhvm 4 5 6 # Composer packages are installed first. This will only add packages 7 # that are not already in the yii2-base image. 8 COPY composer.json /var/www/html/ 9 COPY composer.lock /var/www/html/ 10 RUN composer self-update --no-progress && \ 11 composer install --no-progress 12 13 # Copy the working dir to the image's web root 14 COPY . /var/www/html 15 16 # The following directories are .dockerignored to not pollute the docker images 17 # with local logs and published assets from development. So we need to create 18 # empty dirs and set right permissions inside the container. 19 RUN mkdir -p runtime web/assets \ 20 && chown www-data:www-data runtime web/assets 21 22 # Expose everything under /var/www (vendor + html) 23 # This is only required for the nginx setup 24 VOLUME ["/var/www"]