Mysql: Why is MYSQL_DATABASE env var being ignored by MySQL docker container?

Created on 29 Sep 2019  路  7Comments  路  Source: docker-library/mysql

I've got the following docker file:

version: "3.7"
services:
  db:
    container_name: db
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "mysql"
      MYSQL_DATABASE: "mydb"
    security_opt:
      - seccomp:unconfined
    volumes:
      - ./supplied/init:/docker-entrypoint-initdb.d/:ro

When I run 'docker-compose up' I get this in the logs:

Recreating db ... done
Attaching to db

If I check the available databases I see:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db                 |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

Why is the MYSQL_DATABASE env var being ignored?

I'm running on windows 10.

Thanks,
Pete

EDIT :

This looks like some sort of caching issue. I changed the service name and container_name in the docker-compose file and it started working... Is this the expected behaviour? I have similar issues with running SQL scripts from within /docker-entrypoint-initdb.d. They only seem to run the first time I call docker-compose up. After that the result is cached, even if I run docker-compose up --force-recreate.

question

Most helpful comment

@kdanW Right I'd mispoke, for using an official-image the entrypoint scripts check for existing installations/data, so it only executes its task once per container creation. It runs everytime but does nothing after the first
https://github.com/docker-library/mysql/blob/d56d41dd7ceb73084f42c775f133ef013ab5ff1c/8.0/docker-entrypoint.sh#L100

All 7 comments

The entrypoint is only ever executed once per container creation, if you do a docker-compose down it should be fully removed and not just "stopped", as restarting/starting a stopped container won't rerun the entrypoint
https://docs.docker.com/engine/reference/commandline/ps/#show-both-running-and-stopped-containers

$ docker-compose up -d
Creating network "2_default" with the default driver
Pulling db (mysql:)...
latest: Pulling from library/mysql
8f91359f1fff: Already exists
6bbb1c853362: Already exists
e6e554c0af6f: Already exists
f391c1a77330: Already exists
414a8a88eabc: Already exists
fee78658f4dd: Already exists
9568f6bff01b: Already exists
5a026d8bbe50: Pull complete
07f193b54ae1: Pull complete
1e404375a275: Pull complete
b81b2ef0e430: Pull complete
2f499f36bd40: Pull complete
Digest: sha256:6d95fa56e008425121e24d2c01b76ebbf51ca1df0bafb1edbe1a46937f4a149d
Status: Downloaded newer image for mysql:latest
Creating db ... done

$ grep "mysqld: ready" <(docker logs db 2>&1)
2019-09-30T16:10:46.218707Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2019-09-30T16:11:18.652850Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

$ docker exec -it db mysql -uroot -pmysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql>

@wglambert are you sure that the docker entrypoint only ever executed once per container creation and not each time the container starts? I have added the code to start my server as the ENTRYPOINT.
ENTRYPOINT ["/home/docker-entrypoint.sh"] and my server is restarted each time i stop the containers and run again. As per your explanation this cannot happen as the entrypoint is not run when a container is stopped and started. An explanation is highly appreciated :D Thanks

It's only executed if the data-dir was not yet present in the container; https://github.com/docker-library/mysql/blob/381a03b13962b4d93da00370afa096063778889c/8.0/docker-entrypoint.sh#L100-L181

However, if you're using a (named) volume for your data-dir, then those files are created the first time you run the container. docker-compose down does not automatically remove volumes (because they contain your data, which is usually important, and you want to preserve it)

To remove the old data (which will delete your actual databases), you can use docker-compose down -v (the -v removes both containers, and volumes). Once the volumes are removed, the next time you run docker-compose up, new containers and new volume(s) will be created, and the entrypoint script should run

@kdanW Right I'd mispoke, for using an official-image the entrypoint scripts check for existing installations/data, so it only executes its task once per container creation. It runs everytime but does nothing after the first
https://github.com/docker-library/mysql/blob/d56d41dd7ceb73084f42c775f133ef013ab5ff1c/8.0/docker-entrypoint.sh#L100

Thanks for the clarification @wglambert . Does https://github.com/docker-library/mysql/blob/381a03b13962b4d93da00370afa096063778889c/8.0/docker-entrypoint.sh#L94 run each time we restart the server? I know its not good practice to overwrite the entrypoint,but if I want to put a custom code which runs each time the server starts(or restarts) where can I put in the entrypoint?

@kdanW, yes, you'd want to be inside that if, but not inside the next one, since the second is the block that skips on an already initialized database.

https://github.com/docker-library/mysql/blob/d56d41dd7ceb73084f42c775f133ef013ab5ff1c/8.0/docker-entrypoint.sh#L100

Just a warning that the database will not be running since it is only started at the end of the script (exec "$@") when it has already been initialized.

Closing since it seems like the original question has been solved

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcandre picture mcandre  路  4Comments

bernex picture bernex  路  4Comments

jicki picture jicki  路  3Comments

perfeyhe picture perfeyhe  路  4Comments

seangerhardt-wf picture seangerhardt-wf  路  4Comments