docker - 仅在Docker中重建代码

标签 docker dockerfile

我有相当大的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/

相关文章:

docker - Dial-http tcp 127.0.0.1:51640:意外的EOF

bash - 设置文本编辑器,如 Vim、Spacemacs 以使用 docker 容器,而无需在主机上安装工具

python - 在另一个容器内使用Docker容器的数据

docker - docker 如何检测应保存哪些更改,不保存哪些更改?

node.js - 是否可以忽略 docker 容器中已安装卷中的子文件夹(例如 node_module)?

security - Docker 中的 Volume 是一个安全漏洞吗?

docker - 如何引用存储在单独容器中的Flask HTML模板?

Docker COPY 目标路径未找到

asp.net - 在 docker 中安装 .net framework 4.7.2

apache - Docker CentOS 镜像不会自动启动 httpd