【发布时间】:2026-02-12 00:25:04
【问题描述】:
我在子目录中有 Dockerfiles 和 .dockerignore 文件的 docker-compose 小配置。我无法理解为什么构建后的图像包含所有内容,即使我在 .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 放在不同的路径中,因为 build 只能在上下文中工作。我很困惑。
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 .. 不是吗?
【问题讨论】:
-
尝试
*而不是**。我认为**在.dockerignore中不起作用。另外,您的图片中究竟包含哪些您希望被排除在外的内容? -
docker-compose.yml文件的实际内容似乎对调试很重要;看到这两个Dockerfiles 也不会受到伤害。如果您使用 Docker 绑定挂载 (volumes:) 将映像中的所有内容替换为本地源代码树,则不会查看.dockerignore文件。 -
构建应该忽略这些文件,因此您需要提供minimal reproducible example 以获得帮助。您的示例不完整,并且没有减少到允许其他人尝试重现该问题所需的最低限度。
-
您在使用卷吗?如果您使用卷,则 .dockerignore 将忽略这些卷所镜像的文件。
标签: docker docker-compose