0

I have a MariaDB Docker container that I want to access automatically with the help of a bash script.

I'm using Docker compose:

version: '3'
services:
  drupal:
    image: jonasvbogaert/php-docker:${IMAGE_VERSION}
    container_name: drupalenv
    ports:
      - 8080:80
    volumes:
      - /var/www/html/
    restart: always
    environment:
      DRUPAL_SITE_NAME: Drupal
      DRUPAL_USER: admin
      DRUPAL_PASS: admin
  mariadb:
    image: mariadb:latest
    container_name: mariadbenv
    restart: always
    ports:
      - 3036:3036
    depends_on:
      - drupal
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
      MYSQL_DATABASE: drupal`

The first command is to dive in the container (works fine):

docker exec -it mariadbenv bash

But the second one:

mysql

outputs the following error:

ERROR 1045 (28000): Access denied for user 'jonasvb'@'localhost' (using password: NO) the input device is not a TTY

When I enter "mysql" myself, it works.

This is the script I use:

function main () {
  getUserInput
}


function fireDocker () {
  if [ $DRUPAL_VERSION = "7" ]; then                            
    IMAGE_VERSION="drupal7"                                     
    export IMAGE_VERSION                                        
    docker-compose up -d                                         
    mountDump
  else
    IMAGE_VERSION="drupal8"                                     
    export IMAGE_VERSION                                        
    docker-compose up -d                                        
    mountDump
  fi
}

function getUserInput () {
  echo "Enter Drupal version (7 or 8)"
  read DRUPAL_VERSION                                           # Read 
  fireDocker $DRUPAL_VERSION
}

function mountDump(){
  docker exec -it mariadbenv bash
  mysql

}


main

EDIT

When I execute the first command without -t flag. I have this: enter image description here

And it stays like this.

14
  • remove the t. Commented Aug 24, 2017 at 9:10
  • Removed the "t", but now it keeps executing (no output). Commented Aug 24, 2017 at 9:15
  • what about executing bash -c mysql Commented Aug 24, 2017 at 9:16
  • What do you want to do with the mysql command exactly? You need to manually do something or you want to automate some execution? Commented Aug 24, 2017 at 9:18
  • Same result as @123 answer. Commented Aug 24, 2017 at 9:18

1 Answer 1

1

You can run mysql commands in container using

docker exec -i some_mysql_container mysql --user=root --password=root  <<< "select database();"

Here the password and username should match with the one that is being used in the container.

A better approach in your cases would be to place all the dumps in the host and then map that host directory inside container at /docker-entrypoint-initdb.d. For a better understanding how all dumps are imported in container you may look at official entrypoint.sh of mariadb:latest

L170 onwards:

    for f in /docker-entrypoint-initdb.d/*; do
        case "$f" in
            *.sh)     echo "$0: running $f"; . "$f" ;;
            *.sql)    echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
            *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
            *)        echo "$0: ignoring $f" ;;
        esac
        echo
    done

This enables users to place everything inside /docker-entrypoint-initdb.d/ and then

  • run bash scripts from there
  • run sql scripts
  • restore archived databases
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.