Docker:当Dockerfile位于子目录中时使用COPY [英] Docker: Using COPY when the Dockerfile is located in a subdirectory
问题描述
我正在使用多个dockerfiles (每个服务一个)来构建应用。我的应用程序的目录结构如下:
I'm building an app using multiple dockerfiles (one for each service). My app's directory structure is as follows:
app
├── dockerfiles
│ ├── webserver
│ │ └── Dockerfile
│ └── database
│ └── Dockerfile
├── public
└── <frontend>
├── db
└── <data>
[...]
├── LICENSE
├── README.md
└── docker-compose.yml
在我的网络服务器的 Dockerfile
中,我想使用<$ c $复制到现有代码中c> COPY 命令:
In my webserver's Dockerfile
, I want to copy in my existing code using the COPY
command:
# Dockerfile
COPY ./public /var/www/html
我想使用 docker-compose部署应用程序。 yml
文件:
# docker-compose.yml
version: "3"
services:
webserver:
build: ./dockerfiles/webserver
image: webserver:php-apache
但是,当我从工作目录中运行 docker-compose
( app
),则出现以下错误:
However, when I run docker-compose
from the working directory (app
), I get the following error:
Building webserver
Step 1/2 : FROM php:7.1.11-apache-jessie
---> cb6a5015ad72
Step 2/2 : COPY ./public /var/www/html
Service 'webserver' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder193736188/public: no such file or directory
如果我移动Web服务器的 Dockerfile,此错误就会消失
到应用程序的根目录,所以我知道它是由路径或构建上下文问题引起的。
This error disappears if I move my webserver's Dockerfile
to the app's root, so I know that it's being caused by a paths or build context issue.
知道了这一点,我们可以解决通过以下两种方式之一解决问题之一:
And knowing this, we can fix the problem one of two ways, by either:
(1)在整个应用中使用一个 Dockerfile
(在应用程序的根目录),或
(1) Using one Dockerfile
for the entire app (in the app's root), or
app
└── Dockerfile
(2)为每个服务使用多个 Dockerfile
(在应用程序的根目录中)。
(2) Using multiple Dockerfiles
for each service (in the app's root).
app
├── Dockerfile.webserver
└── Dockerfile.database
这些解决方案很糟糕,因为对所有内容使用一个dockerfile / container并不是最佳实践(1),并且以这种方式组织多个dockerfile看起来很混乱(2)。
These solutions are bad because using one dockerfile/container for everything is not best practice (1), and having multiple dockerfiles organized in this way just looks messy (2).
所以,我的问题是:
如何在不更改原始目录结构的情况下解决此问题?
How do we fix this problem without changing our original directory structure?
- 需要对dockerfile进行哪些更改,更改为
docker-compose.yml
还是基本的运行时命令? - 是否有更好的方法来组织所有内容?
-
WORKDIR
命令如何?
- What changes need to be made to the dockerfiles, to
docker-compose.yml
, or to the basic runtime commands? - Is there a better way to organize everything?
- What about the
WORKDIR
command?
理想情况下,最好的解决方案应该同时适用于开发(本地)环境和生产(远程)环境,因此让我们暂时避免批量生产...
Ideally, the best solution should work for both dev (local) and production (remote) environments, so let's avoid volumes for now...
推荐答案
您要做的就是在构建部分添加上下文:。
和 dockerfile
在docker-compose.yml文件中,以便您的服务理解完整的目录结构。
All you need to do here is add context: .
and dockerfile
in your build section inside your docker-compose.yml file so that your service understands the complete directory structure.
# docker-compose.yml
version: "3"
services:
webserver:
build:
context: .
dockerfile: ./dockerfiles/webserver/Dockerfile
image: webserver:php-apache
这篇关于Docker:当Dockerfile位于子目录中时使用COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!