Can Docker Containers maintain state between restarts? - sql-server

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?

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.

Related

Commited SQL Server image does not work when using a mount

My committed image of SQL Server does not seem to work when using a mount. When not mounted, the image contains the changes I made but when when I mount the data directory I don't see the committed changes.
Step to reproduce:
Run a container with SQL server 2019.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P#ssword123" -p 14350:1433 -d mcr.microsoft.com/mssql/server:2019-latest
Connect to this SQL Server and create a database named TestDB.
Commit the container, tagged as testDB, and delete the container. (My contianer ID is cabc3ac26dd0)
docker commit cabc3ac26dd0 testdb
docker container stop cabc3ac26dd0
docker rm cabc3ac26dd0
Run a container with this new image.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P#ssword123" -p 14350:1433 -d testdb
Connect to this SQL sServer container and you will see the new TestDB database. This is your new database, saved as part of the commit. This shows that the commit worked.
Remove the container, and run a new container for testdb with the data drive mounted to your local filesystem.
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=P#ssword123" -p 14350:1433 --mount type=bind,source=C:\Users\me\testdbfiles\mssql\data,target=/var/opt/mssql/data -d testdb
Connect to this sql server conatiner and you will not see the new TestDB.
It seems that the commit has worked when I run without the mount. When I run with the mount, I don't see the changes for my committed image.
This docker run command is the same one I have used for months.
This problem started about a week ago, near when I upgraded to docker
desktop 4.12.0.
The mounted location (C:\Users\me\testdbfiles\mssql\data) has the
sql server database files (such as master.mdf), therefore the mount
the syntax must be correct.
Running a select from sys.databases shows the new TestDB when I am not mounted. When mounted, this select doesn't show this database. This indicates that the master database itself is different when mounted.
Listing the contents of the /var/opt/mssql/data shows the TestDB file when not mounting, but does not show them when mounting.
Has anyone else experienced this problem?
How can I troubleshoot what happens during the mounting phase?
Update: I also added a new sql server login to the image. When I mount I don't see the login, with no bind mount the login is there.

Mongodb running on Docker is wiping the collection after restart

I have to build a small application that reads data from MongoDB running on docker and uses it for further processes.
The problem is that after I close docker, the local instance of the database is also getting deleted. How can I stop it?
The MONGODB_URI is mongodb://localhost:27017 and what are the attributes that I should add in the docker command to avoid it. should I avoid using localhost? docker-compose seems confusing to me so I use Dockerfile.
So, what exactly can be the docker run command to avoid it? is it one of these?
Commands: docker run -d --name mongo-on-docker -p 27017:27017 mongo
docker run -d --name sample --link mongo-on-docker web app
Also to permanently save what data directory should I use?
Docker container are dead before quiting. For store data you should mount named volume, folder or file to the container.
In MongoDB case try:
docker run --rm -ti -v mongo_data:/data/db mongo bash
Where mongo_data is a special Docker entity, that can be mounted as a folder into container. Including in different containers at the same time.
Not new:
How to set docker mongo data volume

Docker - SQL service wont run when cloned

I'm trying to add a volume to a docker container but when I commit it and run with the new volume none of the sql services run on this copy?? Why would that be.
https://learn.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-ver15&pivots=cs1-powershell
I am adding the initial one as above and it works.All fine. Services running. I can connect to it, run SQL but I need to share a drive.
Seems I cant add one directly to an existing instance??
docker commit 5a8f89adeead newimagename
docker run -ti -v "C:/dir1":/dir1 newimagename /bin/bash
I do the above to clone it and add a volume. WORKS. But the sql services just arent running on this new one. Ill accept it either way I just want SQL running and a share in there.
Can anyone help.
Manged it:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Pa55word1" `
-v C:/db:/dir1 `
-p 1433:1433 --name sql3 `
-d mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04
Had an issue with having no drive or no services but this has done it.

Connect to created database with created user in a docker container

i’m trying to connect to a postgre database which is inside a docker container. I´m creating the docker container with following command:
docker run --name prodcoc -p 5432:5432 -e POSTGRES_USER=testuser -e POSTGRES_PASSWORD=test -e POSTGRES_DB=testDB postgres
After the container is running I want to connect to the created database with the created user, but that seems not possible. Why can't I connect to the database which I'm creating with the enviroment variables?
The solution for my problem was to uninstall the local PostgreSql instance. I don´t know why I installed it but removing it solved my problem.
In windows this can be done in the Settings.

Persist database of docker container

I am using postgres docker image in my project. For initialization I am using following command to create and init my database (tables, views, data, ...)
COPY sql_dump.sql /docker-entrypoint-initdb.d/
Is possible persist these data after container is stopped and removed? For instance when I run image of postgres, it will create database with these data wihout loading script every time of container start. Just load created data of first run.
I did some research and I found VOLUME command, but I don't know how to use it for my purpose, I am new with Docker. Thanks for any help. I am using Docker For Win v18.
You can use docker named volumes more information can be found here.
this will create a named volume called postgres-data
docker volume create postgres-data
and say this is your command to create the container.
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
change that to this.
docker run --name some-postgres -v postgres-data:/var/lib/postgresql -e POSTGRES_PASSWORD=mysecretpassword -d postgres
this should mount the postgres-data volume under /var/lib/postgresql. can they initialize your DB and when you stop and start the container it will contain the persisted data.
-HTH

Resources