skip to Main Content

This is my docker compose file where I have redis service and volume declared dynamically

redis:
  container_name: redis-${ENV}
  image: redis
  command: --port ${REDIS_PORT}
  ports:
    - "${REDIS_PORT}:6379"
  volumes:
    - redis-${ENV}:/data
  networks:
    - app

volumes:
  redis:
    name: redis-${ENV}
    external: false

I get error: service "redis" refers to undefined volume redis-dev: invalid compose project

2

Answers


  1. Once I make your docker-compose.yml workable, by removing the undefined app network, which is not relevant, and adding the missing services section:

    services:
      redis:
        container_name: redis-${ENV}
        image: redis
        command: --port ${REDIS_PORT}
        ports:
          - "${REDIS_PORT}:6379"
        volumes:
          - redis-${ENV}:/data
    
    volumes:
      redis:
        name: redis-${ENV}
        external: false
    

    I get your same output:

    $ docker-compose config
    service "redis" refers to undefined volume redis-env: invalid compose project
    

    I think the problem is that you assumed that you could refer to this volume by the value of its name property. That is just a label, but its identifier is still redis, which cannot be interpolated with environment variables (no key can, only values).

    In other words, I don’t think what you wanted to do can be done, at least not just within the docker-compose.yml file. You can achieve this by creating the volume externally and then using it as an external volume.

    Login or Signup to reply.
  2. These two names need to match:

    services:
      redis:
      volumes:
        - redis-${ENV}:/data
        # ^^^^^^^^^^^^
    
    volumes:
      redis:
    # ^^^^^
    

    Note that Compose is able to construct volume and container names itself. It uses a project name as part of the name, and so the generated names wind up looking a lot like what you’ve specified, so long as you use a docker-compose -p dev option or similar. You can probably remove the manual name settings and the mentions of ${ENV} throughout the file.

    If you also let each Redis instance use its standard port 6379 inside its container (the container network isolation means there won’t be a conflict) and use the Compose-provided default network, you can reduce this to as little as

    version: '3.8'
    services:
      redis:
        image: redis
        ports:
          - "${REDIS_PORT:-6379}:6379"
        volumes:
          - redis:/data
    
    volumes:
      redis:
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search