Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing docker container mysql databases

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 !

like image 823
Mayous Avatar asked Mar 04 '17 15:03

Mayous


People also ask

Can I run MySQL in a Docker container?

MySQL is one of the most popular SQL-compatible relational databases. Running MySQL inside a Docker container lets you separate your database from your code. You can also use a container orchestrator like Kubernetes to scale MySQL independently of your API server instance.

How do I access MariaDB in Docker container?

Execute the following to connect to MariaDB using the command-line client: > docker exec -it mdb mariadb --user root -pPassword123! And that's it! That's all you need to connect to and start using (querying) MariaDB.


2 Answers

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 :)

like image 173
Mayous Avatar answered Oct 15 '22 21:10

Mayous


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

like image 22
Joe C Avatar answered Oct 15 '22 21:10

Joe C