BenBtg
BenBtg

Reputation: 836

Can Docker Containers maintain state between restarts?

Should containers be able to maintain state? I am using a SQLServer Image like so.

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d microsoft/mssql-server-linux:2017-latest

Then I create a database in it using dotnet ef.

dotnet ef database update -v

Database works fine until I restart the container. At which point my database is gona and the container is reset to it's initial state.

What am I missing? Do containers not persist state? If so what's the point in using them for databases?

Upvotes: 4

Views: 2893

Answers (1)

Rafal
Rafal

Reputation: 12619

Yes they can if you don not delete the container so you can

docker stop xxx

or just simply restart your machine and than use

docker start xxx

or

docker restart xxx

if you use docker run you create a new container so there is no previous state to talk about. For sql server specifically there is an option to create a volume and store data there. If you do that you can delete a container and recreate it again without loosing data as its is no longer stored inside it.

Upvotes: 5

Related Questions