14

I am trying to access mysql databases from my docker host to the container.

It's my own dockerfile which install a database expose on port 3306. I launch my docker with docker-compose, and my compose file is mapping 3308 host port on 3306 container port.

I can access to mysql from the host like this : mysql -h localhost -P 3308 -u root -pMyPassword

It's working well, but what I can't figure out, is why I can't see any datas from my container?

From inside the container, I have a test databases which I can connect to without any problem. But when I connect from the host to the container mysql process, It seems to show me the mysql datas from the host machine, not from the container one.

Any ideas?

Thanks :)

EDIT 1 :

So here is the first way I can connect to mysql into the container :

docker exec -it MyContainer mysql -uroot -pMyPassword
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test_db            |
+--------------------+

It show me my db : test_db But If i access from :

mysql -h localhost -P 3308 -u root -pMyPassword
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

My test_db isn't here.

And the result of docker ps

CONTAINER ID        IMAGE                 COMMAND                  CREATED                 STATUS              PORTS                                                                                                    NAMES
a0de6a691d72        MyContainer           "docker-entrypoint.sh"   3 hours ago             Up 3 hours          9000/tcp, 0.0.0.0:8085->80/tcp, 0.0.0.0:3308->3306/tcp, 0.0.0.0:8084->8000/tcp, 0.0.0.0:8086->8080/tcp   MyContainer

EDIT 2 : I am developing a standard docker container for web hosting production environnement. Each host is controlled by ajenti. The host work with an nginx reverse proxy which redistribute websites on correct container. Every thing is wokring well. So here is my Dockerfile :

FROM php:5.6-fpm

RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    libxml2-dev \
    python \
    build-essential \
    make \ 
    gcc \
    python-dev \
    locales \
    python-pip

RUN dpkg-reconfigure locales && \
    locale-gen C.UTF-8 && \
    /usr/sbin/update-locale LANG=C.UTF-8

ENV LC_ALL C.UTF-8

ARG MYSQL_ROOT_PASSWORD

RUN export DEBIAN_FRONTEND=noninteractive; \
    echo mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD | debconf-set-selections; \
    echo mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | debconf-set-selections;

RUN apt-get update && apt-get install -y -q mysql-server php5-mysql

RUN rm /etc/apt/apt.conf.d/docker-gzip-indexes

RUN apt-get update && apt-get install -y wget

RUN wget http://repo.ajenti.org/debian/key -O- | apt-key add -
RUN echo "deb http://repo.ajenti.org/debian main main debian" > /etc/apt/sources.list.d/ajenti.list
RUN apt-get update && apt-get install -y ajenti cron unzip ajenti-v ajenti-v-php-fpm ajenti-v-mysql ajenti-v-nginx

RUN apt-get install -y python-setuptools python-dev \
    && easy_install -U gevent==1.1b3 \
    && sed -i -e s/ssl_version=PROTOCOL_SSLv3/ssl_version=PROTOCOL_SSLv23/ /usr/local/lib/python2.7/dist-packages/gevent-1.1b3-py2.7-linux-x86_64.egg/gevent/ssl.py

EXPOSE 80 8000 8080 3306 

RUN mkdir /tmp/tempfiles \
    && mv /srv /tmp/tempfiles \
    && mv /var/lib/mysql /tmp/tempfiles \
    && mv /var/log /tmp/tempfiles \
    && mv /etc/ajenti /tmp/tempfiles

COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat

ENTRYPOINT ["docker-entrypoint.sh"]

As I said, I wanted to be able do deploy a new container easily. So I created a docker-entrypoint.sh which copy wanted files to my volume when I start the container :

#!/bin/bash
DIR="/var/lib/mysql"
# look for empty dir 
if [ ! "$(ls -A $DIR)" ]; then
    cp -avr /tmp/tempfiles/mysql /var/lib/
fi
# rest of the logic

DIR="/srv"
# look for empty dir 
if [ ! "$(ls -A $DIR)" ]; then
    cp -avr /tmp/tempfiles/srv /
fi
# rest of the logic

DIR="/var/log"
# look for empty dir 
if [ ! "$(ls -A $DIR)" ]; then
    cp -avr /tmp/tempfiles/log /var/
fi
# rest of the logic

DIR="/etc/ajenti"
# look for empty dir 
if [ ! "$(ls -A $DIR)" ]; then
    cp -avr /tmp/tempfiles/ajenti /etc/
fi
# rest of the logic

Finally, my docker-compose.yml to launch everything and map ports :

version: '2'

services:
    ajenti:
        build: 
            context: ./dockerfiles/                                        
            args:                                                                      
                MYSQL_ROOT_PASSWORD: MyPassword
        volumes:
            - ./logs:/var/log
            - ./html:/srv
            - ./ajenti:/etc/ajenti
            - ./mysql-data:/var/lib/mysql
            - ./apache2:/etc/apache2
        ports:
            - "8084:8000"
            #NGINX
            - "8085:80"
            #APACHE
            - "8086:8080"
            - "3308:3306"

Hope this will help to find a solution !

3
  • Could help to know exactly how is your Dockerfile and the docker run commands you are using. Commented Mar 4, 2017 at 16:40
  • Also the docker-compose.yml would be helpful. Commented Mar 4, 2017 at 17:11
  • I edited my question with all the informations, I hope this can help ! Commented Mar 6, 2017 at 9:35

3 Answers 3

8

I finally found a solution and it was pretty simple...

First of all, I need to let mysql bind external address, so I changed the line bind-address to '0.0.0.0' inside the container. Next I just changed the command line with mysql -h 127.0.0.1 -P 3308 -u root -pMyPassword

Now it's fine, I can access container mysql data from the host.

Thanks all for your help :)

Sign up to request clarification or add additional context in comments.

1 Comment

I have specified bind-address to 0.0.0.0 and I am able to access database from same machine but how to access MySQL container databases from another machine?
1

In my case I was confused because docker used a different host and port. So you need to find them then do this:

mysql -P <portnumber> -h <host IP> -u db_name -p

Most people would put the docker DB related variables into the environment of the docker container so do this:

sudo docker exec -it container_name env

See if there's a variable called DB_HOST or DB_PORT or something like that. If not then look thru the source code. If it's a PHP project then find a config directory and look in main.php and see

Comments

0

if you execute MySQL operation as entrypoint in the dockerfile file, you will only see that operation when you connect to the container. try changing the entrypoint.

https://docs.docker.com/engine/reference/builder/#entrypoint

2 Comments

I edited my question. What do you mean by changing the entrypoint? Do you have an example?
Can you put the content of you docker-entrypoint.sh file? that commands can't be done on building instead?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.