ERROR 1698 (28000): Access denied for user when trying to connect to docker mysql container from host machine

I’ve encountered a docker/mysql problem as described below:

I have docker-compose.yml running a simple mysql service:

version: '3.5'
services:
  db:
    image: "mysql:8"
    restart: always
    environment:
      MYSQL_DATABASE: 'test'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'test'
      MYSQL_ROOT_PASSWORD: 'test'
      MYSQL_PORT: 3306
    ports:
      - "3399:3306"
    expose:
      - "3306"
    volumes:
      - container-volume:/var/lib/mysql
      - ./station.sql:/docker-entrypoint-initdb.d/station.sql

volumes:
  container-volume:

when I execute docker-compose up, the following error shows:

ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'@'%'

However, the container is still up and running.

I try to connect to the mysql service running in the docker container by running
mysql -s -h localhost --port=3399 -uroot -ptest in the host machine. Then I got this error:

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

I’ve searched a dozen solutions that deals with ERROR 1698 (28000), but none of them worked. I’ve done:

ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'test';
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';

flush privileges;

and I’ve exit and restarted docker container. I wanted to do sudo service restart mysql or sudo systemctl restart mysql within the container but these are not possible for the minimal mysql container. docker restart [containerid] basically do the same.
I can confirm that the plugin has changed:

mysql> select user, host, plugin from mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

mysql> 

However, when I try to connect to docker sql through port 3399 on host, i;m still getting

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

This is consistent with the error logging that I have for the python project i’m running:

192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /statistics HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /static/image/favicon.ico HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /user/check_login HTTP/1.1" 200 -
192.168.1.124 - - [06/Dec/2019 15:07:44] "GET /tenants/get HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
    rec = pool._do_get()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
    self._dec_overflow()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 129, in reraise
    raise value
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
    return self._create_connection()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 453, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
    self.connect()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 599, in connect
    self._request_authentication()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 861, in _request_authentication
    auth_packet = self._read_packet()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/my_web_project/venv/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1698, "Access denied for user 'root'@'localhost'")

Any suggestions?

Source: StackOverflow