Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remote access to mariadb on docker?

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

like image 616
Lollo Avatar asked Dec 08 '22 11:12

Lollo


1 Answers

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

like image 104
Adonias Alcântara Avatar answered Dec 11 '22 10:12

Adonias Alcântara