2

I would like to access mariadb in the db container from the app container with the following folder configuration, but it does not work with an error.

directory structure

.
├── app
│   └── Dockerfile
├── db
│   └── Dockerfile
└── docker-compose.yml

Code

app/Dockerfile

FROM debian:buster

RUN set -x \
    && apt-get update \
    && apt-get install --no-install-recommends --no-install-suggests -y \
        mariadb-client \
        vim

CMD [ "tail", "-f" ]

db/Dockerfile

FROM debian:buster

RUN set -x \
    && apt-get update \
    && apt-get install --no-install-recommends --no-install-suggests -y \
        mariadb-server \
        mariadb-client \
        vim

CMD service mysql start \
    && tail -f /dev/null

docker-compose.yml

version: "3.9"

services:
  app:
    build: ./app
    networks:
      - frontend

  db:
    build: ./db
    volumes:
      - db_data:/var/lib/mysql
    expose:
      - 3306
    networks:
      - frontend

networks:
  frontend:
    driver: bridge

volumes:
  db_data: {}

Execution Commands

I entered the app container and executed the following commands, but I get an error.

root@0e0ad0889639:/# mysql -h db -uroot
ERROR 2002 (HY000): Can't connect to MySQL server on 'db' (115)

root@0e0ad0889639:/# ping db
PING db (192.168.128.3) 56(84) bytes of data.
64 bytes from test_db_1.test_frontend (192.168.128.3): icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from test_db_1.test_frontend (192.168.128.3): icmp_seq=2 ttl=64 time=0.142 ms

How can I connect?

Environment

❯ docker -v        
Docker version 20.10.12, build e91ed57

❯ docker-compose -v               
docker-compose version 1.29.2, build 5becea4c

❯ sw_vers
ProductName:    macOS
ProductVersion: 12.3
BuildVersion:   21E230
defke
  • 41
  • 1
  • 4
  • Might be a problem with the docker network not allowing connections. A useful test is to try and use the `telnet` program from the app container to connect to port 3306 on the database container: `telnet db 3306`. If the command exits with an error, you'll know there's some problem with the network. – markusjm Mar 22 '22 at 06:15
  • Are you able to connect (`mysql -uroot`) while in the `app` container? – steven7mwesigwa Mar 22 '22 at 06:19
  • 1
    Does this answer your question? [ERROR 2002 (HY000): Can't connect to MySQL server on '192.168.1.15' (115)](https://stackoverflow.com/questions/64320136/error-2002-hy000-cant-connect-to-mysql-server-on-192-168-1-15-115) – derpirscher Mar 22 '22 at 06:19
  • 4
    Any particular reason you are not using a preconfigured image which is correctly configured for allowing remote connections? The default installation of mariadb only allows connections from localhost – derpirscher Mar 22 '22 at 06:28

2 Answers2

0

The following method in db container solved the problem.

  1. database setting
USE mysql;
GRANT ALL ON *.* TO 'root'@'%' identified by 'pass' WITH GRANT OPTION ;
GRANT ALL ON *.* TO 'root'@'localhost' identified by 'pass' WITH GRANT OPTION ;
FLUSH PRIVILEGES ;
  1. mysql config file setting
echo "bind-address = app" >> etc/mysql/mariadb.conf.d/50-server.cnf
defke
  • 41
  • 1
  • 4
0

As pointed out by comments, the default Debian MariaDB configures a bind-address to localhost. This is why the Docker Library mariadb image removes those configuration items.

If all you want is a vim installed in your container maybe the following to gain all the benefits of a maintained and tested container image:

db/Dockerfile

FROM mariadb:10.6
ENV MARIADB_ROOT_PASSWORD=pass
RUN set -x \
    && apt-get update \
    && apt-get install --no-install-recommends --no-install-suggests -y \
        vim; \
    rm -rf /var/lib/apt/lists/*

note small editors exist if you are interested.

danblack
  • 12,130
  • 2
  • 22
  • 41