Correctly using mysql commands in docker-compose?

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?

Answer

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.