Docker Ignore 不适用于 docker compose 和我的文件结构 [英] Docker Ignore is not woking well with docker compose and my file structure

查看:22
本文介绍了Docker Ignore 不适用于 docker compose 和我的文件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个新的 dockerfile 和 docker-compose.yml.我将在下面首先展示我的带有 rails 的文件结构,我想使用新的 .dockerignore 来忽略 node_modules.git.

我的 docker 版本 是:Docker 版本 18.09.0,构建 4d60db4

我的 docker-compose.yml 比我使用的是 v3

根目录

- Gemfile- 应用程序- 斌- 电缆- D b- 配置- 库- 节点模块- 上市- 小贩- 码头工人- 应用程序- Dockerfile- .git- .gitignore- .dockerignore- 码头工人-compose.yml

docker/app/Dockerfile

FROM ruby​​:2.3.1ARG RAILS_ROOT运行 mkdir -p $RAILS_ROOT工作目录 $RAILS_ROOT复制 ..

docker-compose.yml

<上一页><代码>版本:'3'服务:应用程序:构建:&build_core语境: .dockerfile: ./docker/app/Dockerfile参数:- APP_NAME=myappname- RAILS_ROOT=/var/www/${APP_NAME}_web- RAILS_ENV=发展- RAILS_SERVE_STATIC_FILES=true- RAILS_LOG_TO_STDOUT=true- BUNDLE_OPTIONS=- NPM_OPTIONS=图片:自定义网络:1.0容器名称:应用程序卷:- .:/var/www/${APP_NAME}_web环境文件:- .env环境:RAILS_ROOT: "/var/www/${APP_NAME}_web"

.dockerignore

<上一页><代码>.git/节点模块/

我用下一个命令构建了我的图像

docker-compose 构建应用

当我查看容器时,文件文件仍在复制到我的容器中

也许是上下文或者我应该把我的 .dockerignore 我也把我的 .dockerignore 放到 docker/app/.dockerignore 中,我也有同样的问题.

解决方案

在构建时,指令 COPY ..(在 Dockerfile 内)正确复制 $RAILS_ROOT(在图像内)的 .dockerignore 中未列出的所有文件.这里没问题(通过运行 docker run --rm custom-web:1.0 ls -al 进行检查).

但是在这里你运行 docker-compose 来启动容器,并且你已经定义了一个卷:

卷:- .:/var/www/${APP_NAME}_web

这意味着与 docker-compose.yml 位于同一目录中的文件在主机和容器之间共享.这就是为什么您在启动后会在 $RAILS_ROOT(custom-web:1.0 图像的工作目录)中找到所有文件(甚至是 .dockerignore 中列出的文件)通过 docker-compose 的容器.

如果您确实需要在主机和容器之间共享文件(通过卷),我建议您将当前目录挂载到 Dockerfile 中指定的位置以外的另一个位置,喜欢:

卷:- .:/${APP_NAME}_web

否则,使用 COPY .. 而且这里有一个卷是多余的.

I'am setting up a new dockerfile and docker-compose.yml. I'll below show first my file structure with rails and I want to use a new .dockerignore to ignore node_modules, .git .

my docker version is: Docker version 18.09.0, build 4d60db4

my docker-compose.yml than I use is v3

Root Directory

- Gemfile
- app
- bin
- cable
- db
- config
- lib
- node_modules
- public
- vendor
- docker
  - app
    - Dockerfile
- .git
- .gitignore
- .dockerignore
- docker-compose.yml

docker/app/Dockerfile

FROM ruby:2.3.1
ARG RAILS_ROOT
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
COPY . .

docker-compose.yml


version: '3'
services:
  app:
    build: &build_core
      context: .
      dockerfile: ./docker/app/Dockerfile
      args:
        - APP_NAME=myappname
        - RAILS_ROOT=/var/www/${APP_NAME}_web
        - RAILS_ENV=development
        - RAILS_SERVE_STATIC_FILES=true
        - RAILS_LOG_TO_STDOUT=true
        - BUNDLE_OPTIONS=
        - NPM_OPTIONS=
    image: custom-web:1.0
    container_name: app
    volumes:
      - .:/var/www/${APP_NAME}_web
    env_file:
      - .env
    environment:
      RAILS_ROOT: "/var/www/${APP_NAME}_web"

.dockerignore


.git/
node_modules/

I built my image with the next command

docker-compose build app

When I look at the container, the file files are still copying to my container

maybe is the context or where I should put my .dockerignore also I put my .dockerignore into docker/app/.dockerignore and I have the same problem.

解决方案

At build time, the directive COPY . . (inside the Dockerfile) correctly copies all files not listed in .dockerignore in $RAILS_ROOT (inside the image). No problem here (check that by running docker run --rm custom-web:1.0 ls -al).

But here you run docker-compose to start the container, and you have defined a volume :

volumes:
    - .:/var/www/${APP_NAME}_web

That means that files from the same directory as docker-compose.yml are shared between the host and the container. That's why you find all files (even those listed in .dockerignore) in $RAILS_ROOT (workdir of custom-web:1.0 image) after starting the container via docker-compose.

If you really need to share files between your host and the container (via a volume), I'll suggest you to mount the current directory in another location than the one specified in your Dockerfile, like :

volumes:
    - .:/${APP_NAME}_web

Otherwise, using COPY . . and a volume is redundant here.

这篇关于Docker Ignore 不适用于 docker compose 和我的文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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