Skip to main content Miguel Hernández

Database Access With Docker

TL;DR
Quickly connect to PostgreSQL, MariaDB, MongoDB and Redis databases using Docker with simple, one-line commands, bypassing the need for local installations.

Ever needed to quickly connect to a database without installing the client tools directly on your machine? Docker to the rescue! This allows you to jump into a database shell from any environment with Docker installed, saving time and keeping your system clean.

PostgreSQL

shell code snippet start

docker run --rm -it -e PGPASSWORD=$DB_PASS postgres:17 psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME

shell code snippet end

MariaDB

shell code snippet start

docker run --rm -it mariadb:11 mariadb -h $DB_HOST -P $DB_PORT -u $DB_USER -p"$DB_PASS" -D $DB_NAME

shell code snippet end

MongoDB

shell code snippet start

docker run --rm -it mongo:8 mongosh --host $DB_HOST --port $DB_PORT -u $DB_USER -p $DB_PASS

shell code snippet end

Redis

shell code snippet start

docker run --rm -it redis:7.4 redis-cli -h $DB_HOST -p $DB_PORT

shell code snippet end

Caution
Double-check that the Docker image version you select aligns with the compatibility requirements of your targeted database server to prevent any version-related issues.