3

Here is my docker-compose.yml:

version: '2'
services:
web:
  build: .
  context: ./
  dockerfile: Dockerfile
  ports:
    - "8080:8080"
  container_name: demo
  volumes:
    - .:/images

I got an error:

ERROR: The Compose file './docker-compose.yml' is invalid because: Unsupported config option for services.web: 'dockerfile'

So I would like to build the container and also run. I will have a few more containers to build and run. Is it possible by docker-compose?

8
  • Have you tried removing the dockerfile option? Since the error clearly says this shouldn't be there
    – Jimi
    Commented Feb 18, 2020 at 11:47
  • 2
    You don't need context: or dockerfile: here. If you kept them, they need to be indented one step more and you need to remove the context directory name from the build: line.
    – David Maze
    Commented Feb 18, 2020 at 11:52
  • @FrancescoGimignano I have to build the container. Without those how can I build?
    – masiboo
    Commented Feb 18, 2020 at 11:55
  • @DavidMaze could tell more what do u mean by emove the context directory name from the build:
    – masiboo
    Commented Feb 18, 2020 at 11:56
  • 1
    You need build: ., or build: { context: . }, but not both.
    – David Maze
    Commented Feb 18, 2020 at 12:02

3 Answers 3

6

If I'm not mistaken the issue is after build you have a dot, the dockerfile and context are not correct idented. And I think the bar in the context should not be there.

version: "3.7"
services:
  api:
    image: image-name
    build:
      context: .
      dockerfile: path/to/Dockerfile
    container_name: container-name

I think this resolves the issue you have, but couldn't test it.

5
  • Can u pls share the whole yml. Coz, I am confused under which level the build: will be
    – masiboo
    Commented Feb 18, 2020 at 12:05
  • I completed more the file
    – Alexandre
    Commented Feb 18, 2020 at 13:22
  • 1
    ERROR: Version in "./docker-compose.yml" is unsupported. But if I add version: "2" . It works fine.
    – masiboo
    Commented Feb 18, 2020 at 13:32
  • that configuration works for me with the docker-compose version 1.22.0
    – Alexandre
    Commented Feb 18, 2020 at 13:55
  • main docker-compose version 1.17.1, build unknown on ubuntu 18
    – masiboo
    Commented Feb 18, 2020 at 14:58
0

You need to respect the indentations , here is an example for building Nginx docker image https://github.com/wshihadeh/three_methods_to_share_assets/blob/master/precompile_during_docker_build/docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: nginx
      dockerfile: Dockerfile
    command: server
    ports:
      - 80:80

0

YAML files works on indentation and an indentation error marks the yaml as invalid. Always validate your yaml in appropriate linting tool for e.g. https://www.yamllint.com/

version: '3.7'
services:
  web:
    image: my-web-service
    build:
      dockerfile: Dockerfile
      context: .
    ports:
      - '9080:8080'

context value is . because my Dockerfile and docker-compose.yaml files exists at the same location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.