Docker with Mariadb

Hi all,
I’m a docker newbie, this is my environment : CentOS Linux release 7.4.1708 (Core) + Docker version 18.03.1-ce, build 9ee9f40 .
This is my Dockerfile for Mariadb image container

From centos:7

#Install epel release
RUN yum -y install --setopt=tsflags=nodocs epel-release && \
    yum install -y --setopt=tsflags=nodocs mariadb-server && \
    yum clean all

USER 27

EXPOSE 3306
#start mariadb
CMD ["mysqld_safe"]

I build image and run container with this image successfully, it return container hash id but there is no process listen at port 3306 and , no container exist .

docker build -t mariadb .
...
Successfully tagged mariadb:latest
docker run -d -p 3306:3306 mariadb
8c2caa367c8726be37d8714dc99c15e635f76e1cc6d3f1c15d24e348fbe020d6
docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

I did something wrong with CMD ?
I want to keep my image as simple as possible so I want it just install mariadb and run process as default, I leave the “configuration” part later for docker-compose.yml
Please give me some advice , thank you very much.

I find a guide at this link https://github.com/CentOS/CentOS-Dockerfiles/tree/master/mariadb/centos7
It help me build Mariadb image and run in a container ok.

Dockerfile
FROM centos:centos7

RUN yum -y install --setopt=tsflags=nodocs epel-release && \
    yum -y install --setopt=tsflags=nodocs mariadb-server && \
    yum clean all


# Fix permissions to allow for running on openshift
COPY fix-permissions.sh ./
RUN ./fix-permissions.sh /var/lib/mysql/   && \
    ./fix-permissions.sh /var/log/mariadb/ && \
    ./fix-permissions.sh /var/run/

COPY docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]

# By default will run as random user on openshift and the mysql user (27)
# everywhere else
USER 27

EXPOSE 3306
CMD ["mysqld_safe"]

fix-permissions.sh
#!/bin/sh
# Taken from https://raw.githubusercontent.com/openshift/sti-base/master/bin/fix-permissions
# Fix permissions on the given directory to allow group read/write of
# regular files and execute of directories.
chgrp -R 0 $1
chmod -R g+rw $1
find $1 -type d -exec chmod g+x {} +

docker-entrypoint.sh
#!/bin/bash
set -e

if [ "${1:0:1}" = '-' ]; then
        set -- mysqld_safe "$@"
fi

if [ "$1" = 'mysqld_safe' ]; then
        DATADIR="/var/lib/mysql"

        if [ ! -d "$DATADIR/mysql" ]; then
                if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" ]; then
                        echo >&2 'error: database is uninitialized and MYSQL_ROOT_PASSWORD not set'
                        echo >&2 '  Did you forget to add -e MYSQL_ROOT_PASSWORD=... ?'
                        exit 1
                fi

                echo 'Running mysql_install_db ...'
                mysql_install_db --datadir="$DATADIR"
                echo 'Finished mysql_install_db'

                # These statements _must_ be on individual lines, and _must_ end with
                # semicolons (no line breaks or comments are permitted).
                # TODO proper SQL escaping on ALL the things D:

                tempSqlFile='/tmp/mysql-first-time.sql'
                cat > "$tempSqlFile" <<-EOSQL
                        DELETE FROM mysql.user ;
                        CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
                        GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
                        DROP DATABASE IF EXISTS test ;
                EOSQL

                if [ "$MYSQL_DATABASE" ]; then
                        echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" >> "$tempSqlFile"
                        if [ "$MYSQL_CHARSET" ]; then
                                echo "ALTER DATABASE \`$MYSQL_DATABASE\` CHARACTER SET \`$MYSQL_CHARSET\` ;" >> "$tempSqlFile"
                        fi

                        if [ "$MYSQL_COLLATION" ]; then
                                echo "ALTER DATABASE \`$MYSQL_DATABASE\` COLLATE \`$MYSQL_COLLATION\` ;" >> "$tempSqlFile"
                        fi
                fi

                if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
                        echo "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD' ;" >> "$tempSqlFile"

                        if [ "$MYSQL_DATABASE" ]; then
                                echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;" >> "$tempSqlFile"
                        fi
                fi

                echo 'FLUSH PRIVILEGES ;' >> "$tempSqlFile"

                set -- "$@" --init-file="$tempSqlFile"
        fi

