skip to Main Content

I get the below error when I run docker-compose up, any pointers why I am getting this error

service "mysqldb-docker" refers to undefined volume mysqldb: invalid compose project

Also, is there a way to pass the $ENV value in CLI to docker-compose up , currently I have a ENV variable that specified dev, uat or prod that I use to specify the db name. Are there better alternatives to do this other than create a .env file explicitly for this

 version: '3.8'
    services:
      mysqldb-docker:
        image: '8.0.27'
        restart: 'unless-stopped'
        ports:
          - "3309:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_PASSWORD=root
          - MYSQL_DATABASE=reco-tracker-$ENV
        volumes:
          - mysqldb:/var/lib/mysql
      reco-tracker-docker:
        image: 'reco-tracker-docker:v1'
        ports:
          - "8083:8083"
        environment:
          - SPRING_DATASOURCE_USERNAME=root
          - SPRING_DATASOURCE_PASSWORD=root
          - SPRING_DATASOURCE_URL="jdbc:mysql://mysqldb-docker:3309/reco-tracker-$ENV"
        depends_on: [mysqldb-docker]

2

Answers


  1. You must define volumes at the top level like this:

    version: '3.8'
    
    services:
      mysqldb-docker:
        # ...
        volumes:
          - mysqldb:/var/lib/mysql
    
    volumes:
      mysqldb:
    
    Login or Signup to reply.
  2. You can pass environment variables from your shell straight through to a service’s containers with the ‘environment’ key by not giving them a value

    https://docs.docker.com/compose/environment-variables/#pass-environment-variables-to-containers

    web:
      environment:
        - ENV
    

    but from my tests you cant write $ENV in the compose file and expect it to read your env

    for this you need to call docker-compose that way :

    docker-compose run -e ENV web python console.py
    

    see this : https://docs.docker.com/compose/environment-variables/#set-environment-variables-with-docker-compose-run

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search