skip to Main Content

I’m new to the world of containers and specially when it comes to Docker Compose. I’m confused about some concepts and I can’t find information about them.

Basically I want to know if I can handle settings in different "docker-compose.yml" files in a isolated manner. I explain better… I would like to know if I can up or stop resources referring to a specific "docker-compose.yml" individually.


PLUS:

To better explain my doubt I’ll show you some conjectures about what I’m trying to explain.

It seems to me that it is possible to have multiple configurations for Docker Compose using different ".yml" files like the example below…

EXAMPLE

docker-compose -f docker-compose.a.yml -f docker-compose.b.yml up -d

… and that I can also handle each of these settings individually, such as stopping all the resources referring to a specific docker-compose.yml

EXAMPLE

docker-compose -f docker-compose.b.yml stop

[Ref(s).: https://gabrieltanner.org/blog/docker-compose#using-multiple-docker-compose-files , https://stackoverflow.com/q/29835905/3223785 , https://stackoverflow.com/questions/42287426/docker-multiple-environments , https://runnable.com/docker/advanced-docker-compose-configuration ]

3

Answers


  1. Chosen as BEST ANSWER

    All docker-compose subcommands (up, stop, down...) must be executed consuming a docker-compose<.SOME_OPT_VAL>.yml file.

    This docker-compose.yml file must be in the folder where the docker-compose command is executed or must be informed via the -f flag. This way, these subcommands will be executed on the "services" (resources) defined in the docker-compose.yml file.

    There is also the possibility of defining the service where a certain subcommand will be executed...

    MODELS

    docker-compose <SUBCOMAMND> <OPT_SERVICE_NAME>
    docker-compose -f <DOCKER_COMPOSE_YML> <SUBCOMAMND> <OPT_SERVICE_NAME>
    

    EXAMPLES

    docker-compose stop api
    docker-compose -f docker-compose.yml stop api
    

  2. Yes, it is possible. I’m not exactly sure what you are trying to do, but to be able to manage the services using -f option the way that you described, there shouldn’t be a service with the same name on multiple files.
    For example, if you have a service called db in docker-compose.a.yml and one other db service in docker-compose.b.yml. The following command will only built one container for db service:

    docker-compose -f docker-compose.a.yml -f docker-compose.b.yml up -d
    

    Take a look at -p option. It will make a project with the services isolated inside it. Then you can manage them using following commands with the same docker-compose.yml file:

    docker-compose -p foo up -d
    docker-compose -p foo stop [service_name]
    
    
    Login or Signup to reply.
  3. yes you can.

    It is just a matter of preference but i usually create a folder for every project i have, each of them have a unique docker-compose.yml file in it with all its dependencies (frontend / database /redis)

    Then to start a specific project i just go inside its folder and run docker-compose up. it then only starts this project without touching others.

    you can also type this if you only want to start redis.

    docker-compose up redis
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search