9

I've created a docker container containing an instance of mariadb, but i cannot access to the database from my phisical machine:

enter image description here

I've got the ip address from docker inspect and the port from docker ps but Sequel Pro gave me the connection failed message (same thing with Visual Studio Code). Obviously from inside the docker container I can connect myself to the database engine.

Where am i wrong? Thanks so much to everyone! :)


[EDIT] Thanks to all comments...

if I try to expose the port, the container doesn't run :/ enter image description here

Lollo
  • 535
  • 2
  • 5
  • 18
  • Can you ping the mariadb container? What about if you run nmap on your localhost to see what ports are exposed? – cnizzardini Jan 04 '20 at 14:33
  • 1
    You need to bind Port 3306 inside the container to a port outside the container. You would then use that outside port from Visual Studio code and SQL Pro. – bishop Jan 04 '20 at 14:33
  • When you run the container you should bind the Port using the argument `-p 3306:3306` – med.b Jan 04 '20 at 14:35
  • @bishop Beeing on the machine where the container runs, there is no need to map the port `3306` to the outside of the container, using the containers IP should be sufficient. – t.niese Jan 04 '20 at 14:59
  • @med.b port binding is only required if you need to access the container from outside the machine where Docker is running on (or more precisely if you are not in the same subnet as the containers) – t.niese Jan 04 '20 at 15:02
  • The grants for your user are probably only defined for `localhost` and not for your physical machines `172.17.0. …` address. – t.niese Jan 04 '20 at 15:06
  • @t.niese i use root user at the moment... how can i give the grants to root user? – Lollo Jan 04 '20 at 16:39
  • I've never used a PNG file to connect to a database. Can you replace those images with a description, in text, of how exactly you started the containers and how you're trying to connect? – David Maze Jan 04 '20 at 16:46
  • 1
    The Docker-internal IP addresses only work on one very specific setup (calling from outside Docker, on the same machine, where it's a native-Linux host) and I'd suggest never looking them up at all. If you're trying to access the database remotely or it's a MacOS host, thither will not work. – David Maze Jan 04 '20 at 16:47

1 Answers1

19

It's worked for me:

  1. Create a new mariadb container
docker container run \
        --name sql-maria \
        -e MYSQL_ROOT_PASSWORD=12345 \
        -e MYSQL_USER=username \
        -e MYSQL_PASSWORD=12345 \
        -e MYSQL_DATABASE=dbname \
        -p 3306:3306 \
        -d mariadb:10
  1. Watch the logs and wait for mariadb server is up
docker container logs -f sql-maria

The tail of the log should look something like this

2020-02-04 20:02:44 0 [Note] mysqld: ready for connections.

  1. Use a client of your choice to connect to mariadb. I'm using mysql client here
mysql -h 127.0.0.1 -p -u username dbname

If you are on a unix-based system it is mandatory to use the loopback address 127.0.0.1 instead of localhost

  • 1
    That last line, "it is mandatory to use the loopback address 127.0.0.1 instead of localhost," was the key for me. – Stephan Samuel Jan 04 '23 at 01:27
  • 1
    I think the question has a different perspective. Tunnelling the port will restrict us from using another instance of MySQL on the host machine! As each container is assigned with individual IP address, in my case 172.18.0.x, I want to be able to access the container MySQL through that IP, so that my host MySQL does not conflict with the container MySQL. Using different ports is never a convenient solution. – Broken Arrow Jan 14 '23 at 15:53