2

Trying to use docker compose for my application, so using official mysql images and my own Dockerfile to create my app image. I think some how my initdb.sql is not executed during docker compose up.

Running this as :

docker-compose up --build

But I can not see that initdb.sql is executed.

My docker compose looks like this:

version: "3"
services:
  db:
    image: mysql:5.7
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: "MYDB"
      MYSQL_USER: "myuser"
      MYSQL_PASSWORD: "mypassword"
      MYSQL_ROOT_PASSWORD: "mypassword"
    ports:
      - "3306:3306"
    expose:
      - "3306"
    volumes:
      - my-db:/var/lib/mysql
      - ./sqlinit:/docker-entrypoint-initdb.d/initdb.sql
    networks:
      vpcbr:
        ipv4_address: 10.5.0.6
  app:
    restart: always
    build: .
    ports:
      - 5000:5000
    volumes:
      - .:/app
    depends_on:
      - db
    networks:
      vpcbr:
        ipv4_address: 10.5.0.5

volumes:
  my-db:

networks:
  vpcbr:
    driver: bridge
    ipam:
      config:
        - subnet: 10.5.0.0/16
        #  gateway: 10.5.0.1

As you can see I have my initdb.sql in this directory ./sqlinit and after I login to mysql container I can see this file in /docker-entrypoint-initdb.d/initdb.sql

But looks like is not executed because when I login to my app I can see:

pymysql.err.OperationalError: (1044, "Access denied for user 'myuser'@'%' to database 'MYDB'")

and on container mysql

2019-09-15T13:17:59.361447Z 2 [Note] Access denied for user 'myuser'@'%' to database 'MYDB

Of course I'm doing something wrong, I found some similar problems here but I still haven't fixed my issue

GMB
  • 216,147
  • 25
  • 84
  • 135
Karol
  • 165
  • 2
  • 6
  • 19

1 Answers1

2

The problem should be in the way you are mapping the volume. You should map the source file into the target file instead of a source folder into a file, like so:

- ./sqlinit/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql
rph
  • 2,104
  • 2
  • 13
  • 24
  • Hi @picadoh, thank you for your comment. I dont think this is a problem because script is on container /docker-entrypoint-initdb.d/initdb.sql and I don't think I need to specify any file in ./sqlinit. I can have there more *.sql and all should be executed. Anyway I did try your solution and the same issue :( – Karol Sep 15 '19 at 14:06
  • Did you run `docker-compose logs` for the mysql container? Just to make sure it’s not logging any error on startup. – rph Sep 15 '19 at 14:08
  • Also worth logging in with root user to mysql just to make sure things are getting created. It might just be a matter of not granting the right permissions to that user. Or run that script manually to understand if there’s a problem with the script itself. – rph Sep 15 '19 at 14:12
  • 1
    Hi @picadoh, I think I know what was wrong :( Because I was playing around with create volumes many times - I guess. Now removed volume `my-db` and run docker-compose up --build and I can not see the same error, I can see other like some tables are not created but this is other issue. DB is created and other stuff so script was executed. SO in my case I should delete volume every time I change something in docker-compose.yml - Thank you – Karol Sep 15 '19 at 14:23
  • Sorry I set too early as soultion, but looks like I can see this error db_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/initdb.sql db_1 | mysql: [Warning] Using a password on the command line interface can be insecure. db_1 | ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device. – Karol Sep 15 '19 at 14:38
  • Are you running on Windows? – rph Sep 15 '19 at 15:15
  • By Docker design, files are created as folders in the container if it’s not able to locate the files correctly in the source, which leads to that error. Generally, it might be a matter of a misspelled file/folder name, access permissions to that file/folder, mapping files to folders or folders to files. Specifically for Windows, it might be that Docker was not granted access to the C: drive. Have a look at a similar case [here](https://github.com/docker-library/mariadb/issues/62). – rph Sep 15 '19 at 15:21
  • Hi @picadoh, you had right in your first comment. Sorry I had some strange issues, but in the end of the day your solution was good. Thank you very much :) – Karol Sep 18 '19 at 14:57