如何从多个Dockerfile中的共享目录中复制文件?

时间:2015-04-20 07:25:43

标签: docker dockerfile docker-compose

在我的应用程序中,我有5个docker容器,每个容器都是从Dockerfile构建的映像开始的。以下是结构:

-projectdir
  -docker
    -application1
      -Dockerfile
      -script.sh
    -application2
      -Dockerfile
      -script.sh
    -application3
      -Dockerfile
    -application4
      -Dockerfile
      -script.sh
    -application5
      -Dockerfile

script.sh被复制到应用程序1,2和4中的Dockerfile中。问题是我必须在每个应用程序目录中放置相同的script.sh.有没有办法使用包含单个script.sh的共享文件夹并从中复制?我正在使用docker-compose来构建和运行容器。

2 个答案:

答案 0 :(得分:5)

您可以定义一个专用于将脚本保存在volume(作为data volume container

的容器
scripts:
  volumes:
    - /path/to/scripts
application1:
  volumes_from:
    - scripts
application2:
  volumes_from:
    - scripts

/path/to/scripts文件夹将在每个应用程序中共享 scripts Dockerfile应该创建/path/to/scripts并复制其中的script.sh

答案 1 :(得分:2)

从Docker 1.5开始,您可以在Dockerfile的构建上下文中指定路径。以下是有效的:

-projectdir
  -docker
    -script.sh
    -application1
      -Dockerfile  
    -application2
      -Dockerfile
    ...

您可以使用以下内容进行构建:

$ cd docker
$ docker build -f application1/Dockerfile .

不幸的是,我不认为您可以使用Compose执行此操作,因此您可能必须使用单独的脚本来构建和标记您可以在Compose中使用的图像。

相关问题