1

I am trying to learn docker-compose. I am having trouble running mysql commands through docker compose.

This is my docker-compose file:

version: '3'

services:
  mysql:
    image: mysql
    container_name: mysql
    restart: always
    env_file: .env
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASS
      - MYSQL_USER=$MYSQL_USER
      - MYSQL_PASSWORD=$MYSQL_PASS
      - MYSQL_DB=$MYSQL_DB
    volumes:
      - db-data:/var/lib/mysql
    command: >
      mysql -e "CREATE DATABASE \${MYSQL_DB}"
      && mysql -e "GRANT ALL PRIVILEGES ON \${MYSQL_DB}.* TO '\${MYSQL_USER}'@'%'"
      && mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
    ports:
      - $MYSQL_PORT:3306

volumes:
  db-data:

But when I run the docker-compose up --force-recreate --build, I get the mysql usage guide output:

Attaching to mysql
mysql    | mysql  Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
mysql    | Copyright (c) 2000, 2021, Oracle and/or its affiliates.
mysql    |
mysql    | Oracle is a registered trademark of Oracle Corporation and/or its
mysql    | affiliates. Other names may be trademarks of their respective
mysql    | owners.
mysql    |
mysql    | Usage: mysql [OPTIONS] [database]
mysql    |   -?, --help          Display this help and exit.
mysql    |   -I, --help          Synonym for -?
mysql    |   --auto-rehash       Enable automatic rehashing. One doesn't need to use
mysql    |                       'rehash' to get table and field completion, but startup
...etc...

Am I using the command: properly to send mysql commands?

John
  • 32,403
  • 80
  • 251
  • 422
  • Does this answer your question? [How to add a startup script to a mysql docker container?](https://stackoverflow.com/questions/46482257/how-to-add-a-startup-script-to-a-mysql-docker-container) – Hans Kilian Jul 16 '21 at 13:37
  • Thanks @HansKilian - that's what I'm currently doing. I was hoping to include it in my docker-cmpose file. But I suppose that's not a best practice. So i'll just use a different way – John Jul 16 '21 at 13:45
  • 1
    The way you've tried to do it is not really how `command` works. In most cases - including this - what you put in the command is more like a parameter to the entrypoint defined in the container. – Hans Kilian Jul 16 '21 at 15:15

1 Answers1

2

I want to suggest a different approach with MySQL:

You can mount another SQL script file that will run at the beginning:

version: '3'

services:
  mysql:
    image: mysql
    container_name: mysql
    restart: always
    env_file: .env
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASS
      - MYSQL_USER=$MYSQL_USER
      - MYSQL_PASSWORD=$MYSQL_PASS
      - MYSQL_DB=$MYSQL_DB
    volumes:
      - db-data:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - $MYSQL_PORT:3306

volumes:
  db-data:

now create init.sql in your docker-compose.yml directory:

CREATE DATABASE ${MYSQL_DB}
GRANT ALL PRIVILEGES ON ...

You can see it in Initializing a fresh instance.

ItayB
  • 10,377
  • 9
  • 50
  • 77