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
Related
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
I hosted a redis database with docker and now I want to know if it is possible to load the database scheme with a config file I will save in my repository?
Thanks for your help
If the image you use has the ability to load external file, then it will be a matter to provide the file to the container when you create container.
The official redis image allows you to give your own config file as well as mount a file storage area.
What you need to do is:
Create folder in your repository ex. redis
Create configuration and data folders under it ex redis/config and redis/data
Create the redis.conf in redis/config to enable the persistence as follows:
SAVE 60 1
dbfilename initial_file.rdb
dir /data
Start container with redis as follows
$ docker run -v <repo folder>/redis/conf:/usr/local/etc/redis -v <repo folder>/redis/data:/data -d --name some-redis redis redis-server /usr/local/etc/redis/redis.conf
Enter in to the container and initialize the database
$ docker exec -ti some-redis /bin/bash
root#4ec2d5dc5082:/data# redis-cli
127.0.0.1:6379> set test 1
OK
127.0.0.1:6379> save
OK
you can initialize the database with file as well, just copy the file to the container and use the cli tools to load the file.
Stop the container and clean
$ docker stop some-redis
$ docker rm some-redis
Add the new files to your repository and commit the changes
After this you will have the database files in your repository. Whenever you want to use them, you can start a container as in step 4
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.
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.
I am using a postgreSQL docker image.
When I inspect the docker image running I have the following configuration:
"Volumes" : {
"var/lib/postgresql": {}
};
I want to run the docker container, make changes to the postgresql data bases and create another version of the docker container with those changes. For that I use the following command:
docker commit –m “Add data” –a “My User Name” \7b827 miguelbgouveia/postegresql:version2
The I run this new container expecting that the added data be present. Like this:
docker run -d --name db –p 5432:5432 miguelbgouveia/postgresql:version2
The problem is all the new data added is not present in the new running container. This is normal? How can I create a new container containing all the data base changes?
Volumes are outside of the Docker layered filesystem. Any changes you make there are persisted in the volume, not in the image.