skip to Main Content

I’m a beginner in docker / docker-compose and i’m trying to run a container from a local docker file

my dockerFolder look like this

├── docker-compose.yml
└── dockerfiles
    ├── dockerfile.test1
    └── dockerfile.test2

What did i should put on the docker-compose file ?

I try it but it doesn’t work

version: "3.9"
services:
  test1:
    build: dockerfiles/dockerfile.test1
  test2:
    build: dockerfiles/dockerfile.test2

(I had warned that I’m a beginner)

2

Answers


  1. You can use context and dockerfile to specify a custom Dockerfile name like you are doing:

    version: "3.9"
    services:
      test1:
        build:
          context: dockerfiles
          dockerfile: dockerfile.test1
      test2:
        build:
          context: dockerfiles
          dockerfile: dockerfile.test2
    

    You can also keep the build but naming your files Dockerfile and put them of two distinct directories.

    Login or Signup to reply.
  2. docker-compose does not recognize your context, the build argument requires the context (the folder where your Dockerfile is located).
    to build from dockerfiles named differently, you must first specify the context and then the name of your dockerfile
    as in the following example

    version: "3.9"
    services:
      test1:
        build:
          context: ./dockerfiles
          dockerfile: dockerfile.test1
    
      test2:
        build: 
        context: ./dockerfiles
        dockerfile: dockerfile.test1
    

    where dockerfile is a subdirectory of parent directory wich contain the docker-compose file

    check the docker-compose file reference for more details https://docs.docker.com/compose/compose-file/build/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search