1

定位镜像

在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"]

西树先森
7.1k 声望926 粉丝

从事开发多年,前端、后端(go、Python、php)、服务架构都有涉猎,经历过大公司、创业公司,擅长前端及公司技术选型。