0

I'm trying to set up a database with a table inside a docker container. For correct working of DB, I need to run the following command:

--default-authentication-plugin=mysql_native_password
ps: I don't understand what this command exactly is for, but it prevents some strange logs by setting the DB up.

For set up, I use docker-compose as follow:

db:
image: mysql
command: >
  bash -c "--init-file /pictureapi_mydb_response.sql
  && --default-authentication-plugin=mysql_native_password"
volumes:
  - ./pictureapi_mydb_response.sql:/pictureapi_mydb_response.sql
restart: always
ports:
  - 3307:3306
environment:
  MYSQL_ROOT_PASSWORD: ${DB_MYSQL_PASS}

source

I'm getting the following errors:

bash: --: invalid option db_1 | Usage: bash [GNU long option] [option] ...
db_1 | bash [GNU long option] [option] script-file ...
db_1 | GNU long options:
db_1 | --debug
db_1 | --debugger\

How should I actually run two or more commands if "bash" instruction doesn't work?

1 Answers1

1

The Docker Hub mysql image is configured so that, if the command: starts with -, the entire command is assumed to be mysqld startup options. It's not actually "a command", and you can't use bash to run it. If you need multiple startup options, just combine them together into a single command::

command: --init-file /pictureapi_mydb_response.sql --default-authentication-plugin=mysql_native_password

When you launch a Docker container, its "entrypoint" and "command" parts are combined into a single command, so the command is passed as additional arguments to the entrypoint if both are present. The most common pattern is that the command is a complete executable command on its own, but there's an alternate pattern where the entrypoint is (or provides) the main command to run and the command just provides extra options.

The Docker Hub mysql container has a rather involved entrypoint script, but it eventually concludes with this logic:

# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = '-' ]; then
    set -- mysqld "$@"
fi

That is, if you run a container with

command: --an-option-starting-with-minus

the actual command the container runs is mysqld --an-option-starting-with-minus. So if you have multiple mysqld options you need to set, you can just pass them as the "command" and they'll get handled appropriately.

David Maze
  • 130,717
  • 29
  • 175
  • 215