Docker Compose: How to specify path to docker file while building as if in different directory?

时光总嘲笑我的痴心妄想 提交于 2021-01-27 07:20:47

问题


docker build -t test2 -f tests/low_conviction_integration/Dockerfile .

If I need to copy a file from a directory ABOVE my Dockerfile, I can accomplish it by calling

docker build -t image_name -f path_to_docker_file/Dockerfile .

from that ABOVE directory

How can I accomplish the same behavior with docker-compose? I want docker-compose to exist in same directory as Dockerfile, to build with Dockerfile, but to ultimately call it as if it was in ABOVE directory, so that I can copy the correct file. I tried

docker-compose -f path_to_docker_file_and_compose/docker-compose.yml up

from ABOVE directory, but when building from the Dockefile, it complains the files in ABOVE directory are not there to copy when building (which means it is running the docker build from the same directory as the Dockerfile. I need it to run the build from the ABOVE directory).


回答1:


This can be done like this, add build in docker-compose.yml and run the build from ABOVE directory:

version: '3.8'

services:
  your_service:
    build: # "context" and "dockerfile" fields have to be under "build"
      context: .
      dockerfile: <Below_Directory>/Dockerfile



回答2:


You might want to use the -p flag

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name (default: directory name)


来源:https://stackoverflow.com/questions/51563603/docker-compose-how-to-specify-path-to-docker-file-while-building-as-if-in-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!