fi

exec "$@"

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=****** mariadb
52f71e631a2175bf50d64ea287e51d6fba315e007d6505a46939f5f5e4c7ec67

But with this way I have to specify mysql root password every time I run container or put it into docker-compose.yml for service which I don’t want.

Hi all,
I need your help.
I try to create stack with 3 services : nginx, php-fpm, mariadb and make them running together , however, nginx and php cannot connect to mariadb container. Here my configuration

docker-compose.yml
version: "3.3"
services:
  php-fpm70:
    image: php-fpm70
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    configs:
# map www.conf from host to php-fpm container to change port from 9000 to 9003
      - source: www.conf.1
        target: /etc/opt/rh/rh-php70/php-fpm.d/www.conf
    volumes:
      - /opt/www:/opt/www
      - /var/lib/mysql:/var/lib/mysql
    ports:
      - "9003:9003"
    networks:
      - webnet
  web:
    image: nginx
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    volumes:
# websites source code
      - /opt/www:/opt/www
# nginx virtual host config
      - /opt/nginx/conf.d:/etc/nginx/conf.d
      - /var/log/nginx:/var/log/nginx
      - /var/lib/mysql:/var/lib/mysql
    ports:
      - "80:80"
      - "443:443"
    networks:
      - webnet
  mariadb:
    image: mariadb
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
    environment:
      MYSQL_ROOT_PASSWORD: ***
    configs:
      - source: my.cnf
        target: /etc/my.cnf
    volumes:
# mariadb datadir
      - /var/lib/mysql:/var/lib/mysql
      - /opt/mariadb/my.cnf.d:/etc/my.cnf.d
    ports:
      - "3306:3306"
    networks:
      - webnet

configs:
  www.conf.1:
    file: /opt/php-fpm70/www.conf
  my.cnf:
    file: /opt/mariadb/my.cnf

networks:
   webnet:
/opt/nginx/conf.d/test.conf
server {
        listen 80;
        server_name test.mydomain.com;
    access_log /var/log/nginx/test/access.log;
    error_log /var/log/nginx/test/error.log;
    root /opt/www/test/;
    gzip  on;
gzip_comp_level 9;
gzip_min_length 1000;
gzip_proxied off;
gzip_types text/plain text/css application/xml+html application/javascript image/jpeg image/x-icon image/gif image/png video/jpeg;
gzip_disable "MSIE [1-6]\.";

index index.html index.htm index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-fpm70:9003;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
docker stack deploy -c docker-compose.yml getstartedlab
docker service ls
ID                  NAME                      MODE                REPLICAS            IMAGE               PORTS
ugibxe1c4o76        getstartedlab_mariadb     replicated          1/1                 mariadb:latest      *:3306->3306/tcp
p489hfezvwlw        getstartedlab_php-fpm70   replicated          1/1                 php-fpm70:latest    *:9003->9003/tcp
du2fpdpev154        getstartedlab_web         replicated          1/1                 nginx:latest        *:80->80/tcp, *:443->443/tcp

I can access to test.mydomain.com --> “Welcome to nginx” , test.mydomain.com/index.php --> show phpinfo. But I cannot access to test.mydomain.com/abc.php --> “Warning: mysqli::__construct(): (HY000/2002): Connection refused in /opt/www/test/abc.php on line 7
Connection failed: Connection refused” which actually connect to mariadb

/opt/www/test/abc.php
 <?php
$servername = "localhost";
$username = "root";
$password = "***";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

If my stack only include nginx and php-fpm and mariadb starting on host , it works fine.
I can “docker exec -it mariadb-container bash” and use mysql to connect to mariadb service ok.
This is nginx container error log when it try to run abc.php

2018/07/05 02:33:37 [error] 7#7: *3 FastCGI sent in stderr: "PHP message: PHP Warning:  mysqli::__construct(): (HY000/2002): Connection refused in /opt/www/test/abc.php on line 7" while reading response header from upstream, client: 10.255.0.2, server: test.mydomain, request: "GET /abc.php HTTP/1.1", upstream: "fastcgi://10.0.0.5:9003", host: "test.mydomain.com"

Please give me some advice , thank you very much.

I fixed it, the $servername must be “service name” so it can connect

 <?php
$servername = "mariadb";
$username = "root";
$password = "***";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>