ygetarts
ygetarts

Reputation: 944

Data not persisting in docker volume

I'm using windows with linux containers. I have a docker-compose file for an api and a ms sql database. I'm trying to use volumes with the database so that my data will persist even if my container is deleted. My docker-compose file looks like this:

version: '3'
services:
  api:
    image: myimage/myimagename:myimagetag
    environment:
      - SQL_CONNECTION=myserverconnection
    ports:
      - 44384:80
    depends_on:
      - mydatabase
  mydatabase:
    image: mcr.microsoft.com/mssql/server:2019-latest
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=mypassword
    volumes:
      - ./data:/data
    ports:
      - 1433:1433
volumes:
  sssvolume:    

everything spins up fine when i do docker-compose up. I enter data into the database and my api is able to access it. The issue I'm having is when I stop everything and try deleting my database container, then do docker-compose up again. The data is no longer there. I've tried creating an external volume first and adding

external: true

to the volumes section, but that hasn't worked. I've also messed around with the path of the volume like instead of ./data:/data I've had

  • sssvolume:/var/lib/docker/volumes/sssvolume/_data

but still the same thing happens. It was my understanding that if you name a volume and then reference it by name in a different container, it will use that volume.

I'm not sure if my config is wrong or if I'm misunderstanding the use case for volumes and they aren't able to do what I want them to do.

Upvotes: 1

Views: 1801

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25120

MSSQL stores data under /var/opt/mssql, so you should change your volume definition in your docker-compose file to

volumes:
  - ./data:/var/opt/mssql

Upvotes: 2

Related Questions