docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.81k stars 5.21k forks source link

Compose with variable on image name #8601

Closed cfeltz34 closed 3 years ago

cfeltz34 commented 3 years ago

Description of the issue

Context information (for bug reports)

Output of docker(-)compose version

V2.0.0-rc.2

Output of docker version

 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:10 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Steps to reproduce the issue

  1. Create a yml file and define a service like (where .env file with "RABBITMQ_VERSION=3.8.9")
    
    version: '3.7'
    services:
    rabbitmq:
    container_name: example.rabbitmq
    image: rabbitmq:$RABBITMQ_VERSION-management

My container is based on image "rabbitmq:3.8.9" instead of "rabbitmq:3.8.9-management"
cfeltz34 commented 3 years ago

No issue with docker compose 1.29.2

ndeloof commented 3 years ago

I just tested with rc3 and issue has been fixed it seems:

$ docker compose config
services:
  rabbitmq:
    container_name: example.rabbitmq
    image: rabbitmq:3.8.9
(...)
$ rm .env 
$ docker compose config
services:
  rabbitmq:
    container_name: example.rabbitmq
    image: rabbitmq:management
(...)
$ RABBITMQ_VERSION=foo docker compose config
services:
  rabbitmq:
    container_name: example.rabbitmq
    image: rabbitmq:foo
(...)
ndeloof commented 3 years ago

Please re-open if issue persist after upgrade to v2.0.0-rc.3

cfeltz34 commented 3 years ago

I still have the issue with rc3 but I don't know how to reopen the issue

.env

RABBITMQ_VERSION=3.8.9

docker-compose.test.rabbitmq.yml

version: '3.7'

services: rabbitmq: container_name: test.rabbitmq image: rabbitmq:$RABBITMQ_VERSION-management environment:

  • RABBITMQ_DEFAULT_USER=RabbitMqUser
  • RABBITMQ_DEFAULT_PASS=Pass@word34 volumes:
  • rabbitmq-data:/var/lib/rabbitmq ports:
  • 45672:5672
  • 45674:15672 # managment plugin networks:
  • network

volumes:
rabbitmq-data:

networks: network:

Command :

docker-compose -p TestBug -f docker-compose.test.rabbitmq.yml up -d --build docker ps --> CONTAINER ID IMAGE
--> d6c0c157275b rabbitmq:3.8.9

This container should be based on rabbitmq:3.8.9-management and not on rabbitmq:3.8.9

ndeloof commented 3 years ago

I think I get it: $RABBITMQ_VERSION-management syntax actually means "variable RABBITMQ_VERSION, defaults to management if not set" (see https://docs.docker.com/compose/environment-variables/#substitute-environment-variables-in-compose-files)

defining variable without explicit ${} makes this syntax ambiguous, so the confusion here. Better use ${RABBITMQ_VERSION}-management to make the variable boudaries explicit within this composed value

ndeloof commented 3 years ago

As a side-note, while docker-compose python only applies bash-style rules to braced variables, compose-go doesn't. I wonder this is something we should fix @ulyssessouza ?

ulyssessouza commented 3 years ago

I agree that it should only apply with the strict mode (the one with ${}), otherwise that become ambiguous. Even the documentation only uses this notation. I can have a look at godotenv to check how hard it is to distinguish between the 2 and apply the default.

ndeloof commented 3 years ago

This isn't godotenv but https://github.com/compose-spec/compose-go/blob/de56f4f0cb3c925f41df594221148613534c2cd3/template/template.go#L30-L32