docker: create a mongodb volume that is still saved after docker-compose down? - mongodb

I have the following docker-compose.yml file:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
My issue is that when I run docker-compose down all the data within my mongo database is lost.
How can I create a mongo volume that persists even when the mongo container goes down?

Per the image documentation the database volume needs to be /data/db. This is also seen in the Dockerfile volume.
Since the volume is defined in the Dockerfile, if you do not create a volume at that directory, even if you created a volume in the parent like /data, docker will create an anonymous volume at /data/db which will show up as a long guid volume name in docker volume ls. Depending on how the container is run, those may be left behind.
Therefore the fix is to adjust your volume mount to that path:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
Note that unless you need direct access to this data on the host, I'd recommend using a named volume instead. It includes initialization steps that helps with permission issues you may encounter with host volumes, particularly when running directly on a Linux host.
To use a named volume, that would look like:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- dbdata:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
volumes:
dbdata:

Try to change volumes for MongoDb container:
volumes:
- "./database:/data/db"

Related

Mongo image does not created with credentials

I am creating mongo:latest container on my machine using docker-compose.
This is my docker-compose.yaml:
version: '3.6'
services:
backend:
image: backend:1.0.0
container_name: backend
ports:
- '5000:5000'
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
JWT_TOKEN: Auto
scheduler:
image: scheduler:1.0.0
container_name: scheduler
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
mongodb:
image: mongo
container_name: mongodb
expose:
- 27017
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootPassXXX
volumes:
- ./conf/mongodb:/data/db
volumes:
autoc:
external: true
networks:
default:
external:
name: automatic
After creating those containers I am receiving from them an error: MongoServerError: Authentication failed.
This is the URI that the application is trying to connect: mongodb://root:rootPassXXX#mongodb:27017/matiautomai.
While trying to investigate what was happening, I was trying to log in to the same mongo container
that initializes with the user name and password variables without the credentials using that way: mongodb://mongodb:27017/automatic, And the application manages to connect to it.
I've reviewed the docs inside the Docker hub and it seems to be that I used the environment variables in the correct way.
What am I missing here?
After seeing your helpful comments, I found the solution for this issue.
I needed to initialize a one-time docker-compose with an initial script that located inside.
docker-compose.yaml:
version: '3.7'
services:
mongodb:
container_name: mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: root-db
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
This is the script that located inside ./docker-entrypoint-initdb.d/mongo-init.js:
print('start ~~~~');
db = db.getSiblingDB('yourDBname');
db.createUser(
{
user: 'username',
pwd: 'password',
roles: [{ role: 'readWrite', db: 'yourDBname' }],
},
);
db.createCollection('users');
print('end ~~~~');
After that, you will be able to connect to mongo with the credential you have set in the .js file.

I don't manage to configure volumes in docker-compose to have a folder reachable by dockerized pgAdmin, to restore a database

I have setup from a tutorial a pgadmin4 container and a PostgreSQL container with the following docker-compose:
version: "2"
services:
db:
image: postgres:14
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/opt/docker/pgadmin4/postgresql/data
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#linuxhint.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
links:
- "db:pgsql-server"
volumes:
db-data:
pgadmin-data:
In both conainers, I have mounted the path /home/laurent/Documents/Informatique/Docker/sample-database where I have put a sample database.
After having created the dvdrental database as explained in the tutorial, I try to import it with restore, but I cannot find my /opt/dvdrental.tar as expected.
When I Select file, I get this:
If I add opt in the path and refresh, nothing more.
But if I open a console in both containers, I have /opt/dvdrental.tar.
What do I miss?

How to attach persistent volume in docker-compose file for mongodb?

I have a docker-compose file that will bring up mongo and mongo-express containers in the same network "mynet".
I have created a network by:
docker network create mynet
I have created a volume named "demo-vol" externally by docker command.
docker volume create demo-vol
Inside the container, I have created a sample mongo collection.
When I do docker-compose up I'm able to see the container running but I'm not able to find the mongo data in that specified volume.
Below is my docker-compose.yaml file
version: '3'
services:
mongo:
image: mongo
container_name: mymongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- "/demo-vol:/data/db"
networks:
- mynet
ports:
- 27017:27017
mongoexpress:
image: mongo-express
container_name: mymongoexpress
ports:
- 8081:8081
volumes:
- "/demo-vol:/data/db"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
networks:
- mynet
volumes:
demo-vol:
external: true
networks:
mynet:
external: true
What I need is:
Even after deleting the container, I want my data to be persistent.
How to do that and please explain. Where i'm going wrong?
Note:I'm a beginner to Docker concepts.
Thanks in advance.
You can to use local driver for volume
volumes:
demo-vol:
driver: local
and try to remove slash
volumes:
- demo-vol:/data/db

Saving mongo data in Windows host

I am trying to save mongodb data in Windows host, but getting an error, I want this to be happen through Docker-compose only
My Docker-compose file
version: '3'
services:
mongo:
image: mongo
restart: always
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME:
MONGO_INITDB_ROOT_PASSWORD:
volumes:
- C:\Users\sd\Desktop\Data:/data/db
mongo-express:
image: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME:
ME_CONFIG_MONGODB_ADMINPASSWORD:
volumes:
C:\Users\sd\Desktop\Data:
Getting this error
Volume value 'C:\Users\sd\Desktop\Data' doesn't match with any of the regexes: '[a-zA-Z0-9._-]+$'
I tried with
volumes:
- /C/Users/sd/Desktop/Data:/data/db
and
volumes:
- //C/Users/sd/Desktop/Data:/data/db
but still the same issue
This syntax should work (notice the minus c ) :
volumes: - //c/Users/sd/Desktop/Data:/data/db
In addition, you should add the COMPOSE_CONVERT_WINDOWS_PATHS variable :
As described here

How to use Robo3T to connect mongodb server running as a docker container

I have a mongodb running as a docker container which created by docker-compose.
Here is my docker-compose.yml:
verion:'3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
I want to connect mongodb from outside using Robo 3T,But It always tell me that 'Failed to load database'.Here is my Robo 3T config:
Please Help me It's emergency!
Are your sure it started successfull? Your Docker Compose has a typo in "version:3.7".
correct version:
version: '3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
Please check after starting if the container is running with
docker ps