为什么 docker-compose 没有以正确的顺序构建容器?
Why is docker-compose not building the containers in the correct order?
这是docker-撰写
version: '3'
services:
creator:
build:
context: .
dockerfile: Dockerfile.php
image: creator_image
container_name: creator_container
restart: always
volumes:
- ./:/var/www
- ./config/php/php.ini:/usr/local/etc/php/conf.d/php.ini
artisan:
image: creator_image
command: sh -c 'php artisan optimize && php artisan config:cache && php artisan
view:cache && php artisan view:clear && php artisan config:cache'
container_name: creator_container_artisan
depends_on:
- creator
cron:
image: creator_image
command: sh -c '(crontab -l 2>/dev/null; echo "* * * * * cd /var/www && php
artisan schedule:run >> /dev/null 2>&1")| crontab - && /usr/sbin/crond
start -f'
container_name: cronjobs_container
restart: always
volumes:
- ./:/var/www
- ./config/php/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- creator
webserver:
build:
context: .
dockerfile: Dockerfile.nginx
image: nginx_image
container_name: nginx_container
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/logs:/var/logs
Docker build 会失败,会报如下错误
pull access denied for creator_image, repository does not exist or may require ‘docker login’
上面的 docker-compose 构建了 Laravel 容器和 Nginx,还清除了一些 Laravel 缓存并重新创建它,以及 运行s Laravel 定时任务。
执行 docker-compose up --build
时构建失败,因为 Docker 试图在 creator
之前 运行 artisan
容器
解决此问题的唯一方法是注释掉 artisan
和 cron
容器,并在构建完成后阅读它们。如何解决这个问题? depends_on
没有解决这个问题。
我添加多少 depends_on
并不重要,显然 it's deprecated,有什么方法可以强制 Docker 以正确的顺序构建容器?
Compose 没有任何构建顺序或构建依赖关系的概念。
另一方面,重建完全相同的图像几乎是免费的:Docker 可以检测到您在完全相同的输入文件上使用了完全相同的命令 运行 并跳过执行任何实际操作工作。如果您在所有三个容器中都有相同的 build:
块,Compose 将“构建”图像三次,但其中两次将使用 Docker 构建缓存,最后您将获得具有三个名称的单张图片。
如果您通过让每个容器 build:
使用(相同的)图像来清理它;删除 container_name:
和 image:
选项(Compose 为这些生成合理的默认值);删除 depends_on:
(容器之间显然没有建立网络连接);并删除 volumes:
安装(代码应 COPY
编辑到图像中);然后这减少到
version: '3.8'
services:
creator:
build: .
restart: always
artisan:
build: .
command: sh -c 'php artisan optimize && php artisan config:cache && php artisan
view:cache && php artisan view:clear && php artisan config:cache'
cron:
build: .
command: sh -c '(crontab -l 2>/dev/null; echo "* * * * * cd /var/www && php
artisan schedule:run >> /dev/null 2>&1")| crontab - && /usr/sbin/crond
start -f'
restart: always
这是docker-撰写
version: '3'
services:
creator:
build:
context: .
dockerfile: Dockerfile.php
image: creator_image
container_name: creator_container
restart: always
volumes:
- ./:/var/www
- ./config/php/php.ini:/usr/local/etc/php/conf.d/php.ini
artisan:
image: creator_image
command: sh -c 'php artisan optimize && php artisan config:cache && php artisan
view:cache && php artisan view:clear && php artisan config:cache'
container_name: creator_container_artisan
depends_on:
- creator
cron:
image: creator_image
command: sh -c '(crontab -l 2>/dev/null; echo "* * * * * cd /var/www && php
artisan schedule:run >> /dev/null 2>&1")| crontab - && /usr/sbin/crond
start -f'
container_name: cronjobs_container
restart: always
volumes:
- ./:/var/www
- ./config/php/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- creator
webserver:
build:
context: .
dockerfile: Dockerfile.nginx
image: nginx_image
container_name: nginx_container
restart: always
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/logs:/var/logs
Docker build 会失败,会报如下错误
pull access denied for creator_image, repository does not exist or may require ‘docker login’
上面的 docker-compose 构建了 Laravel 容器和 Nginx,还清除了一些 Laravel 缓存并重新创建它,以及 运行s Laravel 定时任务。
执行 docker-compose up --build
时构建失败,因为 Docker 试图在 creator
artisan
容器
解决此问题的唯一方法是注释掉 artisan
和 cron
容器,并在构建完成后阅读它们。如何解决这个问题? depends_on
没有解决这个问题。
我添加多少 depends_on
并不重要,显然 it's deprecated,有什么方法可以强制 Docker 以正确的顺序构建容器?
Compose 没有任何构建顺序或构建依赖关系的概念。
另一方面,重建完全相同的图像几乎是免费的:Docker 可以检测到您在完全相同的输入文件上使用了完全相同的命令 运行 并跳过执行任何实际操作工作。如果您在所有三个容器中都有相同的 build:
块,Compose 将“构建”图像三次,但其中两次将使用 Docker 构建缓存,最后您将获得具有三个名称的单张图片。
如果您通过让每个容器 build:
使用(相同的)图像来清理它;删除 container_name:
和 image:
选项(Compose 为这些生成合理的默认值);删除 depends_on:
(容器之间显然没有建立网络连接);并删除 volumes:
安装(代码应 COPY
编辑到图像中);然后这减少到
version: '3.8'
services:
creator:
build: .
restart: always
artisan:
build: .
command: sh -c 'php artisan optimize && php artisan config:cache && php artisan
view:cache && php artisan view:clear && php artisan config:cache'
cron:
build: .
command: sh -c '(crontab -l 2>/dev/null; echo "* * * * * cd /var/www && php
artisan schedule:run >> /dev/null 2>&1")| crontab - && /usr/sbin/crond
start -f'
restart: always