简体   繁体   中英

How should define Dockerfile & docker-compose if they are placed in project subdirectory?

project-root/
├─ build/
│  ├─ Dockerfile
│  ├─ docker-compose.yml
├─ internal/
│  ├─ ...
├─ ...

Dockerfile:

...

WORKDIR /app

COPY go.mod go.mod
COPY go.sum go.sum

...

docker-compose.yml:

...

services:
  api:
    container_name: 'api'
    build: ./build/
    ports:

...

after run command:

docker compose --project-directory . up

get error:

failed to solve: rpc error: code = Unknown desc = failed to compute cache key: "/go.mod" not found: not found

It is really a lot easier to keep your docker files in the root. But you can do this the way you want, it is just somewhat more confusing, and possibly more difficult therefore to maintain.

First: in your docker-compose you have build: ./build/ Since the docker-compose file itself is located inside build directory it will look for build/build that will not work.

Then it is important to understand the concept of context/path in docker. It will take whatever you have in that directory, and send it to the docker daemon, any files outside of it, even if their paths are corrrectly written in the dockerfile will not be found. The paths of files inside the dockerfile, for instance for COPY commands, are relative to this context.

So build: ./build/ sends everything in the build directory to the daemon but anything outside will fail to be found. You can the say

build:
  context: ..
  dockerfile: Dockerfile

That should then work again, if you have any paths in your dockerfile relative to the context! So lets say you need to copy ../internal/somefile then you would need: COPY./internal/somefile./somewhere

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM