Skip to main content

Simple Containers

1. MariaDB

This container may be used by applications that need a MySQL/MariaDB database.

1.1 The scripts

The scripts are located at: https://gitlab.com/docker-scripts/mariadb

We can get them with ds pull:

ds pull mariadb

ls /opt/docker-scripts/mariadb/
cd /opt/docker-scripts/mariadb/
ls
tree

nano Dockerfile
nano settings.sh
nano cmd/create.sh
nano cmd/config.sh
nano inject/mariadb.sh

nano cmd/clone.sh

1.2 Make

ds init mariadb @db1
cd /var/ds/db1/
nano settings.sh
ds make
ls
ls data/

ds shell
systemctl status mariadb
mariadb <<< 'show databases;'

ls -alh /var/lib/mysql
ls
ls -alh data/
exit

echo 'show databases;' | ds exec mariadb
ds exec mariadb <<< 'show databases;'

1.3 Usage

This container is normally used by other containers/applications that are installed in the same docker virtual LAN. They can access it by the name of the container, because Docker provides an internal DNS service that resolves the name of the container to the IP of the container.

To facilitate the management of the database by the other containers, the global command ds mariadb is installed. It is called global because it can be called in the context (directory) of any other container (not in the directory of the mariadb container, as usual). It can be thought of as an extension of the basic functionality of the docker-script framework. It is not installed by default when DS is installed because it makes sense and is useful only when a mariadb container is installed.

It is installed by placing the script cmd/mariadb.sh in the directory /root/.ds/cmd/ (where /root/.ds/ is the default value of DSDIR). This is done by the script cmd/create.sh.

ds | head -20
ls /root/.ds/
ls /root/.ds/cmd/

ls /opt/docker-scripts/mariadb/cmd/
nano /opt/docker-scripts/mariadb/cmd/create.sh
nano /opt/docker-scripts/mariadb/cmd/mariadb.sh

This global command assumes (requires) that the container that calls it has defined (on settings.sh) the variables: DBHOST, DBNAME, DBUSER, DBPASS. The variable DBHOST keeps the name of the MariaDB container.

From its usage we can see that it can be used to create, dump and drop a database, as well as to run SQL commands and scripts on it.

1.4 Test

Let's use it from a container.

  1. Let's create a test container that needs a DB:

    ds pull debian
    ds init debian @test1
    cd /var/ds/test1/
    sed -i settings.sh -e '/PORTS/d'
    nano settings.sh
    ds make

    We removed PORTS because we don’t need to access it from the internet.

  2. To create and manage a database for this container we can use the command ds mariadb:

    ds mariadb

    It complains: Error: No DBHOST defined on 'settings.sh'

    Actually we also need to define DBNAME, DBUSER and DBPASS, beside DBHOST. Let’s do it:

    cat <<EOF >> settings.sh
    DBHOST='db1'
    DBNAME='test1db'
    DBUSER='test1user'
    DBPASS='test1pass'
    EOF
    nano settings.sh

    Here, db1 is the name of the mariadb container that we built previously. The rest of the variables can be anything.

  3. Let's try it again:

    ds mariadb
    ds mariadb create
    ds mariadb sql "show databases;"
    ds mariadb drop
    ds mariadb sql "show databases;"
    ds mariadb create
  4. To access the database from inside the container we need to install a MariaDB client first (iside the container):

    ds shell

    apt install mariadb-client
    source settings.sh
    echo $DBHOST
    echo $DBNAME
    mariadb \
    --host=$DBHOST \
    --user=$DBUSER \
    --password=$DBPASS \
    <<< "show databases;"

2. PostgreSQL

This container may be used by applications that need a PostgreSQL database.

2.1 The scripts

The scripts are located at: https://gitlab.com/docker-scripts/postgresql

We can get them with ds pull:

ds pull postgresql

ls /opt/docker-scripts/postgresql/
cd /opt/docker-scripts/postgresql/
ls
tree

nano Dockerfile
nano settings.sh
nano cmd/build.sh
nano cmd/create.sh
nano cmd/config.sh
nano inject/postgresql.sh

nano cmd/pg.sh
nano cmd/postgresql.sh

2.2 Make

ds init postgresql @db2
cd /var/ds/db2/
nano settings.sh
ds make
ls
ls var/
ls var/16/main/
ls etc/
ls etc/16/main/
ls dblogs/
tail dblogs/postgresql-16-main.log

ds shell
su -l postgres
pwd
ls
ls -al 16/main/
psql
\x
\l
\q
exit
exit

ds pg
ds pg psql -x
\list
\q

ds pg psql -x -c '\list'
ds pg psql -x <<< '\list'
echo '\list' | ds pg psql -x

2.3 Usage

This container is normally used by other containers/applications that are installed in the same docker virtual LAN. They can access it by the name of the container, because Docker provides an internal DNS service that resolves the name of the container to the IP of the container.

To facilitate the management of the database by the other containers, the global command ds postgresql is installed. It is called global because it can be called in the context (directory) of any other container (not in the directory of the postgresql container, as usual). It can be thought of as an extension of the basic functionality of the docker-scripts framework. It is not installed by default when DS is installed because it makes sense and is useful only when a postgresql container is installed.

It is installed by placing the script postgresql.sh in the directory /root/.ds/cmd/ (where /root/.ds/ is the default value of DSDIR). This is done by the script cmd/create.sh.

ls /opt/docker-scripts/postgresql/cmd/
nano /opt/docker-scripts/postgresql/cmd/create.sh

ds | head -20
ls /root/.ds/cmd/
nano /root/.ds/cmd/postgresql.sh

This global command assumes (requires) that the container that calls it has defined (on settings.sh) the variables: DB_HOST, DB_NAME, DB_USER, DB_PASS. The variable DB_HOST keeps the name of the PostgreSQL container.

From its usage we can see that it can be used to create, dump and drop a database, as well as to run SQL commands and scripts on it.

2.4 Test

Let's use it from a container.

  1. Let's create a test container that needs a DB:

    ds pull debian
    ds init debian @test2
    cd /var/ds/test2/
    sed -i settings.sh -e '/PORTS/d'
    nano settings.sh
    ds make

    We removed PORTS because we don’t need to access it from the internet.

  2. To create and manage a database for this container we can use the command ds postgresql:

    ds postgresql

    It complains: Error: No DB_HOST defined on 'settings.sh'

    Actually we also need to define DB_NAME, DB_USER and DB_PASS, beside DB_HOST. Let’s do it:

    cat <<EOF >> settings.sh
    DB_HOST='db2'
    DB_NAME='test2db'
    DB_USER='test2user'
    DB_PASS='test2pass'
    EOF
    nano settings.sh

    Here, db2 is the name of the postgresql container that we built previously. The rest of the variables can be anything.

  3. Let's try it again:

    ds postgresql
    ds postgresql create
    ds postgresql sql "SELECT datname FROM pg_database;"
    ds @db2 pg psql -x -c '\l'
    ds postgresql drop
    ds @db2 pg psql -x -c '\l'
    ds postgresql create
  4. To access the database from inside the container we need to install a PostgreSQL client first (inside the container):

    ds shell

    apt install postgresql-client
    which psql

    psql -h db2 -d test2db -U test2user
    \x
    exit

    PGPASSWORD=test2pass psql -h db2 -d test2db -U test2user -x -c '\l'

    source settings.sh
    echo $DB_HOST
    echo $DB_NAME
    PGPASSWORD=$DB_PASS \
    psql -x -h $DB_HOST -d $DB_NAME -U $DB_USER <<< '\list'
    PGPASSWORD=$DB_PASS \
    psql -x -h $DB_HOST -d $DB_NAME -U $DB_USER <<< '\list' \
    | grep Name

    exit