How can I access my docker maria db?

MorRich picture MorRich · Oct 16, 2015 · Viewed 29k times · Source

My main question is that after I have created a docker container for my mariadb with the command docker run --name db -e MYSQL_ROOT_PASSWORD=test -d -p 3306:3306 mariadb how can I access the sql db?

Somewhere I have seen a solution using a temporal (after exit the container is deleted) container, but cannot find it anymore.

I am searching for a command like: sudo docker exec -it [other flags] [command] db.

Answer

michaelbahr picture michaelbahr · Oct 16, 2015

Just mysql-client, no extra docker container

Install the mysql client on your host,

apt-get install mysql-client

then use the following command to access your database container.

mysql -u<user> -p<pass> -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' <db-container>)

The command will automatically get the IP of your docker container.

Make sure to replace <user>, <pass> and <db-container> with your respective values. In your case:

mysql -uroot -ptest -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' db)

Your command lets mariadb run at the standard port 3306. If not, you have to tell the mysql command the new port.