1

I'm new to Docker and might be confusing things but let's say I have multiple projects and I want to use the same docker MySQL database with them.

Some of these projects also run Docker, some do not, the ones running Docker have docker-compose.yml in repos so I cannot change this file, for the others I don't want to use Docker at all (excepting the base container).

I pulled https://hub.docker.com/_/mysql/ already then run it like this:

docker run --name mysql-experiment -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.21

When I run docker ps I can see mysql-experiment is running.

I'm able to login to the container using docker exec -it mysql-experiment bash. But I'm not good enough with MySQL to manage it through Terminal.

My question is - how could I setup this container so it gets its own port, and IP and I can use it anywhere within my local network, without docker-compose?

Right now it runs on localhost on port 3306 (according to docker ps), but when I try to access 127.0.0.1:3306 in Sequel Pro I'm getting:

Unable to connect to host 127.0.0.1, or the request timed out.

Also, I'm not sure what username I should use with this container (I've tried admin/root/empty)

[edit]

I know there are many questions like this one, but all of them involve docker-compose.

Wordpressor
  • 7,173
  • 23
  • 69
  • 108

2 Answers2

0

At the moment, the mysql process runs on port 3306 in the container itself. You can verify that the process is running by ssh-ing into the container using docker exec, like what you did, and do the following:

mysql -u root -p root -h localhost

The localhost here belongs to the mysql container. In order to access your mysql process through your local environment (which I assume is the docker host in this case), you need to publish the port. This can be done using the -p flag in docker run:

docker run --name mysql-experiment -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.21

Here, we are binding the host's 3306 port to the container's 3306 port. If you want to access mysql using a different port on your localhost, say 3307, you can do -p 3307:3306.

Ref: https://docs.docker.com/engine/reference/run/#expose-incoming-ports

Jay Lim
  • 402
  • 3
  • 6
0

I would suggest you always use clean mysql db in all your docker-compose services. Your services will be more independent and it is easy to manage access in one docker-compose.yml

But if you want to use just one mysql i would recommend

create volume to store data, you should never write to container filesystem in longterm

docker volume create my_mysql_data

run mysql and map the port to host with -p

docker run -d -p 3306:3306 --restart always \ # port and restart policy
-v my_mysql_data:/var/lib/mysql \ # use the volume created before
--name mysql-experiment -e MYSQL_ROOT_PASSWORD=root \
mysql:5.7.21

access mysql from other docker container using host IP or use this --link option allows this "my_web to access mysql container on address mysql

docker run -d --name my_web \
--link mysql-experiment:mysql \
debian

if you are on mac and in docker-compose contianer you can get the IP of the host (your mac) with docker.for.mac.host.internal as it is in this answer

Mazel Tov
  • 2,064
  • 14
  • 26