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
CodePudding user response:
The following method in db container solved the problem.
- 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 ;
- mysql config file setting
echo "bind-address = app" >> etc/mysql/mariadb.conf.d/50-server.cnf
CodePudding user response:
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.