Created
February 19, 2024 19:07
-
-
Save tecnologer/5d6fdacbf8d2ed7dcd2fc20ed62e8c1d to your computer and use it in GitHub Desktop.
create docker database psql|mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Function to display help | |
| function display_help() { | |
| echo "Usage: $0 <db-type> [OPTIONS]" | |
| echo "db-type: Type of the database ('psql' for PostgreSQL, 'mysql' for MySQL)." | |
| echo "Options:" | |
| echo " --container-name Name of the Docker container to be used. Default is 'docker-postgres' for PostgreSQL and 'docker-mysql' for MySQL." | |
| echo " --db-password Password for the database. Default is 'S3cret*_2024'." | |
| echo " --db-user User for the database. Default is 'admin' for mysql and 'postgres' for psql." | |
| echo " --db-name Name of the database to be created. Default is 'master'." | |
| echo " --db-port Port to expose the database. Default: psql-> 5432, mysql: 3306" | |
| echo " -h, --help Display this help message and exit." | |
| } | |
| # Set default values | |
| LOCAL_DB_PASSWORD="S3cret*_2024" | |
| LOCAL_DB="master" | |
| # Check if at least one argument is provided | |
| if [ $# -lt 1 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then | |
| display_help | |
| exit 1 | |
| fi | |
| # The first argument is the database type | |
| DB_TYPE="$1" | |
| shift # Shift the arguments to the left so we can parse the rest | |
| # Set default values based on the DB type | |
| case $DB_TYPE in | |
| psql) | |
| DB_CONTAINER="docker-postgres" | |
| LOCAL_DB_PORT=5432 | |
| LOCAL_DB_USER="postgres" | |
| ;; | |
| mysql) | |
| DB_CONTAINER="docker-mysql" | |
| LOCAL_DB_PORT=3306 | |
| LOCAL_DB_USER="admin" | |
| ;; | |
| *) | |
| echo "Invalid database type: $DB_TYPE" | |
| echo "Supported types are 'psql' for PostgreSQL and 'mysql' for MySQL." | |
| exit 1 | |
| ;; | |
| esac | |
| # Parse named arguments | |
| while [[ "$#" -gt 0 ]]; do | |
| case $1 in | |
| --container-name) DB_CONTAINER="$2"; shift ;; | |
| --db-password) LOCAL_DB_PASSWORD="$2"; shift ;; | |
| --db-user) LOCAL_DB_USER="$2"; shift ;; | |
| --db-name) LOCAL_DB="$2"; shift ;; | |
| --db-port) LOCAL_DB_PORT="$2"; shift ;; | |
| -h|--help) display_help; exit 0 ;; | |
| *) echo "Unknown parameter passed: $1"; exit 1 ;; | |
| esac | |
| shift | |
| done | |
| echo $LOCAL_DB_PORT | |
| # Define ANSI color codes | |
| reset_color=$'\e[0m' | |
| blue=$'\e[34m' | |
| green=$'\e[32m' | |
| yellow=$'\e[93m' | |
| cyan=$'\e[96m' | |
| # Break down the echo statement into parts for color coding | |
| prefix="${blue}::${reset_color}" | |
| cmd="${green}$DB_TYPE${reset_color}" | |
| db_name_arg="${cyan}--db-name${reset_color}" | |
| db_name_value="${yellow}$LOCAL_DB${reset_color}" | |
| db_user_arg="${cyan}--db-user${reset_color}" | |
| db_user_value="${yellow}$LOCAL_DB_USER${reset_color}" | |
| db_password_arg="${cyan}--db-password${reset_color}" | |
| db_password_value="${yellow}${LOCAL_DB_PASSWORD:0:4}*****${reset_color}" | |
| db_port_arg="${cyan}--db-port${reset_color}" | |
| db_port_value="${yellow}$LOCAL_DB_PORT${reset_color}" | |
| echo "creating database..." | |
| echo "${prefix} ${cmd} ${db_name_arg} ${db_name_value} ${db_user_arg} ${db_user_value} ${db_password_arg} ${db_password_value} ${db_port_arg} ${db_port_value}" | |
| # Run the appropriate Docker container based on the database type | |
| if [[ "$DB_TYPE" == "psql" ]]; then | |
| docker run -d -p $LOCAL_DB_PORT:$LOCAL_DB_PORT --rm -it --name $DB_CONTAINER \ | |
| -e POSTGRES_PASSWORD=$LOCAL_DB_PASSWORD \ | |
| -e POSTGRES_USER=$LOCAL_DB_USER \ | |
| -e POSTGRES_DB=$LOCAL_DB postgres:latest | |
| elif [[ "$DB_TYPE" == "mysql" ]]; then | |
| docker run -d -p $LOCAL_DB_PORT:$LOCAL_DB_PORT --rm -it --name $DB_CONTAINER \ | |
| -e MYSQL_ROOT_PASSWORD=$LOCAL_DB_PASSWORD \ | |
| -e MYSQL_PASSWORD=$LOCAL_DB_PASSWORD \ | |
| -e MYSQL_DATABASE=$LOCAL_DB \ | |
| -e MYSQL_USER=$LOCAL_DB_USER \ | |
| -e MYSQL_PASSWORD=$LOCAL_DB_PASSWORD mysql:latest | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment