Home > Back-end >  docker-compose context references directories that are not related
docker-compose context references directories that are not related

Time:03-06

I made two files like this:

# file: /home/john/work/docker/build.yml


version: "3.8"
services:
  website:
    build:
      context: ../
      dockerfile: ./docker/docker-files/build.dockerfile
    entrypoint: ["tail", "-f", "/dev/null"]
# file: /home/john/work/docker/docker-files/build.dockerfile
FROM mysql:latest

Then I ran the following commands:

cd /home/john/work/docker
docker-compose -f build.yml up --build -d

But this gave the error

error checking context: 'no permission to read from '/home/john/work/db/docker-john/mongo/WiredTiger''.

I don't understand why the error mentions /home/john/work/db/docker-john/mongo/WiredTiger? It's not even related to my docker project and I make no mention of it! If I delete the directory /home/john/work/db, then my docker-compose -f build.yml up --build -d works perfectly.

What's going on? Why is docker-compose referencing directories that have nothing to do with what I want?

CodePudding user response:

The (first) Compose file mentioned with a docker-compose -f option is in /home/john/work/docker, so all paths in all Compose files are considered relative to this directory.

When you build the image, you specify its parent as the build context:

build:
  context: ../

Relative to the Compose file's directory, this is the /home/john/work directory.

Docker sends itself a copy of everything in the context directory, less what's in that directory's .dockerignore file. If a subdirectory of that is unreadable then you'll get the sort of error you describe.

I'm guessing you don't actually mean to copy the parent directory into the image, and you need to change

build:
  context: .

Double-check file paths in the Dockerfile: the left-hand side of any COPY (and ADD) directives is always relative to this context: directory (with leading .. and leading / removed).

Consider naming the Dockerfile exactly Dockerfile and putting it in the top-level directory of the project to simplify this further. If this build fragment is only used on developer systems but is always used there, you can also name the file docker-compose.override.yml and Compose will automatically use that as well.

# docker-compose.override.yml
# (only for development, not for production)
version: '3.8'
services:
  website:
    build: .
  • Related