6

My docker-compose script successfully manages to run a mysql/mariadb service, and copies my "init.sql" script containing the database schema to "/docker-entrypoint-initdb.d". However, the sql script is never executed like it should according to the docs.

There is no error, nothing in the logs. I checked that the sql file is copied at the right place in the container. I checked that the script runs with no errors on an empty database.

What am I missing? Is it even visible in the logs if the script was executed (and for some reason did nothing)?

(Edit: docker-compose version: 1.8.1, image mariadb:10.1.21)

# docker-compose.yml

version: '2'
volumes:
  data-volume: {}
services:
  mysql:
    image: mariadb
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: pwd
      MYSQL_DATABASE: users_db
    volumes:
      - data-volume:/var/lib/mysql
      - ./resources/docker-sql/init.sql:/docker-entrypoint-initdb.d/init.sql
  backend:
    image: myapp
    ports:
      - "8000:80"
    depends_on:
      - mysql
    links:
      - mysql
4
  • 1
    According to the source of the docker-entrypoint.sh (the script which is supposed to execute your init.sql) there should be line in the logs if it is executed (line 154). Could you add the output of docker logs? Furthermore, your script will only be executed the first time the container is started (line 76)! You have to delete it between tries. Commented Feb 9, 2017 at 13:03
  • Oh, I had to delete the container indeed. Thanks! docker-compose stop, docker-compose rm, docker-compose up. Commented Feb 9, 2017 at 13:13
  • Whould any of you to write an official answer? :) Commented Feb 9, 2017 at 13:18
  • Note that there are no logs printed relative to the sql script actually being run, though. Maybe there is a verbose option. But not a problem anymore. If @fzgregor doesn't write an answer of it I'll do it tomorrow. Commented Feb 9, 2017 at 13:23

2 Answers 2

14

Looking at the source of the docker-entrypoint.sh file of the mariadb image, the script which is supposed to execute your init.sql, it becomes clear that

  1. the script will only be executed when the container didn't contain a database before (line 76 of the script), and
  2. there will a log output informing you that you script was called.

To achieve this with docker-compose you have to stop the service, remove the container (to get rid of the database) and start it again:

docker-compose stop
docker-compose rm
docker-compose start

Here is an example of the image's behavior, only with a .sh file instead of .sql:

$ cat hello.sh 
echo "This is output of the hello script"
$ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb   
Initializing database
2017-02-09 14:13:54 140617005938624 [Note] /usr/sbin/mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 63 ...
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: The InnoDB memory heap is disabled
2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
[...]
2017-02-09 14:14:03 139836212086528 [Note] InnoDB: Dumping buffer pool(s) not yet started
2017-02-09 14:14:03 139836971521984 [Note] Plugin 'FEEDBACK' is disabled.
2017-02-09 14:14:03 139836971521984 [Warning] 'user' entry 'root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:14:03 139836971521984 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:14:03 139836971521984 [Note] mysqld: ready for connections.
Version: '10.1.21-MariaDB-1~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  mariadb.org binary distribution
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
GENERATED ROOT PASSWORD: dau6voh4eej2jooRohpiop4eh6ahl7Uz
2017-02-09 14:14:05 139836970654464 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/hello.sh
This is output of the hello script

2017-02-09 14:14:05 139836970351360 [Note] mysqld: Normal shutdown

2017-02-09 14:14:05 139836970351360 [Note] Event Scheduler: Purging the queue. 0 events
2017-02-09 14:14:05 139836195301120 [Note] InnoDB: FTS optimize thread exiting.
2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Starting shutdown...
2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Waiting for page_cleaner to finish flushing of buffer pool
2017-02-09 14:14:07 139836970351360 [Note] InnoDB: Shutdown completed; log sequence number 1616829
2017-02-09 14:14:07 139836970351360 [Note] mysqld: Shutdown complete


MySQL init process done. Ready for start up.
[...]

You see somewhere in the log output the call and its stdout of the hello.sh script is buried. On subsequent starts of the container the script is not executed because the database is already created (in the local mariadb volume):

$ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb
2017-02-09 14:19:13 140155189532608 [Note] mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 1 ...
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using mutexes to ref count buffer pool pages
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: The InnoDB memory heap is disabled
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Compressed tables use zlib 1.2.8
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using Linux native AIO
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using SSE crc32 instructions
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Completed initialization of buffer pool
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Highest supported file format is Barracuda.
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: 128 rollback segment(s) are active.
2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Waiting for purge to start
2017-02-09 14:19:13 140155189532608 [Note] InnoDB:  Percona XtraDB (http://www.percona.com) 5.6.34-79.1 started; log sequence number 1616839
2017-02-09 14:19:13 140154429736704 [Note] InnoDB: Dumping buffer pool(s) not yet started
2017-02-09 14:19:13 140155189532608 [Note] Plugin 'FEEDBACK' is disabled.
2017-02-09 14:19:13 140155189532608 [Note] Server socket created on IP: '::'.
2017-02-09 14:19:13 140155189532608 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode.
2017-02-09 14:19:13 140155189532608 [Note] mysqld: ready for connections.
Version: '10.1.21-MariaDB-1~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
Sign up to request clarification or add additional context in comments.

3 Comments

Anyone else with an issue the commands docker-compose stop docker-compose rm are very key as they help you start over . Otherwise your changes to the compose file might not be effected.
@GideonMaina You're missing the && sign. docker-compose stop && docker-compose rm. I used docker-compose down && docker-compose up worked for me.
docker-compose down -v does this.
2

Maybe a little late, but I have found another more accurate solution.

Through depends_on and healthcheck, don't run backend until mysql dump has finished. The only detail is that you must run it with version 2.1.

# docker-compose.yml

version: '2.1'
volumes:
  data-volume: {}
services:
  mysql:
    image: mariadb
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: pwd
      MYSQL_DATABASE: users_db
    depends_on:
      backend:
        condition: service_healthy
    volumes:
      - data-volume:/var/lib/mysql
      - ./resources/docker-sql/init.sql:/docker-entrypoint-initdb.d/init.sql
  backend:
    image: myapp
    ports:
      - "8000:80"
    depends_on:
      - mysql
    links:
      - mysql
    healthcheck:
      test: "/usr/bin/mysql --user=root --password=you_pass --database=your_db --execute \"SELECT * FROM last_table_of_your_dump;\""
      interval: 10s
      timeout: 3s
      retries: 10

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.