File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,3 +6,6 @@ COPY init.js /docker-entrypoint-initdb.d/
66
77# Exponer el puerto de MongoDB
88EXPOSE 27017
9+
10+ # Exponemos el puerto
11+ EXPOSE 27017
Original file line number Diff line number Diff line change 1- db = db . getSiblingDB ( ' admin' ) ;
1+ const db_admin = db . getSiblingDB ( " admin" ) ;
22
3- db . createUser ( {
4- user : 'admin' ,
5- pwd : 'admin' ,
6- roles : [
7- { role : 'root' , db : 'admin' }
8- ]
9- } ) ;
3+ const username = process . env . MONGO_INITDB_ROOT_USERNAME || "admin" ;
4+ const password = process . env . MONGO_INITDB_ROOT_PASSWORD || "admin" ;
5+ const database = process . env . MONGO_INITDB_DATABASE || "smartpot" ;
106
11- db = db . getSiblingDB ( 'smartpot' ) ;
7+ const userExists = db_admin . getUser ( username ) ;
8+
9+ if ( ! userExists ) {
10+ print ( `Creando usuario ${ username } ...` ) ;
11+ db_admin . createUser ( {
12+ user : username ,
13+ pwd : password ,
14+ roles : [ { role : "root" , db : "admin" } ]
15+ } ) ;
16+ } else {
17+ print ( `Usuario ${ username } ya existe, actualizando contraseña...` ) ;
18+ db_admin . updateUser ( username , { pwd : password } ) ;
19+ }
20+
21+ db = db . getSiblingDB ( database ) ;
1222
1323db . createCollection ( 'usuarios' ) ;
1424db . usuarios . insertMany ( [
You can’t perform that action at this time.
0 commit comments