MySQL scripts not running in docker-entrypoint-initdb

I am trying to create multiple databases when starting a MySQL container. According to https://github.com/docker-library/mysql/pull/18 , I can mount or copy my scripts into /docker-entrypoint-initdb.dimages, and they will be automatically executed at startup.

However, my scripts are not executed at all. It docker-entrypoint.shdoes not seem to see the files in the directory /docker-entrypoint-initdb.d.

This is my Dockerfile:

FROM mysql
ADD script.sql /docker-entrypoint-initdb.d/script.sql
RUN chmod -R 775 /docker-entrypoint-initdb.d

ENV MYSQL_ROOT_PASSWORD mypass

This is mine script.sql:

CREATE DATABASE mydb;
CREATE DATABASE mydb2;

I build and run the container:

$ docker build -t mysql .
$ docker run -v data_volume:/var/lib/mysql --name somedb -d mysql

When I access the container in tty, I see what script.sqlis in /docker-entrypoint-initdb.dbut not executed.

I saw the conclusion docker logs -f somedbbut there is no error.

I tried with the file .sh, I also tried with Maria-db, the result is the same.

?

+10
7

, data_volume . sql .

:

docker-entrypoint.sh mysql /var/lib/mysql.

if [ ! -d "$DATADIR/mysql" ]; then
    //Some other logic here

    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 

Dockerfile

+7

, docker-entrypoint.sh. , $ DATADIR/mysql, /var/lib/mysql, , datadir, .. -EntryPoint-initdb.d

, init.sh, , Docker.

-compose.yml:

volumes:
  - ./docker/mysql/scripts:/docker-entrypoint-initdb.d
  - ./mysql_data:/var/lib/mysql

init.sh:

#!/bin/bash
rm -rf mysql_data
docker-compose up --force-recreate

, , -d docker-compose , .

+5


, mariadb (, , docker-entrypoint.sh, mysql), . , , :)

.sql, "docker-entrypoint-initdb.d", , docker-compose.yml (Linux), (Windows 7),

( .sql CREATE TABLE...), .


:

  • EOL .sql CRLF LF;
  • chmod "docker-entrypoint-initdb.d" ( );
  • "docker exec" ( , , );
  • *.sh "docker-entrypoint-initdb.d" ( , .sql);

. :

$ docker logs someappname_db >& logs_db.txt

, docker-entrypoint.sh Linux, @anteverse:

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

"docker exec", :

Linux:

root@deda78e393f9:/# ls -l
total 72
drwxr-xr-x    2 root root 4096 Mar 19 19:53 bin
drwxr-xr-x    2 root root 4096 Nov 19 16:32 boot
drwxr-xr-x    5 root root  340 Apr 29 15:53 dev
**drwxrwxrwx+   2 root root 4096 Apr 29 11:30 docker-entrypoint-initdb.d**
lrwxrwxrwx    1 root root   34 Mar 14 06:26 docker-entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
...

Windows 7:

root@a3ff19060712:/# ls -l
total 64
drwxr-xr-x   2 root root  4096 Feb 17 09:07 bin
drwxr-xr-x   2 root root  4096 Nov 19 16:32 boot
drwxr-xr-x   5 root root   340 Apr 29 16:04 dev
**drwxrwxrwx   1 1000 staff    0 Apr 28 18:13 docker-entrypoint-initdb.d**
lrwxrwxrwx   1 root root    34 Feb 17 09:08 docker-entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
...

UID: GID? 1000: Linux?

Linux 'admin', , root ( ): admin ( ) thingy?


: Windows 7, Docker 18.02.0-CE
: Linux (QNAP TS-253A), Docker 17.07.0-CE

[/share/Container/] # uname -or
4.2.8 GNU/Linux

[/share/Container/] # cat /etc/issue
Welcome to TS-253A, QNAP Systems, Inc.

[/share/Container/] # cat /etc/os-release
NAME="QTS"
VERSION="4.3.4 (20180215)"
ID=qts
PRETTY_NAME="QTS 4.3.4 (20180215)"
VERSION_ID="4.3.4"

-compose.yml

version: '3'
services:

  db:
    container_name: someappname_db
    image: mariadb:latest
    restart: on-failure:3
    environment: 
      - MYSQL_DATABASE=someappname
      - MYSQL_ROOT_PASSWORD=someappname
      - MYSQL_USER=someappname
      - MYSQL_PASSWORD=someappname
      - MYSQL_ROOT_HOST=%
      - TZ=Europe/Stockholm
    ports:
      - 3306:3306  
    volumes:
      - dbdata:/var/lib/mysql      
      - /share/Container/someappname/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:ro
    networks:
      - net
    command: ["mysqld", "--user=mysql", "--lower_case_table_names=1"]

    # other services

volumes:
  dbdata:

networks:
  net:

:
:

$ cd /share/Containers/someappname
$ docker stop $(docker ps -a -q --filter="name=someappname") && docker rm $(docker ps -a -q  --filter="name=someappname") && docker volume rm someappname_dbdata
$ docker-compose -f someappnameprod-docker-compose.yml up -d db

!

+2

?

/docker -entrypoint-initdb.d/

, : https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile#L70

-entrypoint.sh /usr/local/bin .

/docker -entrypoint-initdb.d/, :

for f in /docker-entrypoint-initdb.d/*; do

, .

:

for f in /usr/local/bin/docker-entrypoint-initdb.d/*; do

, Dockerfile:

COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

COPY docker-entrypoint-initdb.d/ /usr/local/bin/docker-entrypoint-initdb.d/
RUN chmod -R +x /usr/local/bin/docker-entrypoint-initdb.d

/docker -entrypoint-initdb.d/test.sql

CREATE DATABASE `test`;

, , , .

+1

, :

RUN mkdir -p /docker-entrypoint-initdb.d && mv myScript.sql /docker-entrypoint-initdb.d/myScript.sql

, - , . , 775 - ? btw im, FROM mysql: 5

0

. , . Dockerfile:

FROM mysql:5.7.17
ADD scripts/init.sql /docker-entrypoint-initdb.d/

RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/

CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
0

mariadb (: 10.4), , , , .

:

  mariadb:
    image: mariadb:10.4
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ********
    volumes:
    - ./storage/db:/var/lib/mysql:rw
    - ./app/db/SQL:/docker-entrypoint-initdb.d/:rw
    ports:
    - 3306: 3306 / tcp

For me, I just had to make sure that this path: ./ storage / db is empty of the files. Note that the directory must exist, but be empty.

0
source

Source: https://habr.com/ru/post/1648649/


All Articles