Home > other >  Running docker-compose up ends up with error "Service has neither an image nor a build context
Running docker-compose up ends up with error "Service has neither an image nor a build context

Time:11-29

My microservices project structure is like this:

my-service-one/
  - Dockerfile
  - ...
my-service-two/
  - Dockerfile
  - ...
docker-compose.yml

As you can see, each service directory contains a Dockerfile. There is a docker-compose.yml in the root level.

The docker-compose.yml :

version: "3"
services:
  service-one:
    container_name: service-one
    build:
      dockerfile: ./my-service-one/Dockerfile
    ports:
      - "8081:8081"
  
  service-two:
    container_name: service-two
    build:
      dockerfile: ./my-service-two/Dockerfile
    ports:
      - "8082:8082"

Now, I run docker-compose up -d from the root. I end up with error:

$ docker-compose up -d
ERROR: The Compose file is invalid because:
Service service-one has neither an image nor a build context specified. At least one must be provided.

My question is why does docker-compose think my service-one doesn't have a build context specified? Didn't I specify it already with:

build:
  dockerfile: ./my-service-one/Dockerfile

Why this error?

CodePudding user response:

Peovide a context property below both services in build section like that:

build:
      context: YOUR_DIRECTORY
      dockerfile: ./my-service-one/Dockerfile

YOUR_DIRECTORY is the place where the files for your project are listed.

Most probably YOUR_DIRECTORY is already written i the child .yml files.

You have a couple of main approaches:

  • To copy paste the context from the child .yml
  • To produce the docker build using the child .yml with a command like:

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up --build

CodePudding user response:

why does docker-compose think my service-one doesn't have a build context specified?

Weeeell, because you did not specified the build context.

Didn't I specify it already with:

No, you specified the dockerfile. No the context.

Why this error?

You have to specify the context so that docker knows what to build.

If you want to build with the context of current directory, you would do:

build:
  context: .
  dockerfile: ./my-service-two/Dockerfile

Maybe the context is inside my-service-two, I suspect youw antto write:

build:
  context: ./my-service-two
  dockerfile: ./Dockerfile

or really just:

build: ./my-service-two
  • Related