我在子目录中有 docker-compose 的小配置,其中包含 Dockerfiles 和 .dockerignore 文件。我不明白为什么我的图像在构建后包含所有内容,即使我在 .dockerignore 中有一些要忽略的项目。这是我所拥有的
docker-compose.yml
docker
├── app
│ ├── Dockerfile
│ └── .dockerignore
└── web
├── Dockerfile
├── nginx.conf
└── .dockerignore
.dockerignore 示例:**/.dockerignore
docker-compose.yml
log
**/log
../../log
我只是按如下方式运行:docker-compose up -d --build --no-cache
以上都没有被忽略..我做错了什么?docker-compose version 1.23.2, build 1110ad01
为应用服务添加 Dockerfile(请参阅 docker-compose)。 Docker-compose 使用 ROR app 和 nginx 打包了两个镜像。对于两个 dockerfile,我都有 dockerignore。所以我的理解是我可以说出我想要的。但事实并非如此。如何忽略包中的某些文件?或者我应该把我的 Dockerfile 和 dockerignore 放在不同的路径中,因为构建只在上下文中工作..我很困惑。FROM ruby:2.4.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs imagemagick
# Configuring main directory
RUN mkdir -p /var/www/example.com
WORKDIR /var/www/example.com
# Setting env up
ENV RAILS_ENV='production'
ENV RACK_ENV='production'
# Adding gems
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
# Adding project files
COPY . .
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
所以我认为COPY . .
首先检查 .dockerignore .. 没有?
最佳答案
你有 2 Dockerfiles
,它们有各自的上下文。在每个上下文(文件夹)中,您都有 .dockerignore
,这是正确的。但是.dockerignore
不在构建上下文之外工作,这意味着使用 ../../
是错的。
我猜你是假设这些文件适用于 docker
文件夹,但实际上它们适用于 docker/app
和 docker/web
分别。
构建过程如下:
第一 docker cli
打包上下文(如果未明确指定,则包含 Dockerfile
的文件夹)并将其发送到 docker daemon
.没什么外 此文件夹已发送。它是为 COPY
明确写的命令,用于将上下文项放入容器:
The
src
path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
如果有
.dockerignore
, 忽略的文件 排除在外 包装前。
关于Docker-compose 和 .dockerignore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53945934/