我有相当大的yii2应用程序,需要花费几分钟才能在docker中构建。有没有一种方法可以只生成代码,而不必每次都“重新安装”所有内容?如何加快dockerized yii2应用程序的开发/调试?
现在,我这样做:
docker build -t myapp:mytag . docker run --name myapp -p 8000:8000 myapp:mytag
我的Dockerfile:
FROM php:5.6-apache
COPY . /var/www/html/
ENV APACHE_DOCUMENT_ROOT /var/www/html/web
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN apt-get update && \
apt-get install -y curl nano unzip zlib1g-dev git && \
docker-php-ext-install pdo pdo_mysql zip && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN cd /var/www/html && composer install
RUN cd /var/www/html/ && mkdir web/assets/
RUN chmod 777 /var/www/html/web/assets/
RUN mv /var/www/html/vendor/bower-asset/ /var/www/html/vendor/bower/
最佳答案
Docker将重用以前执行但尚未更改的缓存构建步骤。但是,一旦到达破坏缓存的步骤,则所有后续步骤都必须重新运行,因为缓存包括对上一步的依赖性。因此,缓存是依赖于顺序的,您将以下作为第一步之一:
COPY . /var/www/html/
每次更改代码时,都必须重新运行该行,然后强制
apt-get
行也重新运行。通过重新排序安装,您会看到速度大大提高:FROM php:5.6-apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/web
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN apt-get update && \
apt-get install -y curl nano unzip zlib1g-dev git && \
docker-php-ext-install pdo pdo_mysql zip && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# move this line to as late in the build as possible
COPY . /var/www/html/
RUN cd /var/www/html && composer install
RUN cd /var/www/html/ && mkdir web/assets/
RUN chmod 777 /var/www/html/web/assets/
RUN mv /var/www/html/vendor/bower-asset/ /var/www/html/vendor/bower/
关于docker - 仅在Docker中重建代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48169857/