As you know installing databases in our computers is taking many time and they are also complicated. So In this topic I will instroduce ways to create database by using Docker which will help you save more time.
We will use docker-compose to create database containers
Let's create a folder with anyname you like as mysql docker and in this folder we will create a folder name mysql-data and a file docker-compose.yml and put scripts as belows:
docker-compose.yml
1 2 3 4 5 6 7 8 91011121314151617181920212223
version:'3.7'services:mysql_db_container:# using latest image mysql from dockerHubimage:mysql:latest# container name: check by command: docker container lscontainer_name:mysql_containercommand:--default-authentication-plugin=mysql_native_password# default username is rootenvironment:MYSQL_ROOT_PASSWORD:passwordMYSQL_DATABASE:sys# map port between real machine and container ports:-3306:3306# map data between real machine and container# which will help us avoid lost data when container is downvolumes:-./mysql-data:/var/lib/mysql:rwvolumes:mysql-data:
Save the docker-compose.yml and open terminal and use commands sudo docker compose up -d to start the container and sudo docker compose down to stop the container.
After your mysql DB has started you can use these conection information to connect to your DB
We don't have many oracle database image on DockerHub so you can use my oracle image on my dockerhub by command docker pull minhducnguyen189/oracle-xe-18-4:latest. Or you will create an docker image for yourself to use.
Let's copy your oracle database installation file into the source repo of step 1 following this path docker-images\OracleDatabase\SingleInstance\dockerfiles\<your oracle database xe version>\
Now let go to this path of source repo docker-images\OracleDatabase\SingleInstance\dockerfiles. Then open the Git Bash and execute the build script with command below
Let's create a folder with anyname you like as oracle docker and in this folder we will create 2 folders name oradata and setup. Then we create a file docker-compose.yml and put scripts as belows:
docker-compose.yml
1 2 3 4 5 6 7 8 910111213141516171819202122
version:'3.7'services:database:# oracle database image from DockerHubimage:oracle-xe# container namecontainer_name:oracle_databaseenvironment:# Oracle password for SYS, SYSTEM and PDBADMIN accountsORACLE_PWD:passwordvolumes:# mapping data of container to real machine-./oradata:/opt/oracle/oradata# mapping db scripts to container if we have-./startup:/opt/oracle/scripts/startupports:-1521:1521volumes:oradata:startup:
Then use command sudo docker compose up to start your container.
You should note the the first time starting DB will make many time. After your orcal DB has started, you can use these conection information to connect to your DB
Let's create a folder with anyname you like as postgres docker and in this folder we will create a folder name data and a file docker-compose.yml and put scripts as belows:
Let's create a folder with anyname you like as mongo docker and in this folder we will create a folder name data and a file docker-compose.yml and put scripts as belows:
version:'3.7'services:mongo:# use mongo image from dockerhubimage:mongo# restart if service is failedrestart:alwayscontainer_name:mongodbenvironment:MONGO_INITDB_ROOT_USERNAME:rootMONGO_INITDB_ROOT_PASSWORD:exampleports:-27017:27017# sync data from container to real machinevolumes:-./data:/data/dbmongo-express:# use image from dockerhubimage:mongo-expresscontainer_name:mongo-expressenvironment:ME_CONFIG_MONGODB_ADMINUSERNAME:rootME_CONFIG_MONGODB_ADMINPASSWORD:exampleME_CONFIG_MONGODB_SERVER:mongodb# restart if service is failedrestart:alwaysdepends_on:-mongoports:-8081:8081volumes:data:
After your mongodb has started you can use these connection information to connect to your DB
Field Name
Value
USERNAME
root
PASSWORD
defined in docker-compose file: example
PORT
defined docker-compose file, default 27017
HOST
localhost
Note that: every time you start you container, it will create an anonymous volume. So you should delete it after you shut down your container by command docker volume rm <volume name> -f.
Let's create a folder with any name you like as mariadb and in this folder we will create a folder name mariadb and a file docker-compose.yml and put scripts as below:
After your mariadb has started you can use these connection information to connect to your DB.
If you use root user, you will have permission to access all database and create any database that you want. The root user credentials are showed as below.
Field Name
Value
USERNAME
root
PASSWORD
defined in docker-compose file: rootpass
PORT
defined docker-compose file, default 3306
HOST
localhost
If you use non root user that defined in the docker-compose.yaml then you can only access to the database that you defined mydb.
Field Name
Value
USERNAME
user
PASSWORD
defined in docker-compose file: password
PORT
defined docker-compose file, default 3306
HOST
localhost
DATABASE
mydb
- If you want to grant all permission for the non root user as the root user, you can access the database with root user and run SQL scripts below to grant permissions.
GRANT ALL PRIVILEGES: This command grants all possible privileges to a specific database user. Privileges include the ability to perform operations like SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, and more.
ON .: The *.* syntax specifies that the privileges apply to all databases (*) and all tables (*) within those databases. This means the user will have full access to every database and table on the server.
TO 'user'@'%': This specifies the user and host to which the privileges are granted.
'user' is the username of the account being granted privileges.
'%'is a wildcard for the host part, meaning the user can connect from any IP address or hostname.
IDENTIFIED BY 'password': This clause sets the password for the specified user. If the user already exists, their password will be updated to the provided value.
FLUSH PRIVILEGES;: This command tells the database server to reload the grant tables in memory. This is necessary because MySQL or MariaDB caches user privileges, and changes made by the GRANT command won't take effect until the cache is refreshed. FLUSH PRIVILEGES ensures that the new privileges are applied immediately.
SHOW GRANTS FOR 'user'@'%';:This command displays the privileges that have been granted to the specified user. It shows the exact privileges that the user 'user' has when connecting from any host ('%'). This is a way to verify that the GRANT command worked as intended.