Xen_mar
Xen_mar

Reputation: 9702

Overwriting dockercompose.yml with docker-compose.prod.yml

I hope this is an easy one and will help some more people since I could not find a straight answer to this.

I am following this example: https://docs.docker.com/compose/extends/#different-environments

I have two files:

Docker compose for development

version: '3.7'

services:
  restapi:
    build: "./something/"
  db:
    image: postgres:10.5-alpine
volumes:
  postgres_data:
  static_volume:

Docker compose for production (docker.compose.prod.yml):

version: '3.7'

services:
  restapi:
    image: registry.gitlab.com/...
    restart: always
  db:
    restart: always

I the docker documentation it says:

In the case of build and image, when using version 1 of the Compose file format, using one option in the local service causes Compose to discard the other option if it was defined in the original service.

Yet, when I try to run the docker-compose on my server, I fails because the build path does not exist, which means the entry was not overwritten. What is the problem?

This commands fails:

sudo docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

With:

build path /.../.../.../.../... either does not exist, is not accessible, or is not a valid URL.

Upvotes: 0

Views: 619

Answers (1)

Adi Darachi
Adi Darachi

Reputation: 2175

You can specify multiple docker-compose config files, which will override each other from right to left.

In the following example, prod.yml will override dev.yml.

docker-compose -f /path/to/docker-compose-a.yml -f /path/to/docker-compose-b.yml up.

SO, to solve your issue, I would do the following.

create 3 .yml files,

  • common - will be the base for both environments.
  • prod - will be prod only
  • dev - will be dev only (will hold the build-path)

then to run dev environment: (common.yml + dev.yml)

docker-compose -f /path/to/docker-compose-common.yml -f /path/to/docker-compose-dev.yml up.

and production (common.yml + prod.yml):

docker-compose -f /path/to/docker-compose-common.yml -f /path/to/docker-compose-prod.yml up.

Upvotes: 1

Related Questions