0

https://hub.docker.com/_/mariadb

I'm curious whether I am going about this the wrong way??? I basically need to cat custom config variables to my.cnf. Can this be done via docker-compose.yml or would I better off creating a separate Dockerfile for SQL image creation, allowing remote connections, thus adding a user, GRANT, FLUSH, etc?

Here is my docker-compose.yml:

services:
  app:
    build: .
    ports:
      - "80:80"
    volumes:
      - ./project:/home
  sql:
    image: mariadb
    ports:
      - "3606:3606"

    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root

EDIT |

/docker-entrypoint-initdb.d/setup.sh

mysql -u root -e "CREATE USER 'apps'@'%' IDENTIFIED BY ''; GRANT ALL PRIVILEGES ON *.* TO 'apps'@'%'; FLUSH PRIVILEGES;"

Do I need to add something to docker-compose.yml???

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • 1
    Which variables do you want to add ? You could always use a bind-mount to put your own my.cnf in the right place, as seen in the doc : `If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mariadb container.` – Faeeria Mar 25 '21 at 14:42
  • That could work actually. Only I need to setup a basic user and multiple databases. It’s a dev environment so mounting db on the host isn’t really required, in fact I’d prefer to recreate and seed with a fixture or seed script (I think) – Alex.Barylski Mar 25 '21 at 14:54
  • Okay, I will create a possible answer – Faeeria Mar 25 '21 at 14:57

1 Answers1

0

You can leverage this : When a container is started for the first time [...], it will execute files with extensions .sh, .sql, .sql.gz, and .sql.xz that are found in /docker-entrypoint-initdb.d

You can create a script with the specs you need (user creation, etc.) and put it in the /docker-entrypoint-initdb.d directory. It will initialize the db with what you need, on top of other configuration you can put in /etc/mysql/conf.d

Just use a bind mount for those, or a custom image with ADD or COPY

Faeeria
  • 786
  • 2
  • 13
  • Not sure what i'm doing wrong, but I've updated the question to clarify what i've added. The setup.sh doesn't seem to be running?!? `docker-compose down/up` is sufficient to trigger sh script? – Alex.Barylski Mar 25 '21 at 15:40
  • How long did you wait ? For me it can take as much as 4 mins before everything is initialized (with a mysql image) – Faeeria Mar 25 '21 at 15:42
  • Well the image boots immediately, I shell into the container and can see mysql DB, users, etc. But no `apps` user, unless I specify it in the docker-compose as shown originally – Alex.Barylski Mar 25 '21 at 15:43
  • Yeah, it boots, but what about the logs ? – Faeeria Mar 25 '21 at 15:45
  • I think I figured out part of the issue, seen a Dockerfile example which says I need to *COPY* the init.sql (or whatever) to the container directory `/docker-entrypoint-initdb.d`??? – Alex.Barylski Mar 25 '21 at 15:48
  • You can either `COPY` it in a Dockerfile or mount it with bind-mounts. For example `-v yourDirectoryWithTheInitFile:/docker-entrypoint-initdb.d` – Faeeria Mar 25 '21 at 15:51
  • OK so volumes won't work - I guess because I'm developing on a Windows host with debian container (https://stackoverflow.com/questions/50325494/how-can-i-change-permission-of-mounted-volumes-in-docker-compose-yml-from-the-do) – Alex.Barylski Mar 25 '21 at 16:11
  • Being on Windows shouldn't be a problem... What exactly is wrong ? – Faeeria Mar 25 '21 at 16:41
  • The my.cnf file I copied over from host had permissions of host system, which caused the 777, which mysql didn't like when it attempted to read config it ignored it (according to error message). – Alex.Barylski Mar 25 '21 at 17:49