G
G
GrimJack2017-05-14 14:07:31
Docker
GrimJack, 2017-05-14 14:07:31

How to access mysql db outside container?

Working with docker-compose

docker-compose.yml
web:
  image: igontarev/php-server
  ports:
    - "80:80"
    - "443:443"
  volumes:
    - ./www:/var/www
    - ./sites:/etc/nginx/sites-enabled
    - ./logs/nginx:/var/log/nginx
    - ./logs/php:/var/log/php
    - ./cron:/etc/cron.d
    - ./supervisor:/etc/supervisor/conf.d
    - ./letsencrypt:/etc/letsencrypt
    - ./lib/letsencrypt:/var/lib/letsencrypt
db:
  image: mysql:5.7
  ports:
    - "3306:3306"
  environment:
    - MYSQL_ROOT_PASSWORD=nimda2017


It turns out we have 2 docker containers
sudo docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                      NAMES
30b0b955fdca        igontarev/php-server   "/usr/bin/supervisord"   7 seconds ago       Up 6 seconds        0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   docker_web_1
1d7ad0ed6081        mysql:5.7              "docker-entrypoint..."   7 seconds ago       Up 6 seconds        0.0.0.0:3306->3306/tcp                     docker_db_1


The web container on which nginx + php7 is running normally works from the outside (as evidenced by the fact that I can open a site located in the container via the domain).
The most interesting thing is that mysql workbench logs in and works without problems by server ip, and from the console and when installing scripts - connection timeout.
Ideally, it would be possible to access via loacalhost, but ip is also fine. How to achieve this?
I tried the bind address in my.cnf - mysql does not start at all swearing at this line.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i, 2017-05-30
@ilyarsoftware

It works without additional settings 127.0.0.1, here is a working scenario:

docker run --name db_test \
 -e MYSQL_DATABASE=test \
 -e MYSQL_ROOT_PASSWORD=root \
 -p 3306:3306 \
 -d mysql:5.5

On the host machine, it’s only worth it mysql-client, you can use any client, use the console, roll up the dams:
MYSQL_PWD=root mysql \
-u root -h 127.0.0.1 \
--init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" \
test < dump.sql

remove the dam
MYSQL_PWD=root mysqldump \
-u root -h 127.0.0.1 \
--compact \
--add-drop-table \
--extended-insert=FALSE \
test_moco_core > dump.sql

PS Keeping the base in the docker or setting up other ways is a matter of goals, I use it for unit tests and experiments, the goals have been achieved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question