plitvica plitvica - 11 months ago 106
SQL Question

Cannot connect from MySQL Workbench to dockerized MySQL server

I'm trying to connect from the MySQL Workbench to my dockerized mysql server. I'm using Windows 10. Here is my Dockerfile:

FROM ubuntu:latest

# package updates & install mysql
RUN apt-get update && apt-get install -y mysql-server
RUN apt-get -y install supervisor

ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# bind sql script
ADD musicdb.sql /tmp/musicdb.sql

RUN sed -i -e"s/^bind-address\s*=\s* =" /etc/mysql/my.cnf


RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE musicdb" && \
mysql -u root musicdb < /tmp/musicdb.sql

CMD ["/usr/bin/supervisord", "-n"]

I'm using this library to create, run, etc. the container from Java:

My actual method to do this in Java is:

public static void createContainerWithPorts(){
DockerClient docker;

try {
docker = DefaultDockerClient.fromEnv().build();

Map<String, List<PortBinding>> portBindings = new HashMap<>();

List<PortBinding> hostPorts = new ArrayList<>();

hostPorts.add(PortBinding.of("", "3306"));
portBindings.put("3306", hostPorts);

HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image("mysql-container").exposedPorts("3306").build();

//create container
ContainerCreation contaierCreation = docker.createContainer(containerConfig);
String id =;

//start container

ContainerInfo containerInfo = docker.inspectContainer(id);

Scanner scanner = new Scanner(;;
} catch (DockerCertificateException e) {
} catch (InterruptedException e) {
} catch (DockerException e) {

Java builds the Container fine from the before created Docker Image. When I'm looking on actual running Docker Container it looks that the port binding works. When I access my container with
docker exec -it SQLContainer bash
it works fine and I can access my SQL server.

When i try to connect with MySQL Workbench, I got the error message shown in the picture below:
MySQL Workbench Error Message

Does someone know what I'm doing wrong?



I think you need to create a new user for your mysql server and specify from where the user can be used. This is because root access is only available through localhost I believe.

So to create a new user, you need to add a little something to the RUN command where you start your server:

RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \
sleep 5 && \
mysql -u root -e "CREATE DATABASE musicdb" && \
mysql -u root musicdb < /tmp/musicdb.sql && \
mysql -u root -e "CREATE USER 'newUser'@'%' IDENTIFIED BY 'somePassword'" && \
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'newUser'@'%' WITH GRANT OPTION"

That way you create a new user with the name newUser that can access from anywhere.