Docker:当 Dockerfile 位于子目录中时使用 COPY [英] Docker: Using COPY when the Dockerfile is located in a subdirectory

查看:30
本文介绍了Docker:当 Dockerfile 位于子目录中时使用 COPY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 多个 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 中,我想使用 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 文件部署应用程序:

And I want to deploy the app using my docker-compose.yml file:

# docker-compose.yml
version: "3"
services:
   webserver:
      build: ./dockerfiles/webserver
      image: webserver:php-apache

但是,当我从工作目录 (app) 运行 docker-compose 时,我收到以下错误:

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

如果我将网络服务器的 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...

推荐答案

你需要做的就是在你的 build 部分中添加 context: .dockerfiledocker-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