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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

8 Comments

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 :(
Did you run docker-compose logs for the mysql container? Just to make sure it’s not logging any error on startup.
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.
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
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.
|

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.