Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in docker compose with volume undefined

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]
like image 550
serah Avatar asked Oct 21 '25 04:10

serah


2 Answers

You must define volumes at the top level like this:

version: '3.8'

services:
  mysqldb-docker:
    # ...
    volumes:
      - mysqldb:/var/lib/mysql

volumes:
  mysqldb:
like image 66
André Krosby Avatar answered Oct 22 '25 19:10

André Krosby


If you have a mysqldb volume already created, check if exists with docker volume ls

If not, you can create it with docker volume create mysqldb

You can reference it, as follows:

version: '3.8'
services:
  mysqldb-docker:
    volumes:
      - mysqldb:/var/lib/mysql

volumes:
  mysqldb:
    external: true
like image 28
TkrA Avatar answered Oct 22 '25 20:10

TkrA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!