Skip to content

Starting Database with Docker

Marco Sappé Griot edited this page Nov 16, 2023 · 4 revisions

PostgreSQL

docker run -p 5432:5432 --name pgdemodb --rm -ePOSTGRES_DB=test -ePOSTGRES_USER=test -ePOSTGRES_PASSWORD=test postgres:9.4 \
    -c max-prepared-transactions=110 -c log-statement=all

docker run -d -p 5432:5432 --name pgdemodb --rm -v $(pwd)/init.sql:/docker-entrypoint-initdb.d/init.sql \
    -e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -e POSTGRES_DB=test postgres:11

On after startup of the DB a new database and user following set of commands could be used

doker exec -it pgdemodb bash
# inside of the container
createdb --username test test2
createuser --username test test2
psql test2 test2
# SQL command to setup the password for the created user
##    ALTER USER test2 WITH PASSWORD 'test2';

MySQL

docker run -p 3306:3306 --rm -it --name mymysql -eMYSQL_DATABASE=test -eMYSQL_USER=test -eMYSQL_PASSWORD=test -eMYSQL_LOG_CONSOLE=true \
    docker.io/mysql/mysql-server:5.7 --log-error --general_log=1 --general_log_file=/tmp/mysql.query.log

Some notes for examining MySQL

docker exec -it $(docker ps | grep mymysql | awk '{print $1}') bash -c 'tail -F /tmp/mysql.query.log'
docker stop $(docker ps | grep mymysql | awk '{print $1}') # CTRL + C somehow does not work for the container of mysql

MariaDB

" cat > /tmp/mariadb.sql <<EOF
CREATE DATABASE test;
USE 'test';
CREATE USER 'test' IDENTIFIED BY 'test';
GRANT USAGE ON *.* TO 'test'@localhost IDENTIFIED BY 'test';
GRANT USAGE ON *.* TO 'test'@'%' IDENTIFIED BY 'test';
GRANT ALL privileges ON test.* TO 'test'@localhost;
FLUSH PRIVILEGES;
CREATE TABLE test ( id smallint unsigned not null auto_increment, name varchar(20) not null, constraint pk_example primary key (id) );
" EOF
docker run -t --rm --name maria -p 3306:3306 -v /tmp/mariadb.sql:/docker-entrypoint-initdb.d/test.sql -eMARIADB_ROOT_PASSWORD=mypassword \
    mariadb/server:10.3
# docker kill maria