FG
💻 Software☁️ Cloud & DevOpsDocker

It should allow extended services to define links

Fresh5 days ago
Mar 14, 20260 views
Confidence Score77%
77%

Problem

Hi! I'm in a use-case where I have a basic docker-compose.yml file that defines a php service talking to a db service. Now I added a second file named "dev.yml" where I extend all the services from docker-compose.yml and I add a `volumes` key to the php service. This scenario does not work (`services with 'links' cannot be extended`) and I don't think the docs are quite clear on this behavor. (EDIT: it's clear but stay with me :)) Docs says that `links` won't be copied: [code block] What I'd like is to not being forced to create a prod.yml file, just to add a `links` section. I'm perfectly OK with the idea of always defining links locally (and not merging the array like it's done with `expose` f.e). But I think the base file can also be considered a perfectly valid leaf. I'd like my docker-compose.yml file to be the standard, and dev.yml to add some volumes (and define links locally in it). I hope I'm clear :) I think there is no reasion why I can't define links in the base file. It would be great if I just had to redefine them if needed in any other file that extends it.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Modify docker-compose to Allow Links in Extended Services

Medium Risk

Docker Compose does not allow services defined with 'links' to be extended in other files due to the way service definitions are merged. When extending services, certain properties like 'links' are not copied over, which leads to the inability to define them in the base file and extend them in the derived file.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Remove 'links' from Base File

    To allow the extension of services in 'dev.yml', remove the 'links' section from 'docker-compose.yml'. This will prevent the conflict that arises when trying to extend services with 'links'.

    yaml
    services:
      php:
        image: php:latest
        # Remove 'links' here
      db:
        image: mysql:latest
  2. 2

    Define 'links' in dev.yml

    In your 'dev.yml' file, redefine the 'links' section for the php service. This allows you to specify the links locally without conflicting with the base file.

    yaml
    version: '3.8'
    services:
      php:
        extends:
          file: docker-compose.yml
          service: php
        volumes:
          - ./local:/var/www/html
        links:
          - db
  3. 3

    Add Volumes in dev.yml

    Ensure that the 'volumes' key is properly defined in 'dev.yml' under the php service to mount the local directory for development purposes.

    yaml
    volumes:
      - ./local:/var/www/html
  4. 4

    Test the Configuration

    Run the docker-compose command with the dev file to ensure that the services are up and running with the correct links and volumes.

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

Validation

Confirm that the php service can communicate with the db service as expected and that the local volume is mounted correctly. You can check the logs or access the php service to verify the connection.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

dockerdocker-composecontainers